数据库
首页 > 数据库> > Docker迁移Mysql到另一台服务器上

Docker迁移Mysql到另一台服务器上

作者:互联网

查看运行中的mysql容器

[root@rzk ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS       PORTS                                                  NAMES
7e6b4bc49a   8cf625070931   "docker-entrypoint.s…"   8 months ago   Up 7 weeks   33060/tcp, 0.0.0.0:33->33/tcp, :::33->3306/tcp   mysql5.7

制作mysql镜像

将容器(正在运行)打包成镜像
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停

[root@rzk ~]# docker commit -a 'rzk' -m "mysql" 7e6b4bc49 mysql:5.7

查看镜像

[root@rzk ~]# docker images
REPOSITORY                                             TAG       IMAGE ID       CREATED        SIZE
mysql                                                  5.7       375639a3ba20   1 hours ago   448MB

将生成的镜像保存到宿主机上

[root@rzk ~]# docker save 375639a3ba20 >/home/mysql/mysql5.7.tar

将保存的镜像文件上传到另一台云服务器

[root@rzk mysql]# scp mysql5.7.tar  root@xx:/opt/mysql
root@xx's password: 
mysql5.7.tar     

这是发送tar包的服务器

这是另一台服务器接收tar包的情况

传输比较慢等待即可

在服务器上使用docker加载 刚才上传的mysql5.7tar包(这台服务器是接收到tar包的服务器)

docker load < tar包绝对路径和相对路径都可以

[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.53MB
7075fc380548: Loading layer [==================================================>]  338.4kB/338.4kB
147b6eb160c4: Loading layer [==================================================>]  9.557MB/9.557MB
b4419c864ffe: Loading layer [==================================================>]  4.202MB/4.202MB
719288c1485f: Loading layer [==================================================>]  2.048kB/2.048kB
418c960a3eca: Loading layer [==================================================>]  53.77MB/53.77MB
104ecd74e4e4: Loading layer [==================================================>]  5.632kB/5.632kB
5952cb12b9ed: Loading layer [==================================================>]  3.584kB/3.584kB
7fa793435ff1: Loading layer [==================================================>]  313.1MB/313.1MB
8cb73d42958e: Loading layer [==================================================>]  17.92kB/17.92kB
9885efd52d6f: Loading layer [==================================================>]  1.536kB/1.536kB
3e81841c1ddd: Loading layer [==================================================>]  6.144kB/6.144kB
Loaded image ID: sha256:375639a3ba20b7f16f431b2c5
[root@rzk mysql]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
<none>       <none>    375639a3ba20   34 hours ago   448MB

解决 REPOSITORY和TAG显示 的问题

docker tag 7a2b8e939029 mysql:5.7

解决办法如下

[root@rzk mysql]# docker images
REPOSITORY   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 images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql        5.7       375639a3ba20   34 hours ago   448MB

此时数据库数据是没有迁移过来的,这一步需要查看原先服务器上的mysql数据

需要查看数据卷挂载在哪里,我这里是挂载在宿主机上 /home/mysql/data 下面迁移数据库也是要挂载到宿主机上运行

docker inspect 容器名

[root@rzk ~]# docker inspect 7e6
        "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"
            }
        ],

运行容器并挂载数据文件

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

--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密码

标签:kB,Loading,另一台,Mysql,rzk,mysql,docker,root,Docker
来源: https://www.cnblogs.com/rzkwz/p/16193144.html