mysql配置为半同步复制

mysql 半同步插件是由谷歌提供,具体位置/usr/local/mysql/lib/plugin/下,一个是 master
用的 semisync_master.so,一个是 slave 用的 semisync_slave.so,下面我们就来具体配置一下。
如果不清楚 Plugin 的目录,用如下查找:

mysql> show variables like '%plugin_dir%';
mysql配置为半同步复制

1、 分别在主从节点上安装相关的插件(master,Candicate master, slave)
MySQL 上安装插件需要数据库支持动态载入。检查是否支持,用如下检测:

mysql> show variables like '%have_dynamic_loading%';
mysql配置为半同步复制

 

所有 mysql 数据库服务器,安装半同步插件(semisync_master.so,semisync_slave.so)

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
mysql> install plugin rpl_semi_sync_master soname 'semisync_slave.so';

其他 mysql 主机采用同样的方法安装
检查 Plugin 是否已正确安装:
mysql> show plugins;

mysql配置为半同步复制

最下面两行,说明已经安装成功

mysql> select * from information_schema.plugins;
查看半同步相关信息

mysql> show variables like '%rpl_semi_sync%';         

mysql配置为半同步复制

上图可以看到半同复制插件已经安装,只是还没有启用,所以是 off

  2、 修改 my.cnf 文件,配置主从同步:
注: 若主 MYSQL 服务器已经存在,只是后期才搭建从 MYSQL 服务器,在置配数据同步前应
先将主 MYSQL 服务器的要同步的数据库拷贝到从 MYSQL 服务器上(如先在主 MYSQL 上备
份数据库,再用备份在从 MYSQL 服务器上恢复)
master mysql 主机:
server-id = 1
log-bin=mysql-bin
binlog_format=mixed
log-bin-index=mysql-bin.index
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=10000
rpl_semi_sync_slave_enabled=1
relay_log_purge=0
relay-log = relay-bin
relay-log-index = slave-relay-bin.index
注:
rpl_semi_sync_master_enabled=1 1 表是启用, 0 表示关闭
rpl_semi_sync_master_timeout=10000: 毫秒单位 , 该参数主服务器等待确认消息 10 秒后,
不再等待,变为异步方式。
Candicate master 主机:
server-id = 2
log-bin=mysql-bin
binlog_format=mixed
log-bin-index=mysql-bin.index
relay_log_purge=0
relay-log = relay-bin
relay-log-index = slave-relay-bin.index
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=10000
rpl_semi_sync_slave_enabled=1
注: relay_log_purge=0,禁止 SQL 线程在执行完一个 relay log 后自动将其删除, 对于 MHA
场景下,对于某些滞后从库的恢复依赖于其他从库的 relay log,因此采取禁用自动删除功能
Slave 主机:
Server-id = 3
log-bin = mysql-bin
relay-log = relay-bin
relay-log-index = slave-relay-bin.index
read_only = 1
rpl_semi_sync_slave_enabled=1
查看半同步相关信息
mysql>show variables like ‘%rpl_semi_sync%’;
查看半同步状态:
mysql>show status like ‘%rpl_semi_sync%’;
有几个状态参数值得关注的:
rpl_semi_sync_master_status :显示主服务是异步复制模式还是半同步复制模式
rpl_semi_sync_master_clients :显示有多少个从服务器配置为半同步复制模式
rpl_semi_sync_master_yes_tx :显示从服务器确认成功提交的数量
rpl_semi_sync_master_no_tx :显示从服务器确认不成功提交的数量
rpl_semi_sync_master_tx_avg_wait_time :事务因开启 semi_sync ,平均需要额外等待的时间
rpl_semi_sync_master_net_avg_wait_time :事务进入等待队列后,到网络平均等待时间
     

 mysql> grant replication slave on *.* to mharep@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (10.01 sec)

mysql> grant all privileges on *.* to manager@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 736
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)


第一条 grant 命令是创建一个用于主从复制的帐号,在 master candicate master 的主机上
创建即可。
第二条 grant 命令是创建 MHA 管理账号, 所有 mysql 服务器上都需要执行。 MHA 会在配置
文件里要求能远程登录到数据库,所以要进行必要的赋权。
Candicate master 主机:

 mysql> grant replication slave on *.* to mharep@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (10.01 sec)

mysql> grant all privileges on *.* to manager@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> change master to master_host='10.0.0.102',master_port=3306,master_user='mharep',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=736;
Query OK, 0 rows affected, 2 warnings (0.05 sec)

 mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

 

查看从的状态,以下两个值必须为 yes,代表从服务器能正常连接主服务器
Slave_IO_Running:Yes
Slave_SQL_Running:Yes


mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.102
                  Master_User: mharep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 736
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 736
              Relay_Log_Space: 521
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: cd0b608a-eec3-11e8-9e14-000c2908df09
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

Slave 主机:

mysql> grant all privileges on *.* to manager@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='10.0.0.102',master_port=3306,master_user='mharep',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=736;
Query OK, 0 rows affected, 2 warnings (0.04 sec)


mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

 

查看从的状态,以下两个值必须为 yes,代表从服务器能正常连接主服务器
Slave_IO_Running:Yes
Slave_SQL_Running:Yes


mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.102
                  Master_User: mharep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 736
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 736
              Relay_Log_Space: 521
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: cd0b608a-eec3-11e8-9e14-000c2908df09
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)


查看 master 服务器的半同步状态:
mysql>show status like ‘%rpl_semi_sync%’;

mysql配置为半同步复制

 



两个节点,说明配置成功

 

上一篇:《Adobe Flash CS5中文版经典教程》——导读


下一篇:【译】SQL Pivot介绍