基于 GTID方式 MySQL主从复制

概念

1、全局事务标识:global transaction identifiers。
2、GTID是一个事务一一对应,并且全局唯一ID。
3、一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
4、GTID用来代替传统复制方法,不再使用MASTER_LOG_FILE+MASTER_LOG_POS开启复制。而是使用MASTER_AUTO_POSTION=1的方式开始复制。
5、MySQL-5.6.5开始支持的,MySQL-5.6.10后开始完善。
6、在传统的slave端,binlog是不用开启的,但是在GTID中slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。

组成

GTID = source_id:transaction_id
source_id,用于鉴别原服务器,即mysql服务器唯一的的server_uuid,由于GTID会传递到slave,所以也可以理解为源ID。
transaction_id,为当前服务器上已提交事务的一个序列号,通常从1开始自增长的序列,一个数值对应一个事务。   
配置文件查看
[root@study mysql
-5.7.19-linux-glibc2.12-x86_64]# cat /data/mysql/mysql3306/data/auto.cnf [auto] server-uuid=d9298cf8-8db1-11e7-8fc5-000c291cd934 终端查看
[root@study mysql
-5.7.19-linux-glibc2.12-x86_64]# mysql -p123456 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 65 Server version: 5.6.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT UUID(); +--------------------------------------+ | UUID() | +--------------------------------------+ | 71d44032-a2bb-11e7-9768-000c291cd934 | +--------------------------------------+ 1 row in set (0.00 sec)

优势

1、更简单的实现failover,不用以前那样在需要找log_file和log_pos。
2、更简单的搭建主从复制。
3、比传统的复制更加安全。
4、GTID是连续的没有空洞的,保证数据的一致性,零丢失。

GTID工作原理

1、当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中。
2、binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,即告诉Slave,下一个要执行的GTID值。
3、sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID。
4、如果有记录,说明该GTID的事务已经执行,slave会忽略。
5、如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行。
6、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。

 配置

1、主:
[mysqld]
#GTID:
server_id=1833306               #服务器id
gtid_mode=on                 #开启gtid模式
enforce_gtid_consistency=on  #强制gtid一致性,开启后对于特定create table不被支持

#binlog
log_bin=master-binlog
log-slave-updates=1    
binlog_format=row            #强烈建议,其他格式可能造成数据不一致

#relay log
skip_slave_start=1            

2、从:
[mysqld]
#GTID:
gtid_mode=on
enforce_gtid_consistency=on
server_id=143

#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog_format=row      #强烈建议,其他格式可能造成数据不一致

#relay log
skip_slave_start=1

实践

环境
192.168.18.3 Master
192.168.18.4 Slave

 配置文件修改

配置文件
Master
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

server_id=1833306
gtid_mode=on
enforce_gtid_consistency=on
#binlog
log_bin=master-binlog
log-slave-updates=1
binlog_format=row

##relay log
skip_slave_start=1
slave-skip-errors = all

expire_logs_days = 30
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set=utf8
Slave配置(只有server_id不一样)

 授权用户

Master操作
mysql> grant replication slave on *.* to 'repl'@'192.168.18.4' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)

  同步配置

Slave 操作
[root@wxl ~]# mysql -p123456 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.6.37-log MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.18.3', -> MASTER_PORT=3306, -> MASTER_USER='repl', -> MASTER_PASSWORD='123456', -> MASTER_AUTO_POSITION=1; Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)

 同步信息查看(红色信息为为重要信息)

Slave操作
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.18.3 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-binlog.000002 Read_Master_Log_Pos: 535 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 753 Relay_Master_Log_File: master-binlog.000002 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: 535 Relay_Log_Space: 958 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: 1833306 Master_UUID: e79886da-998a-11e7-816b-000c291cd934 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 the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: e79886da-998a-11e7-816b-000c291cd934:1-2 //接收到的事务日志 Executed_Gtid_Set: e79886da-998a-11e7-816b-000c291cd934:1-2 //已经执行的事务GTID Auto_Position: 1 1 row in set (0.00 sec) ERROR: No query specified mysql> select UUID(); +--------------------------------------+ | UUID() | +--------------------------------------+ | 9b23534c-cdc4-11e7-8640-000c29cb9ace | +--------------------------------------+ 1 row in set (0.00 sec)

 验证

Master操作
mysql> create database test2018;
Query OK, 1 row affected (0.00 sec)
mysql> use test2018
Database changed
mysql> create table t1 (name varchar(20));
Query OK, 0 rows affected (0.06 sec)
mysql> insert  t1 values("wuxinglai");
Query OK, 1 row affected (0.02 sec)
mysql> insert  t1 values("wuxinglai");
Query OK, 1 row affected (0.01 sec)

mysql> select * from t1;
+-----------+
| name      |
+-----------+
| wuxinglai |
| wuxinglai |
+-----------+
Slave 上验证
mysql> select * from test2018.t1;
+-----------+
| name      |
+-----------+
| wuxinglai |
| wuxinglai |
+-----------+
2 rows in set (0.00 sec)
同步配置完成

 

posted @ 2017-11-20 15:45  91King  阅读(275)  评论(0)    收藏  举报