1. Create a new main server container instance 3307
docker run -d -p 3307:3306 -v /mydata/mysql-master/log:/var/log/mysql -v /mydata/mysql-master/data:/var/lib/mysql -v /mydata/mysql-master/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root --name mysql-master mysql:5.7
2. Enter the /mydata/mysql-master/conf directory to create a new my.cnf
vim 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
3. Restart the master instance after modifying the configuration
docker restart mysql-master
4. Enter the mysql-master container
docker exec -it mysql-master /bin/bash
mysql -uroot -proot
5. Create a data synchronization user in the master container instance
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
6. Create a new slave server container instance 3308
docker run -d -p 3308:3306 --privileged=true -v /mydata/mysql-slave/log:/var/log/mysql -v /mydata/mysql-slave/data:/var/lib/mysql -v /mydata/mysql-slave/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root --name mysql-slave mysql:5.7
7. Enter the /mydata/mysql-slave/conf directory to create a new my.cnf
vim 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
8. Restart the slave instance after modifying the configuration
docker restart mysql-slave
9. View the master-slave synchronization status in the master database
show master status;
10. Enter the mysql-slave container
docker exec -it mysql-slave /bin/bash
mysql -uroot -proot
11. Configure master-slave replication in the slave database
change master to master_host='宿主机ip', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;
Master-slave replication command parameter description
master_host: the IP address of the master database;
master_port: the running port of the master database;
master_user: the user account created in the master database for synchronizing data;
master_password: the user password created in the master database for synchronizing data;
master_log_file: Specify the log file to copy data from the database, and obtain the File parameter by viewing the status of the master data;
master_log_pos: Specifies where to start copying data from the database, and obtains the Position parameter by viewing the status of the master data;
master_connect_retry: The time interval for retrying a connection failure, in seconds.
12. View the master-slave synchronization status in the slave database
show slave status \G;
13. Enable master-slave synchronization in the slave database
14. View the status of the slave database and find that it has been synchronized
15. Master-slave replication test
Create a new library on the host - use the library - create a new table - insert data
Slave use library - view records