MySQL的半同步复制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL的半同步复制相关的知识,希望对你有一定的参考价值。
MySQL的半同步复制
半同步复制:
什么是半同步复制?我们知道在默认情况下,mysql的复制是异步的,这意味着主服务器及其从服务器是独立的。异步复制可以提供最佳的性能,因为主服务器在将更新的数据写入它的二进制日志(Binlog)文件中后,无需等待验证更新数据是否已经复制到从服务器中,就可以自由处理其它进入的事务处理请求。但这也同时带来了很高的风险,如果在主服务器或从服务器端发生故障,会造成主从数据的不一致,甚至在恢复时造成数据丢失。
半同步复制是从MySQL5.5开始引入了一种半同步复制功能,该功能可以确保主服务器和访问链中至少一台从服务器之间的数据一致性和冗余。在这种配置结构中,一台主服务器和其许多从服务器都进行了配置,这样在复制拓扑中,至少有一台从服务器在父主服务器进行事务处理前,必须确认更新已经收到并写入了其中继日志(Relay Log)。当出现超时,源主服务器必须暂时切换到异步复制模式重新复制,直到至少有一台设置为半同步复制模式的从服务器及时收到信息。
下面我们看一下怎么从普通复制转化为半同步复制
1、主节点上的配置文件中定义如下
[server] skip_name_resolve = on 为了方便跳过地址解析 #innodb_file_per_table = on #max_connections = 20000 #上述这些配置可以不用一定配置,但是建议可以写上 log_bin = master-log 最主要的两项配置 server_id = 1 read_only=ON 默认只读是打开的,可以不用设置
2、从节点上的配置
[server] skip_name_resolve = on innodb_file_per_table = on max_connections = 20000 relay_log = relay-log 启动中继日志 server_id = 2 这里的节点就不能为1,这是从节点
3、主节点上授权一个用户可以做复制操作
MariaDB [(none)]> grant replication client,replication slave on *.* to ‘repluser‘@‘172.18.77.%‘ identified by ‘centos‘; MariaDB [(none)]> flush privileges; MariaDB [(none)]> show master status; MariaDB [(none)]> show master status\G *************************** 1. row *************************** File: master-log.000004 Position: 245 Binlog_Do_DB: Binlog_Ignore_DB:
4、然后可以让从服务器来复制了
设置从主服务器的哪个文件的哪个位置开始启动复制,要设置从服务器指的主服务器要使用下述的命令启动复制
MariaDB [(none)]> change master to master_host=‘172.18.77.7‘,master_user=‘repluser‘,master_password=‘centos‘,master_log_file=‘master-log.000004‘,master_log_pos=245; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.18.77.7 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000004 Read_Master_Log_Pos: 245 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 530 Relay_Master_Log_File: master-log.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes ........... MariaDB [(none)]> select user from mysql.user; +------------+ | user | +------------+ | root | | repluser | | root | | test | | root | | | | root | | | | root | +------------+
半同步复制:
支持多种插件:/usr/lib64/mysql/plugins/
需要安装方可使用:
mysql> INSTALL PLUGIN plugin_name SONAME ‘shared_library_name‘;
半同步复制:
semisync_master.so
semisync_slave.so
所以要安装/usr/lib64/mysql/plugins/目录下的两个插件才能半同步复制
配置如下:
5、在主节点上安装插件
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘; MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘rpl_semi%‘; +------------------------------------+----------+ | Variable_name | Value | +------------------------------------+----------+ | rpl_semi_sync_master_enabled | OFF | ---------> off | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+----------+ MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=ON; 开启半同步复制 MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%rpl%‘; +------------------------------------+---------+ | Variable_name | Value | +------------------------------------+---------+ | rpl_recovery_rank | 0 | | rpl_semi_sync_master_enabled | ON | ---------> 由上述的off转为on | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+---------+
6、在从节点上安装插件
MariaDB [(none)]> install plugin rpl_semi_sync_slave soname ‘semisync_slave‘; MariaDB [(none)]> show global variables like ‘rpl_semi%‘; +---------------------------------+---------+ | Variable_name | Value | +---------------------------------+---------+ | rpl_semi_sync_slave_enabled | OFF | ---------> off | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+---------+ MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on; MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%rpl%‘; +---------------------------------+----------+ | Variable_name | Value | +---------------------------------+----------+ | rpl_recovery_rank | 0 | | rpl_semi_sync_slave_enabled | ON | ---------> 由上述的off转为on | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-----------+ MariaDB [(none)]> SHOW GLOBAL status LIKE ‘%rpl%‘; #查看全局的状态,但是Rpl_semi_sync_slave_status状态为什么还是off的呢? +----------------------------+--------------+ | Variable_name | Value | +----------------------------+--------------+ | Rpl_semi_sync_slave_status | OFF | | Rpl_status | AUTH_MASTER | +----------------------------+-------------+
7、 在主节点上查看其状态
MariaDB [(none)]> SHOW GLOBAL status LIKE ‘%rpl%‘; 但是查看主节点的状态是为on +--------------------------------------------+-------------------+ | Variable_name | Value | +--------------------------------------------------+-------------+ | Rpl_semi_sync_master_clients | 0 | ------------>这里状态为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 | | Rpl_status | AUTH_MASTER | +--------------------------------------------+------------------+
这是由于主节点的状态是on的 但是从节点状态是off的 ,说明从节点的没有以同步复制的或者半同步复制的状态连接上来
如何解决上述问题,需要重启复制线程识别新的设定
先停止io_thread在启动io_thread
8、 在从从节点上停止和启动io_thread
MariaDB [(none)]> stop slave io_thread; MariaDB [(none)]> start slave io_thread;
9、在次查看主节点状态
MariaDB [(none)]> SHOW GLOBAL status LIKE ‘%rpl%‘; +--------------------------------------------+------------------+ | Variable_name | Value | +--------------------------------------------+------------------+ | Rpl_semi_sync_master_clients | 1 | ----------->由上述的0转变为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 | | Rpl_status | AUTH_MASTER | +--------------------------------------------+------------------+
由此可说明就以半同步复制连接上来了。
最后测试: 在主节点上
MariaDB [(none)]> create database wwwdb; MariaDB [(none)]> SHOW GLOBAL status LIKE ‘%rpl%‘; +--------------------------------------------+------------------+ | Variable_name | Value | +--------------------------------------------+------------------+ | Rpl_semi_sync_master_clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 1019 | ---------> | Rpl_semi_sync_master_net_wait_time | 1019 | ---------> | Rpl_semi_sync_master_net_waits | 1 | ---------> | Rpl_semi_sync_master_no_times | 1 | | Rpl_semi_sync_master_no_tx | 1 | | Rpl_semi_sync_master_status | ON | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 2089938 | | Rpl_semi_sync_master_tx_wait_time | 4179876 | | Rpl_semi_sync_master_tx_waits | 2 | | Rpl_semi_sync_master_wait_pos_backtraverse| 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 2 | | Rpl_status | AUTH_MASTER | +--------------------------------------------+------------------+
以上是关于MySQL的半同步复制的主要内容,如果未能解决你的问题,请参考以下文章