赞
踩
1.什么是GTID?
1、全局唯一,一个事务对应一个GTID
2、替代传统的binlog+pos复制;使用master_auto_position=1自动匹配GTID断点进行复制
3、MySQL5.6开始支持
4、在传统的主从复制中,slave端不用开启binlog;但是在GTID主从复制中,必须开启binlog
5、slave端在接受master的binlog时,会校验GTID值
6、为了保证主从数据的一致性,多线程同时执行一个GTID
**
2、工作原理
1、master更新数据时,会在事务前产生GTID,一同记录到binlog日志中。
2、slave端的i/o线程将变更的binlog,写入到本地的relay log中。
3、sql线程从relaylog中获取GTID,然后对比slave端的binlog是否有记录。
4、如果有记录,说明该GTID的事务已经执行,slave会忽略。
5、如果没有记录,slave就会从relaylog中执行该GTID的事务,并记录到binlog。
6、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描
在上一个实验的基础上修改,做gitd主从复制
参考:https://editor.csdn.net/md/?articleId=115602798
修改配置文件
master上
gtid-mode=ON
enforce-gtid-consistency=ON
slave上
gtid-mode=ON
enforce-gtid-consistency=ON
在slave上
root@(none) 19:57 mysql>stop slave;
Query OK, 0 rows affected (0.01 sec)
root@(none) 19:58 mysql>reset master;
Query OK, 0 rows affected (0.03 sec)
在slave上配置去master上拉取二进制日志的用户信息和日志文件的位置和名称。
5.1 master上产生新的二进制日志
root@(none) 20:14 mysql>flush logs;
Query OK, 0 rows affected (0.06 sec)
root@(none) 20:14 mysql>show master status;
+--------------------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------------+----------+--------------+------------------+----------------------------------------+
| mysql-compile-bin.000012 | 194 | | | 6243584b-077b-11eb-9633-000c29ae98d2:1 |
+--------------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)
在slaves上启用master的信息
CHANGE MASTER TO MASTER_HOST='192.168.2.77',
MASTER_USER='sc_slave',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1;
```powershell root@(none) 20:57 mysql>CHANGE MASTER TO MASTER_HOST='192.168.2.77', -> MASTER_USER='sc_slave', -> MASTER_PASSWORD='123456', -> MASTER_PORT=3306, -> MASTER_AUTO_POSITION=1; Query OK, 0 rows affected, 2 warnings (0.00 sec) root@(none) 20:57 mysql>start slave; Query OK, 0 rows affected (0.00 sec) root@(none) 20:58 mysql>show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.77 Master_User: sc_slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-compile-bin.000014 Read_Master_Log_Pos: 643 Relay_Log_File: localhost-relay-bin.000008 Relay_Log_Pos: 438 Relay_Master_Log_File: mysql-compile-bin.000014 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: 643 Relay_Log_Space: 759 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: 6243584b-077b-11eb-9633-000c29ae98d2 Master_Info_File: /data/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting 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: 6243584b-077b-11eb-9633-000c29ae98d2:3 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) ERROR: No query specified
验证主从复制的效果
1.在master上新建一个库或者插入一行数据,到slave上去查看是否有。有则成功
出了一个错,I/O线程没起来。原因是服务器ip地址变了,所有要重新授权用户,重新修改ip地址。
create user 'sc_slave'@'%' identified by '123456';
grant replication slave on *.* to 'sc_slave';
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。