mysql5.7 开启增强半同步复制

前提是主从异步复制环境要提前搭建好,然后再开启mysql增强半同步

环境:mysql5.7.26 主从异步复制早已部署好。

1.加载plugin插件

建议master和slave上全部执行(考虑到MHA的主从自动切换的环境)

在主库安装semisync_master.so和semisync_slave.so插件:

 

linux服务器上的master库执行:
root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (4.52 sec)
root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.07 sec)

 

linux服务器上的slave库执行:

root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (4.52 sec)
root@localhost [(none)]>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.07 sec)

 

查看时候已经安装了插件

root@localhost 07:52:  [fengjian]> show plugins;
+----------------------------+----------+--------------------+--------------------+---------+
| Name                       | Status   | Type               | Library            | License |
+----------------------------+----------+--------------------+--------------------+---------+
| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL               | GPL     |
| sha256_password            | ACTIVE   | AUTHENTICATION     | NULL               | GPL     |
| MyISAM                     | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| CSV                        | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| PERFORMANCE_SCHEMA         | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| MEMORY                     | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| MRG_MYISAM                 | 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_TEMP_TABLE_INFO     | 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     |
| INNODB_SYS_VIRTUAL         | ACTIVE   | INFORMATION SCHEMA | NULL               | GPL     |
| ARCHIVE                    | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| FEDERATED                  | DISABLED | STORAGE ENGINE     | NULL               | GPL     |
| BLACKHOLE                  | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| partition                  | ACTIVE   | STORAGE ENGINE     | NULL               | GPL     |
| ngram                      | ACTIVE   | FTPARSER           | NULL               | GPL     |
| rpl_semi_sync_master       | ACTIVE   | REPLICATION        | semisync_master.so | GPL     |
| rpl_semi_sync_slave        | ACTIVE   | REPLICATION        | semisync_slave.so  | GPL     |
+----------------------------+----------+--------------------+--------------------+---------+
46 rows in set (0.11 sec)

 

2.slave和master开启增强半同步参数

生产环境上建议先在slave库上的开启 增强半同步参数

 

set global rpl_semi_sync_slave_enabled =1;
stop  slave io_thread;start  slave io_thread;

 

此刻观察master的错误日志:提示开启了Semi-sync replication 复制

2020-02-24T07:39:39.445151-00:00 584 [Note] Start binlog_dump to master_thread_id(584) slave_server(255506), pos(, 4)
2020-02-24T07:44:44.300106-00:00 586 [Note] While initializing dump thread for slave with UUID <22da22b2-56af-11ea-9b23-0242ac120c03>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(584).
2020-02-24T07:44:44.300345-00:00 586 [Note] Start binlog_dump to master_thread_id(586) slave_server(255506), pos(, 4)
2020-02-24T07:44:44.300371-00:00 586 [Note] Start semi-sync binlog_dump to slave (server_id: 255506), pos(, 4)
2020-02-24T07:44:44.300809-00:00 584 [Note] Stop asynchronous binlog_dump to slave (server_id: 255506)
2020-02-24T07:46:09.373869-00:00 585 [Note] Semi-sync replication initialized for transactions.
2020-02-24T07:46:09.373908-00:00 585 [Note] Semi-sync replication enabled on the master.
2020-02-24T07:46:09.374779-00:00 0 [Note] Starting ack receiver thread

 

然后master上再开启增强半同步参数

set global rpl_semi_sync_master_enabled =1
set global rpl_semi_sync_master_timeout        =1000

#增强半同步的个数
rpl_semi_sync_master_wait_for_slave_count = 1  

 

3.MySQL官网配置方法

rpl_semi_sync_master_enabled        =1                             #    0
rpl_semi_sync_slave_enabled         =1                             #    0
rpl_semi_sync_master_timeout        =1000                          #    1000(1 second) 同步复制中由于网络原因导致复制时间超过1s后,增强半同步复制就变成了异步复制了
rpl_semi_sync_master_wait_for_slave_count=N
plugin_load_add                     =semisync_master.so            #
plugin_load_add                     =semisync_slave.so       

 

 

重启master和slave数据库。

4.实际生产环境配置方法

当然实际操作中还是不建议把参数rpl_semi_sync_master_enabled 和rpl_semi_sync_slave_enabled 直接写入配置文件的。
原因:把参数加入my.cnf配置文件时,在slave库挂掉,重新开启slave库或者是slave库重启后,会自动开启增强半同步复制。
如果在slave库断开master库时间较长时,最好是先开启mysql异步复制,让slave库追赶上master库后,然后再开启增强半同步复制。这样不会拖垮master库。要是直接开启增强半同步复制是会拖垮主库的。
rpl_semi_sync_master_timeout =1000 ##1s 这个转化为异步复制的超时参数是可以写入配置文件的。

 

5.案例

当线上运行的MySQL增强半同步复制架构中,当其中的一台slave库挂掉了应该如何正确重新添加到原先的增强半同步复制架构??

正确的做法如下:

1. 此2个参数rpl_semi_sync_master_enabled  和rpl_semi_sync_slave_enabled  不要直接写入到my.cnf配置文件开启。
2.在slave库上先 stop slave io_thread ;set global  rpl_semi_sync_slave_enabled=0 关闭此参数。然后start slave io_thread 或者start slave 开启异步复制,让slave库追赶上master库。
3.然后在slave库 set global  rpl_semi_sync_slave_enabled=1 ;stop  slave io_thread;start  slave  io_thread;

 

 

转载于:https://blog.51cto.com/wujianwei/2409754

 

posted @ 2020-02-24 16:12  fengjian1585  阅读(947)  评论(0编辑  收藏  举报