MySQL 5.6.x半同步複製配置
本文環境
主庫:CentOS6.5 x64 192.168.0.65 mysql-5.6.29
備庫:CentOS6.5 x64 192.168.0.66 mysql-5.6.29
接上文: MySQL 5.6主從同步配置案例
半同步複製的概念:
mysql5.5.x以上版本支援半同步複製,當Slave主機串連到Master時,能夠查看其是否處於半同步複製的機制。當Master上開啟半同步複製的功能時,至少應該有一個Slave開啟其功能。此時,一個線程在Master上提交事務將受到阻塞,直到得知一個已開啟半同步複製功能的Slave已收到此事務的所有事件,或等待逾時。當一個事務的事件都已寫入其relay-log中且已重新整理到磁碟上,Slave才會告知已收到。如果等待逾時,也就是Master沒被告知已收到,此時Master會自動轉換為非同步複製的機制。當至少一個半同步的Slave趕上了,Master與其Slave自動轉換為半同步複製的機制。半同步複製的功能要在Master,Slave都開啟,半同步複製才會起作用;否則,只開啟一邊,它依然為非同步複製。
一、半同步主伺服器配置1. mysql主伺服器配置(1) 在主伺服器上加入半自動設定檔案參數
# vi /etc/my.cnf
[mysqld]
log-bin = master-bin
log-bin-index = master-bin.index
binlog_format = mixed
server-id = 1
expire-logs-days = 7
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000 # 1 second
(2) 安裝半同步配置外掛程式
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
(3) 配置半同步外掛程式
mysql> show global variables like '%rpl%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
(4) 啟用半同步外掛程式
mysql> set global rpl_semi_sync_master_enabled =1;
(5) 配置逾時
mysql> set rpl_semi_sync_master_timeout 1000;
mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 0 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
其它一些效能及相關參數:
2. 重啟主伺服器,使設定檔生效
# service mysqld restart
二、半同步從伺服器配置1. mysql從伺服器配置(1) 加入如下半同步配置。
# vi /etc/my.cnf
[mysqld]
log-bin = mysql-bin
binlog_format = mixed
server-id = 11
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
replicate_wild_ignore_table = mysql.%
rpl_semi_sync_slave_enabled = 1
(2) 安裝半同步配置外掛程式
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)
(3) 配置半同步外掛程式
mysql> show global variables like '%rpl%';
+---------------------------------+----------+
| Variable_name | Value |
+---------------------------------+----------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
| rpl_stop_slave_timeout | 31536000 |
+---------------------------------+----------+
3 rows in set (0.00 sec)
(4) 啟用半同步外掛程式
mysql> set global rpl_semi_sync_master_enabled =1;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like '%rpl%';
+---------------------------------+----------+
| Variable_name | Value |
+---------------------------------+----------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
| rpl_stop_slave_timeout | 31536000 |
+---------------------------------+----------+
3 rows in set (0.00 sec)
(5) 重啟同步進程
mysql> stop slave;
Query OK, 0 rows affected (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.65
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000005
Read_Master_Log_Pos: 120
Relay_Log_File: testdb-relay-bin.000012
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000005
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: mysql.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 622
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: c8bb22a1-024e-11e6-a1e8-000c29225fa0
Master_Info_File: /usr/local/mysql-5.6.29-linux-glibc2.5-x86_64/data/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:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
2. 在主伺服器上可以查看有1個從伺服器串連過來
mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
3. 重啟從伺服器,使設定檔生效
# service mysqld restart
實現兩個MySQL資料庫之間的主從同步
Linux環境中MySQL主從同步--添加新的從庫
通過 XtraBackup 實現不停機不鎖表搭建MySQL主從同步
MySQL主從同步配置記錄
Linux下MySQL資料庫主從同步配置
本文永久更新連結地址: