半同步複製簡介mysql
何爲半同步複製模式呢?在此咱們先了解異步複製模式,這是MySQL的默認複製選項。異步複製便是master數據庫把binlog日誌發送給slave數據庫,而後就沒有了而後了。在此暴露一個問題,當slave服務器發生故障了,那麼確定會致使主從數據庫服務器的數據不一致。
爲了解決上面的問題,MySQL5.5引入一種叫作半同步複製模式。開啓這種模式,能夠保證slave數據庫接收完master數據庫發送過來的binlog日誌並寫入本身的中繼日誌中,而後反饋給master數據庫,告知已經複製完畢。
開啓這種模式後,當出現超時,主數據庫將會自動轉爲異步複製模式,直到至少有一臺從服務器接受到主數據庫的binlog,而且反饋給主數據庫。這時主數據庫纔會切換回半同步複製模式。sql
注意:
(1)半同步複製模式必須在主服務器和從服務器同時中開啓,不然將會默認爲異步複製模式;
且MySQL5.5或者以上版本纔有這個功能
(2)對於開啓半同步複製的集羣,當某一臺slave響應超時,會自動切換爲異步複製模式;半同步複製模式存在的好處是,集羣裏的slave不可能同時全跪了,這個機率過小;只要存在同步正常的,就保證了數據的安全性數據庫
環境:安全
CentOS系統
主庫:192.168.0.1
從庫:192.168.0.2
MySQL:5.6 服務器
開始配置半同步複製session
第一步:MySQL必須有自動加載功能,即have_dynamic_loading變量爲YES(由於咱們是在MySQL裏面來加載安裝這個功能插件)異步
mysql>>show variables like 'have_dynamic_loading'; #檢查是否具備自動加載功能 +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | have_dynamic_loading | YES | +----------------------+-------+ 1 row in set (0.00 sec) mysql>>show variables like 'plugin%'; +---------------+----------------------+ | Variable_name | Value | +---------------+----------------------+ | plugin_dir | /d/mysql/lib/plugin/ | +---------------+----------------------+ 1 row in set (0.00 sec) mysql>>system ls /d/mysql/lib/plugin/ mysql> system ls /d/mysql/lib/plugin/ adt_null.so auth_test_plugin.so debug libmemcached.so qa_auth_client.so semisync_master.so validate_password.so auth.so connection_control.so innodb_engine.so mypluglib.so qa_auth_interface.so semisync_slave.so auth_socket.so daemon_example.ini libdaemon_example.so mysql_no_login.so qa_auth_server.so test_udf_services.so
第二步:主庫(192.168.0.1)配置:socket
mysql>>install plugin rpl_semi_sync_master soname 'semisync_master.so'; #加載主庫半同步插件 mysql>>show plugins; #查看是否加載成功 +----------------------------+----------+--------------------+--------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+--------------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL | | mysql_old_password | ACTIVE | AUTHENTICATION | NULL | GPL | | sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL | | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL | | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL | | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL | | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_DATAFILES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL | | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | | rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL | +----------------------------+----------+--------------------+--------------------+---------+ mysql>>SET GLOBAL rpl_semi_sync_master_enabled = 1; #開啓半同步複製,默認是關閉的
第三步:從庫(192.168.0.2)配置:memcached
mysql>>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; #加載從庫半同步插件 mysql>>show plugins; #查看是否加載成功 +----------------------------+----------+--------------------+-------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+-------------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL | | mysql_old_password | ACTIVE | AUTHENTICATION | NULL | GPL | | sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL | | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL | | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL | | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL | | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_DATAFILES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL | | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | | rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL | +----------------------------+----------+--------------------+-------------------+---------+ 43 rows in set (0.03 sec) mysql>>SET GLOBAL rpl_semi_sync_slave_enabled = 1; #開啓半同步複製,默認是關閉的
第四步:重啓從服務器IO線程,手動將異步模式切換爲半同步模式插件
mysql>>STOP SLAVE IO_THREAD; mysql>>START SLAVE IO_THREAD; ##能夠把開啓半同步寫入my.cnf文件配置裏,以後啓動mysql服務後自動開啓 rpl_semi_sync_master_enabled=1 #主庫配置文件添加,表示之後啓動MySQL將會自動開啓半同步複製 rpl_semi_sync_slave_enabled=1 #從庫配置文件添加,表示之後啓動MySQL將會自動開啓半同步複製
###########################################
查看是否配置成功
(1)參數確認:
主庫執行命令show variables like '%semi%'; +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | rpl_semi_sync_master_enabled | ON | | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+-------+ 4 rows in set (0.00 sec) Rpl_semi_sync_master_enabled=ON #表示開啓半同步複製 Rpl_semi_sync_master_timeout=10000 #默認10000毫秒,即10秒超時,將切換爲異步複製 Rpl_semi_sync_master_wait_no_slave #表示是否容許master每一個事物都要等待slave接收確認,默認爲ON Rpl_semi_sync_master_trace_level=32 #表示用於開啓半同步複製時的調試級別,默認32 從庫執行命令show variables like '%semi%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | ON | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.00 sec) Rpl_semi_sync_slave_enabled=ON #表示在slave已經開始半同步複製模式 Rpl_semi_sync_slave_trace_level=32 #表示用於開啓半同步複製時的調試級別,默認32
(2)查看當前半同步狀態
主庫執行命令show status like '%semi%'; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 2 | | 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) Rpl_semi_sync_master_status #表示主服務器使用是異步仍是半同步複製 Rpl_semi_sync_master_clients #表示從服務器有多少個配置成半同步複製 Rpl_semi_sync_master_yes_tx #表示從服務器確認成功提交的數量 Rpl_semi_sync_master_no_tx #表示從服務器確認失敗提交的數量 從庫執行命令show status like '%semi%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | ON | +----------------------------+-------+ 1 row in set (0.00 sec) Rpl_semi_sync_slave_status #表示從服務器開啓半同步複製