环境准备: 两个mysql服务 or 两个服务器, 这里用docker环境
cat docker-compose.yml
version: "3"
services:
mysql-m1:
image: mysql:8.0.28
volumes:
- ${PWD}/mysql/data:/var/lib/mysql
- ${PWD}/mysql/conf/master.conf:/etc/mysql/conf.d/master.cnf
ports :
- "3307:3306"
restart: always
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: root
mysql-s1:
image: mysql:8.0.28
volumes:
- ${PWD}/mysql/data:/var/lib/mysql
- ${PWD}/mysql/conf/slave.conf:/etc/mysql/conf.d/slave.cnf
ports :
- "3307:3306"
restart: always
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: root
主库配置
[mysqld]
log-bin=mysql-bin#开启binlog
server-id=1
gtid_mode=on
enforce-gtid-consistency=on #强制gtid一致性,安全
log-slave-updates=on #在从服务器中记录传过来的主服务器修改日志
explicit_defaults_for_timestamp=true
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
binlog-ignore-db=mysql
max_allowed_packet=128M
主库创建repl用户
mysql> create user repl@'%' identified WITH mysql_native_password by 'repl';
Query OK, 0 rows affected (0.02 sec)
mysql> grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
开启gtid_modo为必须,否则会报如下错误:
从库配置
server-id=2
gtid_mode=on
enforce-gtid-consistency=on
explicit_defaults_for_timestamp=true
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
binlog-ignore-db=mysql
max_allowed_packet=128M
配置文件不能为大写,否则报错
unknown variable ‘GTID_MODE=ON’.
配置gtid_mode=on时 enforce-gtid-consistency=on 为必须,否则报错
GTID_MODE = ON requires ENFORCE_GTID_CONSISTENCY = ON.
从库设置开始复制position
mysql> CHANGE MASTER TO MASTER_HOST='xxx.xxx.xxx.xxx',MASTER_PORT=3307,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=197 ;
从库查看复制状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: xxx.xxx.xxx.xxx
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 2850457
Relay_Log_File: fbc4f27702f7-relay-bin.000005
Relay_Log_Pos: 1546309
Relay_Master_Log_File: mysql-bin.000005
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: 2850457
Relay_Log_Space: 2851232
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: 1
Master_UUID: 54033806-af29-11ec-8fb2-0242ac150002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
...略
完成…