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