1、GTID簡介html
MySQL 5.6 的新特性之一,是加入了全局事務 ID (GTID) 來強化數據庫的主備一致性,故障恢復,以及容錯能力。mysql
什麼是GTID?linux
官方文檔:http://dev.mysql.com/doc/refman/5.6/en/replication-gtids.html在這篇文檔裏,咱們能夠知道全局事務 ID 的官方定義是:GTID = source_id:transaction_idsql
MySQL 5.6 中,每個 GTID 表明一個數據庫事務。在上面的定義中,source_id 表示執行事務的主庫 uuid(server_uuid),transaction_id 是一個從 1 開始的自增計數,表示在這個主庫上執行的第 n 個事務。MySQL 會保證事務與 GTID 之間的 1 : 1 映射。數據庫
2、環境準備vim
操做系統:CentOS6.5 64位安全
數據庫版本:MySQL5.6.23服務器
拓撲以下:多線程
3、安裝主數據庫(masterdb.example.com)socket
一、準備數據存放目錄、建立用戶
1 1 [root@masterdb ~]#mkdir /data/mysqldata -p #建立數據存放目錄 2 2 [root@masterdb ~]#mkdir /data/mysqlLog/logs -p #建立日誌存放目錄 3 3 [root@masterdb ~]#groupadd -r mysql 4 4 [root@masterdb ~]#useradd -g mysql -r -s /sbin/nologin -M -d /data/mysqldata mysql 5 5 [root@masterdb ~]#chown -R mysql:mysql /data/mysqldata 6 6 [root@masterdb ~]#chown -R mysql:mysql /data/mysqlLog/logs
二、安裝並初始化mysql5.6.23
1 [root@masterdb ~]# tar xf mysql-advanced-5.6.23-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ 2 [root@masterdb ~]# cd /usr/local/ 3 [root@masterdb ~]# ln -sv mysql-advanced-5.6.23-linux-glibc2.5-x86_64 mysql 4 [root@masterdb ~]# chown -R root.mysql mysql 5 [root@masterdb ~]# cd mysql 6 [root@masterdb ~]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld 7 [root@masterdb ~]# cp support-files/my-default.cnf /etc/my.cnf 8 [root@masterdb ~]# chmod +x /etc/rc.d/init.d/mysqld 9 [root@masterdb ~]# chkconfig --add mysqld 10 [root@masterdb ~]# chkconfig mysqld on 11 [root@masterdb ~]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldata/
三、輸出mysql的man手冊至man命令的查找路徑:
編輯/etc/man.config,添加以下行便可:
MANPATH /usr/local/mysql/man
四、輸出mysql的頭文件至系統頭文件路徑/usr/include:
這能夠經過簡單的建立連接實現:
1 [root@masterdb ~]#ln -sv /usr/local/mysql/include /usr/include/mysql
五、輸出mysql的庫文件給系統庫查找路徑:
1 [root@masterdb ~]#echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
然後讓系統從新載入系統庫:
1 [root@masterdb ~]# ldconfig
六、修改PATH環境變量,讓系統能夠直接使用mysql的相關命令:
1 [root@masterdb ~]# vim /etc/profile.d/mysql.sh 2 export PATH=$PATH:/usr/local/mysql/bin 3 [root@masterdb ~]#source /etc/profile.d/mysql.sh
從數據庫安裝同上,具體過程略過。
4、分別爲主從數據庫提供配置文件/etc/my.cnf
要在MySQL 5.6中使用複製功能,其服務配置段[mysqld]中於少應該定義以下選項:
binlog-format:二進制日誌的格式,有row、statement和mixed幾種類型;
須要注意的是:當設置隔離級別爲READ-COMMITED必須設置二進制日誌格式爲ROW,如今MySQL官方認爲STATEMENT這個已經再也不適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會致使主從數據不一致;
log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用於啓動GTID及知足附屬的其它需求;
master-info-repository和relay-log-info-repository:啓用此兩項,可用於實如今崩潰時保證二進制及從服務器安全的功能;
sync-master-info:啓用之可確保無信息丟失;
slave-paralles-workers:設定從服務器的SQL線程數;0表示關閉多線程複製功能;
binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:啓用複製有關的全部校驗功能;
binlog-rows-query-log-events:啓用之可用於在二進制日誌記錄事件相關的信息,可下降故障排除的複雜度;
log-bin:啓用二進制日誌,這是保證複製功能的基本前提;
server-id:同一個複製拓撲中的全部服務器的id號必須唯一;
主數據庫上:
1 [client] 2 port = 3306 3 socket = /tmp/mysql.sock 4 default-character-set = utf8 5 [mysql] 6 no-auto-rehash 7 default-character-set = utf8 8 9 [mysqld] 10 server-id = 1 11 port = 3306 12 user = mysql 13 basedir = /usr/local/mysql 14 datadir = /data/mysqldata 15 socket = /tmp/mysql.sock 16 default-storage-engine = INNODB 17 character-set-server = utf8 18 connect_timeout = 60 19 interactive_timeout = 28800 20 wait_timeout = 28800 21 back_log = 500 22 event_scheduler = ON 23 skip_name_resolve = ON; 24 25 ###########binlog########## 26 log-bin = /data/mysqlLog/logs/mysql-bin 27 binlog_format = row 28 max_binlog_size = 128M 29 binlog_cache_size = 2M 30 expire-logs-days = 5 31 log-slave-updates=true 32 gtid-mode=on 33 enforce-gtid-consistency=true 34 master-info-repository=TABLE 35 relay-log-info-repository=TABLE 36 sync-master-info=1 37 slave-parallel-workers=4 38 #rpl_semi_sync_master_enabled = 1 39 40 slow_query_log = 1 41 slow_query_log_file = /data/mysqlLog/logs/mysql.slow 42 long_query_time = 1 43 44 log_error = /data/mysqlLog/logs/error.log 45 max_connections = 3000 46 max_connect_errors = 32767 47 log_bin_trust_function_creators = 1 48 transaction_isolation = READ-COMMITTED
從數據庫上:
1 [client] 2 port = 3306 3 socket = /tmp/mysql.sock 4 default-character-set = utf8 5 6 [mysql] 7 no-auto-rehash 8 default-character-set = utf8 9 10 [mysqld] 11 server-id = 205 12 port = 3306 13 user = mysql 14 basedir = /usr/local/mysql 15 datadir = /data/mysqldata 16 socket = /tmp/mysql.sock 17 default-storage-engine = INNODB 18 character-set-server = utf8 19 connect_timeout = 60 20 wait_timeout = 18000 21 back_log = 500 22 event_scheduler = ON 23 24 ###########binlog########## 25 log-bin = /data/mysqlLog/logs/mysql-bin 26 binlog_format = row 27 max_binlog_size = 128M 28 binlog_cache_size = 2M 29 expire-logs-days = 5 30 log-slave-updates=true 31 gtid-mode=on 32 enforce-gtid-consistency=true 33 master-info-repository=TABLE 34 relay-log-info-repository=TABLE 35 sync-master-info=1 36 slave-parallel-workers=4 37 #rpl_semi_sync_slave_enabled = 1 38 skip-slave-start 39 40 slow_query_log = 1 41 slow_query_log_file = /data/mysqlLog/logs/mysql.slow 42 long_query_time = 2 43 44 log-error = /data/mysqlLog/logs/error.log 45 max_connections = 3000 46 max_connect_errors = 10000 47 log_bin_trust_function_creators = 1 48 transaction_isolation = READ-COMMITTED
5、分別在主從數據庫上啓動mysqld服務
1 [root@masterdb ~]# service mysqld start 2 Starting MySQL...... [ OK ] 3 [root@masterdb ~]#
1 [root@slavedb ~]# service mysqld start 2 Starting MySQL...... [ OK ] 3 [root@slavedb ~]#
6、在主數據庫上建立複製用戶
1 mysql> GRANT REPLICATION SLAVE ON *.* TO repluser@172.16.88.205 IDENTIFIED BY 'replpassword';
說明:172.16.88.205是從節點服務器;若是想一次性受權更多的節點,能夠自行根據須要修改;
7、啓動從數據庫上的複製線程
mysql> CHANGE MASTER TO MASTER_HOST='masterdb.example.com', MASTER_USER='repluser', MASTER_PASSWORD='replpassword', MASTER_AUTO_POSITION=1; mysql>start slave;
8、在從數據庫上查看複製狀態:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: masterdb.56xyl.com Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 191 Relay_Log_File: slavedb-relay-bin.000003 Relay_Log_Pos: 401 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes #IO線程已正常運行 Slave_SQL_Running: Yes #SQL線程已正常運行 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: 191 Relay_Log_Space: 1899 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: 971d7245-c3f8-11e5-8b6b-000c2999e5a5 Master_Info_File: mysql.slave_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: 971d7245-c3f8-11e5-8b6b-000c2999e5a5:1-6 Executed_Gtid_Set: 89e78301-c3f4-11e5-8b51-00505624d26a:1-3, 971d7245-c3f8-11e5-8b6b-000c2999e5a5:1-6 Auto_Position: 1 1 row in set (0.00 sec) ERROR: No query specified mysql>
9、測試
在主庫上建立數據庫:
1 mysql> create database log_statics; 2 Query OK, 1 row affected (0.11 sec) 3 4 mysql> use log_statics; 5 Database changed
到從數據庫上查看log_statics是否已經複製過去
1 mysql> show databases; 2 +--------------------+ 3 | Database | 4 +--------------------+ 5 | information_schema | 6 | log_statics | 7 | mysql | 8 | performance_schema | 9 +--------------------+ 10 4 rows in set (0.01 sec) 11 12 mysql>
能夠看到log_statics數據庫已經存在於從數據庫上。