一:概念理解:
1.TID:Transaction ID,即Mysql服務器的事務ID號。
2.GTID:Global Transaction ID,全局事務ID,在整個主從複製架構中任何兩個事物ID是不能相同的。
3.全局事物ID是Mster服務器生成一個128位的UUID+事物的ID號組成的,UUID標示主服務器的身份,此UUID在整個主從複製架構中是絕對唯一,而且即使更換主服務器後UUID也不會改變而是繼承當前主服務器的UUID身份。
4.全局事務ID有何用處?簡單來講GTID能夠保證讓一個從服務器到其他的從服務器那裏實現數據複製而且能夠實現數據整合的。GTID在分佈式架構中可以保證數據的一致性。從而也實現了mysql的高可用性。
5.GTID相關操作:默認情況下將一個事務記錄進二進制文件時將首先記錄它的GTID而且GTID和事務相關信息一併要發送給從服務器由從服務器在在本地應用認證但是絕對不會改變原來的事務ID號。
6.因此在GTID的架構上就算有了N層架構,複製是N級架構,但是全局事務ID依然不會改變;有效的保證了數據的完整和安全性。
二:主要選項:
1、my.cnf配置:
Master:
[root@node4 ~]# grep -v '#' /etc/mysql/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
port= 3306
socket= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
datadir = /data/mydata
innodb_file_per_table = 1
log-bin=/data/binlogs/master-bin
binlog_format=ROW
以下是主要的增該內容:
server-id= 100
log-slave-updates=True #slave更新是否記入日誌
master-info-repository=TABLE
relay-log-info-repository=TABLE #此兩項爲打開從服務器崩潰二進制日誌功能,信息記錄在事物表而不是保存在文件
sync-master-info=1 #值爲1確保信息不會丟失
slave-parallel-threads=2 #同時啓動多少個複製線程,最多與要複製的數據庫數量相等即可
binlog-checksum=CRC32 #效驗碼
master-verify-checksum=1 #啓動主服務器效驗
slave-sql-verify-checksum=1 #啓動從服務器效驗
binlog-rows-query-log-events=1 #用於在二進制日誌詳細記錄事件相關的信息,可降低故障排除的複雜度;
report-port=3306 #請求的主機端口
report-host=node4.a.com #請求的主機名,必須是主機名全稱且DNS可解析
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
驗證: 查看當前主機的二進制文件進度:
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000008 | 446 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
查看當前的二進制文件詳細的信息:
mysql> show binlog events in 'master-bin.000008';
+-------------------+-----+-------------------+-----------+-------------+------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+-------------------+-----------+-------------+------------------------------------------------+
| master-bin.000008 | 4 | Format_desc | 100 | 248 | Server ver: 10.0.10-MariaDB-log, Binlog ver: 4 |
| master-bin.000008 | 248 | Gtid_list | 100 | 277 | [] |
| master-bin.000008 | 277 | Binlog_checkpoint | 100 | 321 | master-bin.000008 |
| master-bin.000008 | 321 | Gtid | 100 | 363 | GTID 0-100-1 |
| master-bin.000008 | 363 | Query | 100 | 446 | create database sd |
+-------------------+-----+-------------------+-----------+-------------+------------------------------------------------+
5 rows in set (0.00 sec)
2、創建複製賬號並授權:
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'jack'@'192.168.%.%' IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
Slave:
[root@node5 ~]# grep -v '#' /etc/mysql/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
port= 3306
socket= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = ON
datadir = /data/mydata
log-bin=/data/binlogs/master-bin
server-id= 1
以下爲重點配置部分:
binlog-format=ROW
log-slave-updates=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-threads=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=node5.a.com
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
連接到主服務器:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.10.204',MASTER_USER='jack',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-bin.000013',MASTER_LOG_POS=379;
mysql> start slave;
然後確認複製進程是否已經成功啓動:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.204
Master_User: jack
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000013
Read_Master_Log_Pos: 725
Relay_Log_File: node5-relay-bin.000002
Relay_Log_Pos: 714
Relay_Master_Log_File: master-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
同步成功後將從進程關閉:
mysql> stop slave;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.10.204',MASTER_USER='jack',MASTER_PASSWORD='123456',MASTER_USE_GTID=current_pos;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.204
Master_User: jack
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000013
Read_Master_Log_Pos: 725
Relay_Log_File: node5-relay-bin.000002
Relay_Log_Pos: 714
Relay_Master_Log_File: master-bin.000013
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: 725
Relay_Log_Space: 1015
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: 100
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: Current_Pos
Gtid_IO_Pos: 0-100-4
已經啓動完成: