您好,登錄后才能下訂單哦!
===
異步復制(Asynchronous replication)
MySQL默認的復制即是異步的,主庫在執行完客戶端提交的事務后會立即將結果返給給客戶端,并不關心從庫是否已經接收并處理,這樣就會有一個問題,主如果crash掉了,此時主上已經提交的事務可能并沒有傳到從上,如果此時,強行將從提升為主,可能導致新主上的數據不完整。
全同步復制(Fully synchronous replication)
指當主庫執行完一個事務,所有的從庫都執行了該事務才返回給客戶端。因為需要等待所有從庫執行完該事務才能返回,所以全同步復制的性能必然會收到嚴重的影響。
半同步復制(Semisynchronous replication)
介于異步復制和全同步復制之間,主庫在執行完客戶端提交的事務后不是立刻返回給客戶端,而是等待至少一個從庫接收到并寫到relay log中才返回給客戶端。相對于異步復制,半同步復制提高了數據的安全性,同時它也造成了一定程度的延遲,這個延遲最少是一個TCP/IP往返的時間。所以,半同步復制最好在低延時的網絡中使用。
引用: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.
====
半同步復制會設置一個超時的時間,如果超過這個時間從服務器沒有給主服務器確認信息,主服務器會啟用異步復制,當檢測到從服務器又好了,主服務器又會采用半同步復制。
配置半同步復制
mysql> show variables like "have_dynamic_loading";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.02 sec)
mysql>
要想使用半同步復制,必須滿足以下幾個條件:
1. MySQL 5.5及以上版本
2. 變量have_dynamic_loading為YES
3. 異步復制已經存在
首先加載插件
因用戶需執行INSTALL PLUGIN, SET GLOBAL, STOP SLAVE和START SLAVE操作,所以用戶需有SUPER權限。
主:
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 |
====
啟動半同步復制
在安裝完插件后,半同步復制默認是關閉的,這時需設置參數來開啟半同步
主:
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
從:
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
以上的啟動方式是在命令行操作,也可寫在配置文件中。
主:
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
查看半同步是否在運行
主:
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)
這兩個變量常用來監控主從是否運行在半同步復制模式下。
至此,MySQL半同步復制搭建完畢~
====
修改完配置文件后,直接加入加載插件和啟用半同步復制,重啟刷新mysqld服務后,導致半同步復制沒有起來
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>
導致的原因是:主服務器上的二進制日志的文件和位置和從服務器的relay-log.info里的文件名和位置不一致導致
主服務器上的信息
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)
從服務器上的信息
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 啟動不了
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' 錯誤提醒
[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; 查看主服務器上的日志文件和位置
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| MySQL-master-bin.000002 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
從服務器里設置日志文件名和位置
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了
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。