MySQL运维---主从复制GTID
一、主从切换(Failover)及数据补偿问题
1、普通架构切换过程
二、GTID介绍
1、介绍
2、开启GTID相关参数
3、配置过程
1)主库配置文件
2)从库配置文件
3)初始化数据库
4)启动数据库
5)构建主从
6)查看状态
7)执行过事务
4、GTID解决从库误写入问题
注意:最好的解决办法还是从新构建主从
5、GTID复制和普通复制的区别
三、MySQL5.7.26基于GTID配置主从
1、安装服务
tar -xf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar cd mysql-5.7.26-1.el7.x86_64.rpm-bundle rm -f *test* rpm -Uvh ./*.rpm
2、启动mysql
systemctl start mysqld
3、修改密码
grep /var/log/mysqld.log 查看默认密码
mysql -uroot -p
4、修改配置文件,添加如下行
server-id=1 log-bin=mysql-bin gtid-mode=on enforce-gtid-consistency=true
5、重启mysql,主从库都要验证gtid是否开启
mysql> show global variables like '%gtid%'; +----------------------------------+-------------------------------------------+ | Variable_name | Value | +----------------------------------+-------------------------------------------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | 这个参数要为ON | gtid_executed | 816971ab-3df3-11eb-94fa-005056a8f427:1-42 | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | 这个参数要为ON | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-------------------------------------------+ 8 rows in set (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+-------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------------------------------+ | mysql-bin.000008 | 194 | | | 816971ab-3df3-11eb-94fa-005056a8f427:1-42 | +------------------+----------+--------------+------------------+-------------------------------------------+ 1 row in set (0.00 sec)
mysql> grant Replication slave, Replication client on *.* to 'repl'@'%' identified by 'repl.TAFCXY@2019'; 创建同步用的账号
mysql> flush privileges;
6、从库配置主从
配置文件
数据库配置:
mysql> show global variables like '%gtid%'; +----------------------------------+-------------------------------------------+ | Variable_name | Value | +----------------------------------+-------------------------------------------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed | 816971ab-3df3-11eb-94fa-005056a8f427:1-42 | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_owned | | | gtid_purged | 816971ab-3df3-11eb-94fa-005056a8f427:1-42 | | session_track_gtids | OFF | +----------------------------------+-------------------------------------------+ 8 rows in set (0.01 sec) mysql> change master to -> master_host='192.168.112.74', -> master_port=3306, -> master_user='repl', -> master_password='repl.TAFCXY@2019', -> master_auto_position = 194; Query OK, 0 rows affected, 2 warnings (0.08 sec)
7、验证结果
mysql> start slave; Query OK, 0 rows affected (0.05 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.112.74 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000008 Read_Master_Log_Pos: 194 Relay_Log_File: mysqlb-relay-bin.000003 Relay_Log_Pos: 367 Relay_Master_Log_File: mysql-bin.000008 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: 194 Relay_Log_Space: 575 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: 816971ab-3df3-11eb-94fa-005056a8f427 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: 816971ab-3df3-11eb-94fa-005056a8f427:1-42 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
8、报错解决
问题1:
问题2:
解决办法一:跳过错误
(1)停止slave进程 mysql> STOP SLAVE; (2)设置事务号,事务号从Retrieved_Gtid_Set获取 在session里设置gtid_next,即跳过这个GTID mysql> SET @@SESSION.GTID_NEXT= '8f9e146f-0a18-11e7-810a-0050568833c8:4' (3)设置空事物 mysql> BEGIN; COMMIT; (4)恢复事物号 mysql> SET SESSION GTID_NEXT = AUTOMATIC; (5)启动slave进程 mysql> START SLAVE;
解决办法二:重置master方法跳过错误
mysql> STOP SLAVE; mysql> RESET MASTER; mysql> SET @@GLOBAL.GTID_PURGED ='8f9e146f-0a18-11e7-810a-0050568833c8:1-4' mysql> START SLAVE; 上面这些命令的用意是,忽略8f9e146f-0a18-11e7-810a-0050568833c8:1-4 这个GTID事务,下一次事务接着从 5 这个GTID开始,即可跳过上述错误。