Install Mysql master-slave replication
Create a new main server container instance 3307
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
Enter the /mydata/mysql-master/conf directory to create a new 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
After modifying the configuration, restart the master instance
docker restart mysql-master
Enter the mysql-master container
docker exec -it mysql-master /bin/bash
mysql -uroot -proot
Create a data synchronization user slave in the master container instance
mysql> CREATE USER 'slave'@'%' IDENTIFIED BY 'root'; (添加用户)
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; (授权)
Create a new slave server container instance 3308
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
Enter the /mydata/mysql-slave/conf directory to create a new 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
After modifying the configuration, restart the slave instance
docker restart mysql-slave;
Check the master-slave synchronization status in the master database
show master status;
![](https://img-blog.csdnimg.cn/img_convert/c7a0c958a8531d0f6898bbeb187774d0.png)
Enter the mysql-slave container
docker exec -it mysql-slave /bin/bash
mysql -uroot -proot
Configure master-slave replication in the slave database
change master to master_host='192.168.3.15', master_user='slave', master_password='root', master_port=3307, master_log_file='mall-mysql- bin.000004', master_log_pos=768, master_connect_retry=30;
主从复制参数说明:
master_host:主数据库的IP地址;
master_port:主数据库的运行端口;
master_user:在主数据库创建的用于同步数据的用户账号;
master_password:在主数据库创建的用于同步数据的用户密码;
master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据的状态,获取File参数;
master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据的状态,获取Position参数;
master_connect_retry:连接失败重试的时间间隔,单位为秒。
Check the master-slave synchronization status in the slave database
show slave status \G;
![](https://img-blog.csdnimg.cn/img_convert/2fb4280f0df73de7bcbed9aa5c25dc97.png)
Enable master-slave synchronization in the slave database
start slave;
Check the master-slave synchronization status in the slave database
show slave status \G;
![](https://img-blog.csdnimg.cn/img_convert/6a49315d83da659daf460e465f27b2ce.png)
Master-slave replication test: - Create a new library on the host - use the library - create a new table - insert data, ok
create database db01;
use db01;
create table t1(id int, name varchar(20));
insert into t1 values(1, 'z3');
select * from t1;
- Use the library from the machine - view the record, ok
use db01;
select * from t1;
[Summary of questions]:
1.若Slave_IO_Running: Conncting
![](https://img-blog.csdnimg.cn/img_convert/fd53cd4193600417442607b77178488d.png)
ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
flush privileges;
*2.若Slave_IO_Running: No*
![](https://img-blog.csdnimg.cn/img_convert/7fc67ed5b4e74f4a83247beed3f87e1c.png)
Execute from the database:
stop slave;
reset slave;
start slave;
**
3. To delete one or more user accounts , use the following DROP USER statement: ``**
DROP USER user, [user],...;
**4. To view all users in the MySQL database server
**
SELECT user, host FROM mysql.user;
![](https://img-blog.csdnimg.cn/img_convert/bbbd8a82f9c66ce475a3c2e97e6f4041.png)