查看运行中的mysql容器
bash
[root@rzk ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAME
S7e6b4bc49a 8cf625070931 "docker-entrypoint.s..." 8 months ago Up 7 weeks 33060/tcp, 0.0.0.0:33->33/tcp, :::33->3306/tcp mysql5.71.2.3.
制作mysql镜像
将容器(正在运行)打包成镜像
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停
sql
[root@rzk ~]# docker commit -a 'rzk' -m "mysql" 7e6b4bc49 mysql:5.71.
查看镜像
csharp
[root@rzk ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql 5.7 375639a3ba20 1 hours ago 448MB1.2.3.
将生成的镜像保存到宿主机上
typescript
[root@rzk ~]# docker save 375639a3ba20 >/home/mysql/mysql5.7.tar1.
将保存的镜像文件上传到另一台云服务器
typescript
[root@rzk mysql]# scp mysql5.7.tar root@xx:/opt/mysqlroot@xx's password: mysql5.7.tar1.2.3.
这是发送tar包的服务器
这是另一台服务器接收tar包的情况
传输比较慢等待即可
在服务器上使用docker加载 刚才上传的mysql5.7tar包(这台服务器是接收到tar包的服务器)
docker load < tar包绝对路径和相对路径都可以
less
[root@rzk mysql]# ls -l总用量 443000-rw-r--r-- 1 root root 453624320 4月 26 03:52 mysql5.7.tar[root@rzk mysql]# pwd/opt/mysql[root@rzk mysql]# docker load < /opt/mysql/mysql5.7.tar 814bff734324: Loading layer [==================================================>] 72.53MB/72.53MB7075fc380548: Loading layer [==================================================>] 338.4kB/338.4kB147b6eb160c4: Loading layer [==================================================>] 9.557MB/9.557MBb4419c864ffe: Loading layer [==================================================>] 4.202MB/4.202MB719288c1485f: Loading layer [==================================================>] 2.048kB/2.048kB418c960a3eca: Loading layer [==================================================>] 53.77MB/53.77MB104ecd74e4e4: Loading layer [==================================================>] 5.632kB/5.632kB5952cb12b9ed: Loading layer [==================================================>] 3.584kB/3.584kB7fa793435ff1: Loading layer [==================================================>] 313.1MB/313.1MB8cb73d42958e: Loading layer [==================================================>] 17.92kB/17.92kB9885efd52d6f: Loading layer [==================================================>] 1.536kB/1.536kB3e81841c1ddd: Loading layer [==================================================>] 6.144kB/6.144kBLoaded image ID: sha256:375639a3ba20b7f16f431b2c5[root@rzk mysql]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE<none> <none> 375639a3ba20 34 hours ago 448MB1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.
解决 REPOSITORY和TAG显示 的问题
docker tag 7a2b8e939029 mysql:5.7
解决办法如下
sql
[root@rzk mysql]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE<none> <none> 375639a3ba20 34 hours ago 448MB[root@rzk mysql]# docker tag 375639a3ba20 mysql:5.7[root@rzk mysql]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql 5.7 375639a3ba20 34 hours ago 448MB1.2.3.4.5.6.7.
此时数据库数据是没有迁移过来的,这一步需要查看原先服务器上的mysql数据
需要查看数据卷挂载在哪里,我这里是挂载在宿主机上 /home/mysql/data 下面迁移数据库也是要挂载到宿主机上运行
docker inspect 容器名
csharp
[root@rzk ~]# docker inspect 7e61.
json
"Mounts": [ { "Type": "bind", "Source": "/home/mysql/conf", "Destination": "/etc/mysql/conf.d", "Mode": "", "RW": true, "Propagation": "rprivate" }, { "Type": "bind", "Source": "/home/mysql/data", "Destination": "/var/lib/mysql", "Mode": "", "RW": true, "Propagation": "rprivate" }, { "Type": "bind", "Source": "/home/mysql/log", "Destination": "/var/log/mysql", "Mode": "", "RW": true, "Propagation": "rprivate" } ],1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.
运行容器并挂载数据文件
docker run --restart=always -d -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/log:/var/log/mysql -v /home/mysql/data:/var/lib/mysql -p 3310:3306 --name mysql5.7 -d mysql:5.7
rust
[root@rzk mysql]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES94c490e7da78 mysql:5.7 "docker-entrypoint.s..." 2 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:3310->3306/tcp, :::3310->3306/tcp mysql5.71.2.3.
|---------------------------------------|-----------------|
| 参数 | 参数解析 |
| --restart=always | 开机启动容器,容器异常自动重启 |
| -d | 以守护进程的方式启动容器 |
| -v /home/mysql/conf:/etc/mysql/conf.d | 映射配置文件 |
| -v /home/mysql/log:/var/log/mysql | 映射日志 |
| -v /home/mysql/data:/var/lib/mysql | 映射数据 |
| -p 3310:3306 | 绑定宿主机端口 |
| --name mysql5.7 | 指定容器名称 |
| -e MYSQL_ROOT_PASSWORD=123123 | 写入配置root密码 |
连接数据库
可以连接成功
此时数据库是没有迁移过来的一片空空
恢复数据
进入mysql容器 然后 使用mysqldump -uroot -p 数据库名 > 数据库名.sql,生成sql文件后数据会自动挂载在宿主机内
查看mysql容器
kotlin
[root@rzk data]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7e6b4bc49a 8cf6250709 "docker-entrypoint.s..." 9 months ago Up 5 days 33060/tcp, 0.0.0.0:3310->3306/tcp, :::3310->3306/tcp mysql5.71.2.3.
进入mysql容器查看数据库数据
kotlin
[root@rzk data]# docker exec -it 7e /bin/bashroot@7e6b4bc49a43:/# lsbin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr varroot@7e6b4bc49a43:/# cd /var/lib/mysql/root@7e6b4bc49a43:/var/lib/mysql# lsauto.cnf ca.pem client-key.pem ib_buffer_pool ib_logfile1 ibtmp1 online_office_system_server private_key.pem server-cert.pem sys wxserverca-key.pem client-cert.pem cre ib_logfile0 ibdata1 mysql performance_schema public_key.pem server-key.pem wordpress1.2.3.4.5.6.7.
生成对应sql文件
javascript
root@7e6b4bc49a43:/var/lib/mysql# mysqldump -uroot -p > /var/lib/mysql/rzk.sqlEnter password: root@7e6b4bc49a43:/var/lib/mysql# mysqldump -uroot -p online_office_system_server > /var/lib/mysql/online_office_system_server.sqlEnter password: root@7e6b4bc49a43:/var/lib/mysql# mysqldump -uroot -p wxserver > /var/lib/mysql/wxserver.sql Enter password: root@7e6b4bc49a43:/var/lib/mysql# mysqldump -uroot -p cre > /var/lib/mysql/cre.sql Enter password: root@7e6b4bc49a43:/var/lib/mysql# mysqldump -uroot -p w > /var/lib/mysql/wordpress.sqlwordpress/ wxserver/ wxserver.sql root@7e6b4bc49a43:/var/lib/mysql# mysqldump -uroot -p wordpress > /var/lib/mysql/wordpress.sqlEnter password: root@7e6b4bc49a43:/var/lib/mysql#1.2.3.4.5.6.7.8.9.10.11.12.13.
将生成的sql文件导入迁移的服务器即可
相关文章