【Docker】从零开始:14.MYSQL主从复制流安装部署
1.下载MySql镜像
[root@docker ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@docker ~]#
2.新建主机容器实例
2.1新建主机映射目录
[root@docker ~]# mkdir -p /mydata/mysql-master/log
[root@docker ~]# mkdir -p /mydata/mysql-master/data
[root@docker ~]# mkdir -p /mydata/mysql-master/conf
[root@docker ~]# ls -l /mydata/mysql-master/
总用量 0
drwxr-xr-x. 2 root root 6 12月 5 16:08 conf
drwxr-xr-x. 2 root root 6 12月 5 16:07 data
drwxr-xr-x. 2 root root 6 12月 5 16:07 log
[root@docker ~]#
2.2启动实例
[root@docker ~]# docker run -p 3307:3306 --name mysql-master -v /mydata/mysql-master/log:/var/log/mysql -v /mydata/mysql-master/data:/var/lib/mysql -v /mydata/mysql-master/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
11a52a777891eaddda5d2d280cce4adbdca4112db0d5ec60bdf907d3d3ba6197
[root@docker ~]#
2.3配置主机
2.3.1 my.cnf配置文件
- 进入/mydata/mysql-master/conf目录下新建my.cnf文件
内容如下
[mysqld]
server_id=101
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
[root@docker ~]# cd /mydata/mysql-master/conf
[root@docker conf]# vi my.cnf
[root@docker conf]# cat my.cnf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
[root@docker conf]#
2.3.2 进入主机配置同步用户
[root@docker conf]# cd ~
[root@docker ~]# docker exec -it mysql-master /bin/bash
root@11a52a777891:/# mysql -uroot -proot
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 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 USER 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql>
3.3 重启主机实例
[root@docker conf]# docker restart mysql-master
mysql-master
[root@docker conf]#
3.新建备机容器实例
3.1新建备机映射目录
[root@docker ~]# mkdir -p /mydata/mysql-slave/log
[root@docker ~]# mkdir -p /mydata/mysql-slave/data
[root@docker ~]# mkdir -p /mydata/mysql-slave/conf
[root@docker ~]# ls /mydata/mysql-slave/
conf data log
[root@docker ~]#
3.2启动实例
[root@docker ~]# docker run -p 3308:3306 --name mysql-slave -v /mydata/mysql-slave/log:/var/log/mysql -v /mydata/mysql-slave/data:/var/lib/mysql -v /mydata/mysql-slave/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
04aff65322fc15515df4b8bca75812b049f332f1abd31c1351ae27b4af764323
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04aff65322fc mysql:5.7 "docker-entrypoint.s…" 11 seconds ago Up 10 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp mysql-slave
11a52a777891 mysql:5.7 "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
[root@docker ~]#
3.3新建备机的my.cnf配置
[root@docker ~]# cd /mydata/mysql-slave/conf/
[root@docker conf]# vi my.cnf
[root@docker conf]# cat my.cnf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mall-mysql-slave1-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
## relay_log配置中继日志
relay_log=mall-mysql-relay-bin
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1
## slave设置为只读(具有super权限的用户除外)
read_only=1
[root@docker conf]#
3.3 重启备机实例
[root@docker conf]# docker restart mysql-slave
mysql-slave
[root@docker conf]#
4.查看状态
主机
mysql-master
[root@docker conf]# docker exec -it mysql-master /bin/bash
root@11a52a777891:/# mysql -uroot -proot
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 2
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| mall-mysql-bin.000001 | 154 | | mysql | |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
备机
mysql> exit
Bye
root@11a52a777891:/# exit
exit
[root@docker conf]# docker exec -it mysql-slave /bin/bash
root@04aff65322fc:/# mysql -uroot -proot
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 2
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 master status;
+------------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------------+----------+--------------+------------------+-------------------+
| mall-mysql-slave1-bin.000001 | 154 | | mysql | |
+------------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
5.配置并开启MySql主从服务
5.1 创建复制槽
root@04aff65322fc:/# mysql -uroot -proot
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 3
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> change master to master_host='192.168.40.21', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=154, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.40.21
Master_User: slave
Master_Port: 3307
Connect_Retry: 30
Master_Log_File: mall-mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mall-mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mall-mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 154
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
5.2 开启复制流
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.40.21
Master_User: slave
Master_Port: 3307
Connect_Retry: 30
Master_Log_File: mall-mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mall-mysql-relay-bin.000002
Relay_Log_Pos: 325
Relay_Master_Log_File: mall-mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 537
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 101
Master_UUID: c6c8db19-9345-11ee-9d1a-0242ac110002
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
6.测试
6.1在主机中插入数据
[root@docker conf]# docker exec -it mysql-master /bin/bash
root@11a52a777891:/# mysql -uroot -proot
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 4
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database test01;
Query OK, 1 row affected (0.00 sec)
mysql> use test01;
Database changed
mysql> create table student_info(
-> sno varchar(10) primary key ,
-> name varchar(10),
-> age int(3),
-> sex char(1),
-> school varchar(10),
-> class varchar(10),
-> address varchar(20)
-> )engine = Innodb default charset = utf8mb4 comment='';
mysql> insert into student_info (sno,name,sex,age,school,class,address) values('001','','',15,'','','1');insert into student_info (sno,name,sex,age,school,class,address) values('002','','',15,'','','2');insert into student_info (sno,name,sex,age,school,class,address) values('003','','',15,'','','3');insert into student_info (sno,name,sex,age,school,class,address) values('004','','',15,'','','4');insert into student_info (sno,name,sex,age,school,class,address) values('005','','',15,'','','5');insert into student_info (sno,name,sex,age,school,class,address) values('006','','',15,'','','6');insert into student_info (sno,name,sex,age,school,class,address) values('007','','',15,'','','7');insert into student_info (sno,name,sex,age,school,class,address) values('008','','',15,'','','8');insert into student_info (sno,name,sex,age,school,class,address) values('009','','',15,'','','9');insert into student_info (sno,name,sex,age,school,class,address) values('010','','',15,'','','10');insert into student_info (sno,name,sex,age,school,class,address) values('011','','',15,'','','11');
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> select * from student_info;
+-----+------+------+------+--------+-------+---------+
| sno | name | age | sex | school | class | address |
+-----+------+------+------+--------+-------+---------+
| 001 | | 15 | | | | 1 |
| 002 | | 15 | | | | 2 |
| 003 | | 15 | | | | 3 |
| 004 | | 15 | | | | 4 |
| 005 | | 15 | | | | 5 |
| 006 | | 15 | | | | 6 |
| 007 | | 15 | | | | 7 |
| 008 | | 15 | | | | 8 |
| 009 | | 15 | | | | 9 |
| 010 | | 15 | | | | 10 |
| 011 | | 15 | | | | 11 |
+-----+------+------+------+--------+-------+---------+
11 rows in set (0.00 sec)
mysql>
create table student_info(
sno varchar(10) primary key ,
name varchar(10),
age int(3),
sex char(1),
school varchar(10),
class varchar(10),
address varchar(20)
)engine = Innodb default charset = utf8mb4 comment='学生表';
insert into student_info (sno,name,sex,age,school,class,address) values('001','赵光明','男',15,'第一中学','初三','人民路1号');
insert into student_info (sno,name,sex,age,school,class,address) values('002','钱仁义','男',15,'第一中学','初三','人民路2号');
insert into student_info (sno,name,sex,age,school,class,address) values('003','孙解放','男',15,'第一中学','初三','人民路3号');
insert into student_info (sno,name,sex,age,school,class,address) values('004','李建设','男',15,'第一中学','初三','人民路4号');
insert into student_info (sno,name,sex,age,school,class,address) values('005','周前进','男',15,'第一中学','初三','人民路5号');
insert into student_info (sno,name,sex,age,school,class,address) values('006','吴胜利','男',15,'第一中学','初三','人民路6号');
insert into student_info (sno,name,sex,age,school,class,address) values('007','郑国强','男',15,'第一中学','初三','人民路7号');
insert into student_info (sno,name,sex,age,school,class,address) values('008','王忠诚','男',15,'第一中学','初三','人民路8号');
insert into student_info (sno,name,sex,age,school,class,address) values('009','张三疯','男',15,'第一中学','初三','人民路9号');
insert into student_info (sno,name,sex,age,school,class,address) values('010','陈二狗','男',15,'第一中学','初三','人民路10号');
insert into student_info (sno,name,sex,age,school,class,address) values('011','程小鸭','男',15,'第一中学','初三','人民路11号');
6.2在备机中查看数据
mysql> exit
Bye
root@11a52a777891:/# exit
exit
[root@docker conf]# docker exec -it mysql-slave /bin/bash
root@04aff65322fc:/# mysql -uroot -proot
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 6
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 |
| test01 |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test01
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> select * from student_info;
+-----+------+------+------+--------+-------+---------+
| sno | name | age | sex | school | class | address |
+-----+------+------+------+--------+-------+---------+
| 001 | | 15 | | | | 1 |
| 002 | | 15 | | | | 2 |
| 003 | | 15 | | | | 3 |
| 004 | | 15 | | | | 4 |
| 005 | | 15 | | | | 5 |
| 006 | | 15 | | | | 6 |
| 007 | | 15 | | | | 7 |
| 008 | | 15 | | | | 8 |
| 009 | | 15 | | | | 9 |
| 010 | | 15 | | | | 10 |
| 011 | | 15 | | | | 11 |
+-----+------+------+------+--------+-------+---------+
11 rows in set (0.00 sec)
mysql>