MySQL5.6新特性GTID、多線程復制
在Oracle發布MySQL5.6看到眾多新特性之后很興奮,包括對復制的改進.在MySQL5.5半同步復制之后MySQL5.6又引入GTID、多線程復制,在這里總結這一下這兩個新特性.
1、引入GTID,在MySQL5.6以前對於主從復制出現問題有時候需要你分析BINLOG找到POS點,然后在CHANG MASTER TO.對於新手來說很容易犯錯,造成主從復制錯誤.在新版本中,不必在需要尋找BINLOG和POS點,你只需要知道MASTER的IP、密碼、端口就可以,因為MySQL會從內部GTID機制自動找到同步點.
2、多線程復制,在MySQL5.6之前,復制是單線程隊列式的,只能一個一個運行.在新版中支持基於庫的多線程復制,但是庫里的表不能多線程.
下面實現以下這兩個新特性
注:以下實驗數據庫版本MySQL5.6.15
MASTER:
my.cnf添加以下參數
binlog_format = row
gtid_mode = ON
enforce-gtid-consistency = ON
slave_parallel_workers=4 --開啟基於庫的多線程復制默認0不開啟
binlog_cache_size = 8M
max_binlog_size = 50M
max_binlog_cache_size = 100M
sync_binlog = 1
expire_logs_days = 1
log-slave-updates=true
SLAVE:
my.cnf添加以下參數
binlog_format = row
gtid_mode = ON
enforce-gtid-consistency = ON
binlog_cache_size = 8M
max_binlog_size = 50M
max_binlog_cache_size = 100M
sync_binlog = 1
expire_logs_days = 1
slave_parallel_workers=4
max_relay_log_size = 50M
relay_log_purge = 1
relay_log_recovery = 1
master_verify_checksum = 1 --主事件校驗
slave_sql_verify_checksum = 1 --從事件校驗
slave_allow_batching = 1
log-slave-updates=true
MASTER:
mysql> use percona;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> show tables;
+-------------------+
| Tables_in_percona |
+-------------------+
| test |
+-------------------+
1 row in set (0.00 sec)
mysql> select * from test;
Empty set (0.02 sec)
mysql> desc test;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into test values (1);
Query OK, 1 row affected (0.04 sec)
mysql> insert into test values (1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test select * from test;
Query OK, 64 rows affected (0.00 sec)
Records: 64 Duplicates: 0 Warnings: 0
mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)
mysql>
SLAVE:
mysql> change master to master_host='192.168.40.176',master_port=3306,master_user='repl',master_password='123456',master_auto_position=1,master_delay=20; --master_delay此參數表示relay日志會同步到slave機,但是會根據事件時間戳延時20秒,在某些場景會用到.
Query OK, 0 rows affected, 0 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected, 0 warning (0.04 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.40.176
Master_User: repl
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000046
Read_Master_Log_Pos: 191
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 401
Relay_Master_Log_File: mysql-bin.000046
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test,percona,mysql
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: 693
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: 2
Master_UUID: 69a73914-62ca-11e3-870f-080027dff846
Master_Info_File: /mysql/data/master.info
SQL_Delay: 30
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:
Executed_Gtid_Set:
Auto_Position: 1
1 row in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.40.176
Master_User: repl
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000050
Read_Master_Log_Pos: 191
Relay_Log_File: mysqld-relay-bin.000009
Relay_Log_Pos: 401
Relay_Master_Log_File: mysql-bin.000050
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test,percona,mysql
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: 856
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: 2
Master_UUID: 69a73914-62ca-11e3-870f-080027dff846
Master_Info_File: /mysql/data/master.info
SQL_Delay: 20
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: 69a73914-62ca-11e3-870f-080027dff846:13-36
Executed_Gtid_Set: 69a73914-62ca-11e3-870f-080027dff846:1-36
Auto_Position: 1
1 row in set (0.00 sec)
開啟SLAVE觀察Retrieved_Gtid_Set與Executed_Gtid_Set
Retrieved_Gtid_Set:記錄從MASTER獲取BINLOG的位置
Executed_Gtid_Set:記錄從機執行的MASTER和SLAVE BINLOG日志位置
注:
UUID:身份ID,第一次啟動MySQL時會產生UUID寫入到auto.cnf文件,不建議修改
例:
[mysql@localhost ~]$ cat /mysql/data/auto.cnf
[auto]
server-uuid=69a73914-62ca-11e3-870f-080027dff846
GTID:全局事務標識符,每次BINLOG寫入事務都會產生唯一一個標識符,由UUID+Transcation ID組成.
例:
mysql> show master status;
+------------------+----------+--------------------+------------------+-------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------------+------------------+-------------------------------------------+
| mysql-bin.000050 | 191 | test,percona,mysql | | 69a73914-62ca-11e3-870f-080027dff846:1-36 |
+------------------+----------+--------------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)