1、用MySQL镜像启动一个MySQL容器
[root@lb docker]# docker pull mysql ... [root@lb docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 0d64f46acfd1 7 days ago 544MB [root@lb ~]# mkdir /data1 [root@lb ~]# docker run -d -p 3306:3306 -v /data1:/var/lib/mysql --name mysql1 -e MYSQL_ROOT_PASSWORD=123 mysql d694e8735af3d7481555013bd84f0a8fcd367d98dd36ce4d6b7d0e2fd10ad04a
PS:/var/lib/mysql 是默认得数据存放目录
2、进入容器中创建库表等一些数据
[root@lb /]# docker exec -it mysql1 /bin/bash root@d694e8735af3:/# mysql -uroot -p123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.01 sec) mysql> create table t1 ( id int, name varchar(20), gender char(20)); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | t1 | +----------------+ 1 row in set (0.00 sec) mysql> desc t1; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | gender | char(20) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> insert into t1 values(1,'tom','m'); Query OK, 1 row affected (0.01 sec) mysql> select * from t1; +------+------+--------+ | id | name | gender | +------+------+--------+ | 1 | tom | m | +------+------+--------+ 1 row in set (0.00 sec) mysql> exit Bye root@d694e8735af3:/# exit exit
3、复制目录至另一节点
[root@lb ~]# scp -rp /data1/ 192.168.53.8:/root/
为了节省镜像下载时间,将镜像打包发送过去
[root@lb docker]# docker save mysql>mysql.tar [root@lb docker]# scp ./mysql.tar 192.168.53.8:/root/ 53.8 [root@web2 ~]# docker load </root/mysql.tar
4、在53.8节点上用容器启动MySQL验证
[root@web2 ~]# docker run -d -p 3306:3306 --name mysql2 -v /root/data1/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 mysql b189fbacc5363e9d53d7fa6a8b0d476041e2a6fcedfabfedf337111a8c22463d [root@web2 ~]# docker exec -it mysql2 /bin/bash root@b189fbacc536:/# mysql -uroot -p123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.05 sec) mysql> select * from test.t1; +------+------+--------+ | id | name | gender | +------+------+--------+ | 1 | tom | m | +------+------+--------+ 1 row in set (0.10 sec) mysql>
验证完毕,数据已完成迁移