搭建mysql半同步复制(Semisynchronous Replication)

1.规划网络和主从机器
master:10.10.54.64
slave:10.10.54.67
slave:10.10.54.63
2.在Master与Slave服务器分别安装半同步插件
#安装Master半同步插件
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
#安装Slave半同步插件
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';

2.开启Master和slave的半同步功能
master config:

[root@gyf  ~]# vim /etc/my.cnf
log-bin=master-bin
server-id=1
binlog_formate=mixed
 #开启Master半同步功能
rpl_semi_sync_master_enabled = 1    
rpl_semi_sync_master_timeout = 1000 (1秒)

[root@gyf  ~]# /etc/init.d/mysqld restart

slave config:

log-bin=slave-bin
binlog_formate=mixed
server-id=10
(server-id=11)
必须大于主

#开启Slave半同步功能
rpl_semi_sync_slave_enabled = 1      

[root@gyf  ~]# /etc/init.d/mysqld restart
3.在master上面创建一个复制用户并授予权限
mysql> grant replication slave on *.* to 'gyf'@'10.10.54.67' identified by 'aaa12345';
mysql> grant replication slave on *.* to 'gyf'@'10.10.54.63' identified by 'aaa12345';
mysql> flush privileges;
//在从上测试是否能用复制用户登录
[root@gyf  ~]# mysql -ugyf -paaa12345 -h10.10.54.64
4.查看master上二进制日志和position位置
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      107 |              |                  |
+-------------------+----------+--------------+------------------+

CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=107;

拓展:重置master  reset master
5.备份master上的数据,把备份maste数据库还原到从库上
[root@gyf  ~]# mysqldump -uroot -paaa12345 --databases employees |mysql -uroot -paaa12345 -h10.10.54.67
6.在slave上面change master操作
mysql> change master to master_host='10.10.54.64',master_user='gyf',
    master_password='aaa12345',
   master_log_file='master-bin.000003',master_log_pos=107;
7.在slave上启动slave
mysql> start slave;
8.查看slave状态
确定slave上的I/O线程和SQL线程状态为YES
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.54.64
                  Master_User: gyf
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 107
               Relay_Log_File: gyf-relay-bin.000008
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes


//可以单独停掉某一个线程
mysql> STOP SLAVE IO_THREAD;
mysql> STOP SLAVE SQL_THREAD;
7.查看半同步开启状态
######在Master服务器上查看
mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 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     |
+--------------------------------------------+-------+
mysql> show global variables like '%rpl%';
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_recovery_rank                  | 0     |
| rpl_semi_sync_master_enabled       | ON    | #Master半同步已经开启
| rpl_semi_sync_master_timeout       | 1000  | #超时时间
| rpl_semi_sync_master_trace_level   | 32    |
| rpl_semi_sync_master_wait_no_slave | ON    |
+------------------------------------+-------+
######在Slave服务器上查看
mysql> show global status like 'rpl_semi%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |         #已经为开启状态
+----------------------------+-------+
mysql> show global variables like '%rpl%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_recovery_rank               | 0     |
| rpl_semi_sync_slave_enabled     | ON    |    #Slave半同步已经开启
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+

你可能感兴趣的:(mysql半同步复制)