技术流ken

运维拯救世界

项目:docker部署MySQL容器实现数据迁移–技术流ken

项目2:
1.启动mysql容器
2.创建数据库和表
3.模拟节点A宕机
4.在节点B上启动并恢复数据

第一步:启动mysql容器

[root@ken1 ken1]# docker run -d -p 3306:3306 -v /ken3:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 mysql

 

第二步:进入容器创建数据库
[root@ken1 ken1]# docker exec -it ba5b6d4bbda4 bash
root@ba5b6d4bbda4:/# mysql -uroot -p123
mysql> create database ken;
Query OK, 1 row affected (0.43 sec)

mysql> use ken;
Database changed
mysql> create table t1 (
-> id int,
-> name char(20),
-> gender char(20));
Query OK, 0 rows affected (0.32 sec)

mysql> show tables;
+—————+
| Tables_in_ken |
+—————+
| t1 |
+—————+
1 row in set (0.00 sec)

mysql> select * from t1;
Empty set (0.01 sec)

mysql> insert into t1 values (1,”ken”,”M”);
Query OK, 1 row affected (0.19 sec)

mysql> insert into t1 values (2,”liu”,”F”);
Query OK, 1 row affected (0.19 sec)

mysql> select * from t1;
+——+——+——–+
| id | name | gender |
+——+——+——–+
| 1 | ken | M |
| 2 | liu | F |
+——+——+——–+
2 rows in set (0.01 sec)

mysql> exit
Bye

第三步:复制文件至节点B

[root@ken1 ken1]# scp -r /ken3 192.168.64.6:/root

 

第四步:节点B启动docker数据库并验证数据一致性
[root@ken2 ~]# docker run -d -p 3306:3306 -v /root/ken3:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 mysql
4c2d2c8d062581cd336d3337e8494e7367df12825c4d9652c2724eb8242f9a3c
[root@ken2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c2d2c8d0625 mysql “docker-entrypoint.s…” 20 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp priceless_diffie
[root@ken2 ~]# docker exec -it 4c2d2c8d06258 bash
root@4c2d2c8d0625:/# 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.11 MySQL Community Server – GPL

Copyright (c) 2000, 2018, 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 |
| ken |
| mysql |
| performance_schema |
| sys |
+——————–+
5 rows in set (0.02 sec)

mysql> use ken;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tabls;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘tabls’ at line 1
mysql> show tables;
+—————+
| Tables_in_ken |
+—————+
| t1 |
+—————+
1 row in set (0.02 sec)

mysql> select * from t1;
+——+——+——–+
| id | name | gender |
+——+——+——–+
| 1 | ken | M |
| 2 | liu | F |
+——+——+——–+
2 rows in set (0.01 sec)

mysql> exit
Bye

2 thoughts on “项目:docker部署MySQL容器实现数据迁移–技术流ken

发表评论

电子邮件地址不会被公开。