MySQL主从复制(GTID模式)

GTID复制原理:

基于GTID的复制是MySQL 5.6后新增的复制方式.
GTID (global transaction identifier) 即全局事务ID, 保证了在每个在主库上提交的事务在集群中有一个唯一的ID.
在原来基于日志的复制中, 从库需要告知主库要从哪个偏移量进行增量同步, 如果指定错误会造成数据的遗漏, 从而造成数据的不一致.
而基于GTID的复制中, 从库会告知主库已经执行的事务的GTID的值, 然后主库会将所有未执行的事务的GTID的列表返回给从库. 并且可以保证同一个事务只在指定的从库执行一次.

GTID是由server_uuid和事物id组成,格式为:GTID=server_uuid:transaction_id。server_uuid是在数据库启动过程中自动生成,每台机器的server-uuid不一样。uuid存放在数据目录的auto.conf文件中,而transaction_id就是事务提交时系统顺序分配的一个不会重复的序列号。

GTID的好处:

(1)GTID使用master_auto_position=1代替了binlog和position号的主从复制搭建方式,相比binlog和position方式更容易搭建主从复制。

(2)GTID方便实现主从之间的failover,不用一步一步的去查找position和binlog文件。

GTID模式复制搭建过程中注意事项:

主从需要设置如下参数(一般直接在配置文件/etc/my.cnf下直接添加):

a、主库配置:

gtid_mode=on

enforce_gtid_consistency=on

log_bin=on

server-id=33062200(主从不能相同)

binlog_format=row

b、从库配置:

gtid_mode=on

enforce_gtid_consistency=on

log_slave_updates=1

server-id=33062211(主从不能相同)

主库上操作:

root@db 15:10:  [mysql]>create user 'repluser'@'192.168.112.%' identified by 'repluser123';

root@db 15:10:  [mysql]> grant replication slave on *.* to 'repluser'@'192.168.112.%';

主库导出数据库脚本all.sql

mysqldump -uroot -hlocalhost -p --single-transaction --master-data=2 -A >all.sql

从库上操作:

将主库上导出的all.sql脚本上传到从库,然后导入从库

mysql -uroot -hlocalhost -p <all.sql

如果出现如下错误:

ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

则在从库命令行下执行:reset master

root@db 15:20:  [mysql]> reset master;

跟普通模式一样,执行change master语句,只不过其中的binlog和position换成master_auto_position=1

CHANGE MASTER TO
MASTER_HOST='192.168.112.220',
MASTER_USER='repluser',
MASTER_PASSWORD='repluser123',
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1;

启动slave复制服务

start slave;

查看主从复制状态:

 1 root@db 15:23:  [mysql]> show slave status\G;
 2 *************************** 1. row ***************************
 3                Slave_IO_State: Waiting for master to send event
 4                   Master_Host: 192.168.112.220
 5                   Master_User: repluser
 6                   Master_Port: 3306
 7                 Connect_Retry: 60
 8               Master_Log_File: on.000002
 9           Read_Master_Log_Pos: 2538363
10                Relay_Log_File: localhost-relay-bin.000004
11                 Relay_Log_Pos: 2031371
12         Relay_Master_Log_File: on.000002
13              Slave_IO_Running: Yes
14             Slave_SQL_Running: Yes
15               Replicate_Do_DB: 
16           Replicate_Ignore_DB: 
17            Replicate_Do_Table: 
18        Replicate_Ignore_Table: 
19       Replicate_Wild_Do_Table: 
20   Replicate_Wild_Ignore_Table: 
21                    Last_Errno: 0
22                    Last_Error: 
23                  Skip_Counter: 0
24           Exec_Master_Log_Pos: 2538363
25               Relay_Log_Space: 2031582
26               Until_Condition: None
27                Until_Log_File: 
28                 Until_Log_Pos: 0
29            Master_SSL_Allowed: No
30            Master_SSL_CA_File: 
31            Master_SSL_CA_Path: 
32               Master_SSL_Cert: 
33             Master_SSL_Cipher: 
34                Master_SSL_Key: 
35         Seconds_Behind_Master: 0
36 Master_SSL_Verify_Server_Cert: No
37                 Last_IO_Errno: 0
38                 Last_IO_Error: 
39                Last_SQL_Errno: 0
40                Last_SQL_Error: 
41   Replicate_Ignore_Server_Ids: 
42              Master_Server_Id: 33060220
43                   Master_UUID: 763c7ef3-5977-11e8-ae7a-000c2936b80f
44              Master_Info_File: mysql.slave_master_info
45                     SQL_Delay: 0
46           SQL_Remaining_Delay: NULL
47       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
48            Master_Retry_Count: 86400
49                   Master_Bind: 
50       Last_IO_Error_Timestamp: 
51      Last_SQL_Error_Timestamp: 
52                Master_SSL_Crl: 
53            Master_SSL_Crlpath: 
54            Retrieved_Gtid_Set: 763c7ef3-5977-11e8-ae7a-000c2936b80f:1393-6926
55             Executed_Gtid_Set: 763c7ef3-5977-11e8-ae7a-000c2936b80f:1-6926
56                 Auto_Position: 1
57          Replicate_Rewrite_DB: 
58                  Channel_Name: 
59            Master_TLS_Version: 
60 1 row in set (0.00 sec)
61 
62 root@db 15:23:  [mysql]> 
View Code

 由上图可知:

Slave_IO_Running: YesS

lave_SQL_Running: Yes

复制主从复制状态OK

猜你喜欢

转载自www.cnblogs.com/kindnull/p/9051358.html