Find the version ready to install mysql
https://hub.docker.com/_/mysql/tags
- docker pull mysql:5.7
Create a new mysql container instance
ps: Change /opt/glpra/mysql/conf to your own local mapping path
Order:
docker run -d -p 3306:3306 --privileged=true -v /opt/glpra/mysql/log:/var/log/mysql -v /opt/glpra/mysql/data:/var/lib/mysql -v /opt/glpra/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
Create a new my.cnf to solve the Chinese garbled problem and configuration
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
## 设置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
Enter the mysql-master container
- docker exec -it mysql /bin/bash
进入mysql
mysql -uroot -proot
Create a data synchronization user in the master container instance
1: Create a user
2: Authorize this user, which read and write operation permissions can be done
- CREATE USER ‘slave’@‘%’ IDENTIFIED BY ‘123456’;
- GRANT REPLICATION SLAVE, REPLICATION CLIENT ON . TO ‘slave’@‘%’;
Create a new slave mysql-slave container instance
ps: Change /opt/glpra/mysql/conf to your own local mapping path
- Master-slave replication command parameter description
master_host: IP address of the master database;
master_port: running port of the master database;
master_user: user account created in the master database for synchronizing data;
master_password: user created in the master database for synchronizing data Password;
master_log_file: Specifies the log file to copy data from the database, and obtains 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: Interval between retries on connection failures, in seconds.
Order:
docker run -d -p 3307:3306 --privileged=true -v /opt/glpra/mysql-slave/log:/var/log/mysql -v /opt/glpra/mysql-slave/data:/var/lib/mysql -v /opt/glpra/mysql-slave/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-slave mysql:5.7
## 进入/mydata/mysql-slave/conf目录下新建my.cnf
- vim my.cnf
```bash
[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
Restart the slave instance after modifying the configuration
- docker restart mysql-slave
View the master-slave synchronization status in the master database
- show master status;
Enter the mysql-slave container
- docker exec -it mysql-slave /bin/bash
- Enter mysql to configure master-slave replication in the slave database
change master to master_host='192.168.58.130', master_user='slave', master_password='123456', master_port=3306, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;
- View the master-slave synchronization status in the slave database
show slave status \G;
- Enable master-slave synchronization in the slave database
start slave;
- Check the status of the slave database and find that it has been synchronized
Master-slave replication test
-
Host new library - use library - new table - insert data
main library - create table:
main library - view inserted table data
-
Slave use library - view records