名词解释:
1.TID:Transaction ID,事务的ID号:也就是说在mysql复制中每一个事务都有自己的ID号(随机数)
2.GTID:Global Transaction ID,全局事务ID,在整个事务架构中每一个事务ID号是全局唯一的,不止是在一个节点上而是整个主从复制架构中每任何两个事务的ID号都不会相同。
3.全局事务ID是怎么生成的?简单来讲是由mysql服务器自动管理的,在mysql5.6以后每一个mysql服务器都有一个全局唯一的ID号叫做uuid,通用唯一识别码 (Universally Unique Identifier),而GTID就是由当前节点的UUID(一个128位的随机数)和为当前节点生成的随机数(TID)组成的,因此只要UUID不同再在此基础上保证事务ID不同就保证全局不一样了。
4.全局事务ID有何用处?简单来讲GTID能够保证让一个从服务器到其他的从服务器那里实现数据复制而且能够实现数据整合的。GTID在分布式架构中可以保证数据的一致性。从而也实现了mysql的高可用性。
5.GTID相关操作:默认情况下将一个事务记录进二进制文件时将首先记录它的GTID而且GTID和事务相关信息一并要发送给从服务器由从服务器在在本地应用认证但是绝对不会改变原来的事务ID号。
6.因此在GTID的架构上就算有了N层架构,复制是N级架构、事务ID依然不会改变;有效的保证了数据的完整和安全性。
二、新增选项
1.MySQL5.6引入的GTID(Global Transaction IDs)使得其复制功能的配置、监控及管理变得更加易于实现,且更加健壮。
2.要在MySQL 5.6中使用复制功能,其服务配置段[mysqld]中于少应该定义如下选项:
2.1. binlog-format:二进制日志的格式,有row、statement和mixed几种类型;
需要注意的是:当设置隔离级别为读提交READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;log-slave-updates(slave更新是否记入日志)、gtid-mode(gtid类型)、enforce-gtid-consistency(强制gtid一致性)、report-port和report-host:用于启动GTID及满足附属的其它需求;
2.2.master-info-repository(资源库)和relay-log-info-repository:启用此两项,可用于实现在崩溃时保证二进制及从服务器安全的功能;
2.3.sync-master-info:确保服务器崩溃时无信息丢失;
2.4.slave-paralles-workers:设定从服务器的SQL线程数;0表示关闭多线程复制功能;值与你要复制的数据库数目相同即可;
2.5.binlog-checksum校验码、master-verify-checksum和slave-sql-verify-checksum:启用复制有关的所有校验功能;
2.6.binlog-rows-query-log-events:用于在二进制日志详细记录事件相关的信息,可降低故障排除的复杂度;
2.7.log-bin:启用二进制日志,这是保证复制功能的基本前提;
2.8.server-id:同一个复制拓扑中的所有服务器的id号必须惟一;
三、操作步骤
主服:station20:192.168.1.20
从服:station21:192.168.1.21
support-files/my-default.cnf 几乎为空文件,mysql-5.6不提供my.cnf
修改主节点配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | [iyunv@station20~]# vim /etc/my.cnf server-id = 1 log-bin=mysql-bin binlog_format=row 二进制格式改为行row模式,三种模式statement语句模式,row行模式,mixed混合模式 当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致; #添加以下这些选项 log-slave-updates=true slave更新是否记入日志 gtid-mode=on 启用gtid类型,否则就是普通的复制架构 enforce-gtid-consistency=true 强制GTID的一致性 master-info-repository=TABLE 主服信息记录库=表/文件 relay-log-info-repository=TABLE 中继日志信息记录库 sync-master-info=1 同步主库信息 slave-parallel-workers=4 从服务器的SQL线程数,要复制库数目相同 binlog-checksum=CRC32 校验码 master-verify-checksum=1 主服校验 slave-sql-verify-checksum=1 从服校验 binlog-rows-query-log_events=1 二进制日志详细记录事件 report-port=3306 提供复制报告端口 report-host=station20.example.com 提供复制报告主机 [iyunv@station20~]# service mysqld restart [iyunv@station20~]# mysql -e "show master status;" 已执行过的GTID集 Executed_Gtid_Set +------------------+----------+--------------+------------------+-------------------+ |File | Position | Binlog_Do_DB |Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ |mysql-bin.000002 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ mysql5.6以后每台mysql服务器都有一个全局唯一的ID号叫做uuid,GTID就是由当前节点的UUID(一个128位的随机数)和为当前节点生成的随机数(TID)组成的,因此只要UUID不同再在此基础上保证事务ID不同就保证全局不一样。 [iyunv@station20~]# mysql -e "show global variables like '%uuid%';" +---------------+--------------------------------------+ |Variable_name | Value | +---------------+--------------------------------------+ |server_uuid |3eda76df-e355-11e3-8d42-000c294698bf | +---------------+--------------------------------------+ 创建复制用户 mysql>grant replication slave,replication client on *.* to repluser@'192.168.1.%'identified by 'replpass'; mysql>flush privileges; |
修改从节点配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | [iyunv@station20~]# scp /etc/my.cnf 192.168.1.21:/etc/my.cnf [iyunv@station21~]# vim /etc/my.cnf 从master复制my.cnf,修改关键部分 server-id = 2 log-bin=mysql-bin binlog_format=row log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=4 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 report-port=3306 report-host=station21.example.com [iyunv@station21~]# service mysqld restart [iyunv@station21~]# mysql -e "show global variables like '%uuid%';" +---------------+--------------------------------------+ |Variable_name | Value | +---------------+--------------------------------------+ |server_uuid |08c840ad-e35c-11e3-8d6f-000c29ed6c68 | +---------------+--------------------------------------+ 从库连接主库 mysql>change master to master_host='192.168.1.20', master_user='repluser',master_password='replpass', master_auto_position=1; mysql>start slave; mysql>show slave statusG; ***************************1. row *************************** Slave_IO_State: Waiting formaster to send event Master_Host: 192.168.1.20 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 151 Relay_Log_File:station21-relay-bin.000002 不用配置,自动生成中继日志 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000003 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: 151 Relay_Log_Space: 569 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:3eda76df-e355-11e3-8d42-000c294698bf Master_Info_File:mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has readall 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: Executed_Gtid_Set: Auto_Position: 1 mysql>show processlist; | Id | User | Host | db | Command | Time | State | Info | | 1 | systemuser | | NULL | Connect | 164 | Slave has read all relay log; waitingfor the slave I/O thread to update it | NULL | | 2 | systemuser | | NULL | Connect | 164 | Waiting for master to send event | NULL | | 3 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 4 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 5 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 6 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 7 | root | localhost | NULL | Query | 0| init | show processlist | 4个复制线程,主库上同时创建4个数据库,每个数据库事务启动一个复制线程,这些复制线程可同时启动; 测试主从测试 主库 mysql>create database hellodb; mysql>show databases; +--------------------+ |Database | +--------------------+ |information_schema | |hellodb | |mysql | | performance_schema| |test | +--------------------+ mysql>show processlist; | Id | User |Host | db | Command | Time | State |Info | | 1 |repluser | slave:60241 | NULL | Binlog Dump GTID | 1178 | Master has sent allbinlog to slave; waiting for binlog to be updated | NULL | | 2 | root | localhost | NULL | Query | 0 | init | show processlist | 从库 mysql>show databases; +--------------------+ |Database | +--------------------+ |information_schema | |hellodb | |mysql | |performance_schema | |test | +--------------------+ mysql>show processlist; | Id | User | Host | db |Command | Time | State | Info | 1 | systemuser | | NULL | Connect | 126 | Slave has read all relay log; waitingfor the slave I/O thread to update it | NULL | 2 | systemuser | | NULL | Connect | 920 | Waiting for master to send event | NULL | 3 | systemuser | | NULL | Connect | 920 | Waiting for an event fromCoordinator | NULL | 4 | systemuser | | NULL | Connect | 920 | Waiting for an event fromCoordinator | NULL | 5 | systemuser | | NULL | Connect | 920 | Waiting for an event fromCoordinator | NULL | 6 | systemuser | | NULL | Connect | 126 | Waiting for an event fromCoordinator | NULL | 7 | root |localhost | NULL | Query | 0| init | show processlist |