===
成都創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供邯鄲網(wǎng)站建設(shè)、邯鄲做網(wǎng)站、邯鄲網(wǎng)站設(shè)計(jì)、邯鄲網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、邯鄲企業(yè)網(wǎng)站模板建站服務(wù),10年邯鄲做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
異步復(fù)制(Asynchronous replication)
MySQL默認(rèn)的復(fù)制即是異步的,主庫(kù)在執(zhí)行完客戶(hù)端提交的事務(wù)后會(huì)立即將結(jié)果返給給客戶(hù)端,并不關(guān)心從庫(kù)是否已經(jīng)接收并處理,這樣就會(huì)有一個(gè)問(wèn)題,主如果crash掉了,此時(shí)主上已經(jīng)提交的事務(wù)可能并沒(méi)有傳到從上,如果此時(shí),強(qiáng)行將從提升為主,可能導(dǎo)致新主上的數(shù)據(jù)不完整。
全同步復(fù)制(Fully synchronous replication)
指當(dāng)主庫(kù)執(zhí)行完一個(gè)事務(wù),所有的從庫(kù)都執(zhí)行了該事務(wù)才返回給客戶(hù)端。因?yàn)樾枰却袕膸?kù)執(zhí)行完該事務(wù)才能返回,所以全同步復(fù)制的性能必然會(huì)收到嚴(yán)重的影響。
半同步復(fù)制(Semisynchronous replication)
介于異步復(fù)制和全同步復(fù)制之間,主庫(kù)在執(zhí)行完客戶(hù)端提交的事務(wù)后不是立刻返回給客戶(hù)端,而是等待至少一個(gè)從庫(kù)接收到并寫(xiě)到relay log中才返回給客戶(hù)端。相對(duì)于異步復(fù)制,半同步復(fù)制提高了數(shù)據(jù)的安全性,同時(shí)它也造成了一定程度的延遲,這個(gè)延遲最少是一個(gè)TCP/IP往返的時(shí)間。所以,半同步復(fù)制最好在低延時(shí)的網(wǎng)絡(luò)中使用。
引用:http://www.cnblogs.com/ivictor/p/5735580.html
AFTER_SYNC (the default): The master writes each transaction to its binary log and the slave, and syncs the binary log to disk. The master waits for slave acknowledgment of transaction receipt after the sync. Upon receiving acknowledgment, the master commits the transaction to the storage engine and returns a result to the client, which then can proceed.
AFTER_COMMIT: The master writes each transaction to its binary log and the slave, syncs the binary log, and commits the transaction to the storage engine. The master waits for slave acknowledgment of transaction receipt after the commit. Upon receiving acknowledgment, the master returns a result to the client, which then can proceed.
====
半同步復(fù)制會(huì)設(shè)置一個(gè)超時(shí)的時(shí)間,如果超過(guò)這個(gè)時(shí)間從服務(wù)器沒(méi)有給主服務(wù)器確認(rèn)信息,主服務(wù)器會(huì)啟用異步復(fù)制,當(dāng)檢測(cè)到從服務(wù)器又好了,主服務(wù)器又會(huì)采用半同步復(fù)制。
配置半同步復(fù)制
mysql> show variables like "have_dynamic_loading";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.02 sec)
mysql>
要想使用半同步復(fù)制,必須滿足以下幾個(gè)條件:
1. MySQL 5.5及以上版本
2. 變量have_dynamic_loading為YES
3. 異步復(fù)制已經(jīng)存在
首先加載插件
因用戶(hù)需執(zhí)行INSTALL PLUGIN, SET GLOBAL, STOP SLAVE和START SLAVE操作,所以用戶(hù)需有SUPER權(quán)限。
主:
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
從:
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
然后在主和從上查看插件是否啟用
mysql>show plugins;
| rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
====
啟動(dòng)半同步復(fù)制
在安裝完插件后,半同步復(fù)制默認(rèn)是關(guān)閉的,這時(shí)需設(shè)置參數(shù)來(lái)開(kāi)啟半同步
主:
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
從:
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
以上的啟動(dòng)方式是在命令行操作,也可寫(xiě)在配置文件中。
主:
plugin-load=rpl_semi_sync_master=semisync_master.so
rpl_semi_sync_master_enabled=1
從:
plugin-load=rpl_semi_sync_slave=semisync_slave.so
rpl_semi_sync_slave_enabled=1
查看半同步是否在運(yùn)行
主:
mysql> show status like 'Rpl_semi_sync_master_status';
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Rpl_semi_sync_master_status | ON |
+-----------------------------+-------+
1 row in set (0.00 sec)
從:
mysql> show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.20 sec)
這兩個(gè)變量常用來(lái)監(jiān)控主從是否運(yùn)行在半同步復(fù)制模式下。
至此,MySQL半同步復(fù)制搭建完畢~
====
修改完配置文件后,直接加入加載插件和啟用半同步復(fù)制,重啟刷新mysqld服務(wù)后,導(dǎo)致半同步復(fù)制沒(méi)有起來(lái)
mysql> show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF |
+----------------------------+-------+
1 row in set (0.02 sec)
mysql>
導(dǎo)致的原因是:主服務(wù)器上的二進(jìn)制日志的文件和位置和從服務(wù)器的relay-log.info里的文件名和位置不一致導(dǎo)致
主服務(wù)器上的信息
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| MySQL-master-bin.000002 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
從服務(wù)器上的信息
mysql> show slave status\G;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 5
Current database: *** NONE ***
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.137
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: Mysql-master-bin.000001
Read_Master_Log_Pos: 1508
Relay_Log_File: mysql-slave-relay-bin.000010
Relay_Log_Pos: 4
Relay_Master_Log_File: Mysql-master-bin.000001
Slave_IO_Running: No 啟動(dòng)不了
Slave_SQL_Running: Yes
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file' 錯(cuò)誤提醒
[root@mysql-slave mysql]# cat relay-log.info
7
./mysql-slave-relay-bin.000010
4
Mysql-master-bin.000001 ---》文件和位置不一致
1508
0
0
1
[root@mysql-slave mysql]#
解決方法:
mysql> show master status; 查看主服務(wù)器上的日志文件和位置
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| MySQL-master-bin.000002 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
從服務(wù)器里設(shè)置日志文件名和位置
mysql> change master to master_log_file='MySQL-master-bin.000002',master_log_pos=154;
mysql> show status like 'Rpl_semi_sync_slave_status'; 查看
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.02 sec)
mysql>
最后ok了