MYSQL主主复制:互为主从
容易产生的问题:数据不一致
主机 | IP |
---|---|
Master1 | 192.168.37.7 |
Master2 | 192.168.37.17 |
Master1设置
[root@Centos7 ~]#vim /etc/my.cnf
[mysqld]
log_bin
server_id=7
auto_increment_offset=1
auto_increment_increment=2
创建复制帐号
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.37.%' identified by 'centos';
Master2设置
[root@Centos7 ~]#vim /etc/my.cnf
[mysqld]
log_bin
server_id=17
auto_increment_offset=2
auto_increment_increment=2
同步信息master2同步master1
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.37.7',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='centos',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mariadb-bin.000001',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)
Master1同步Master2
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.37.17',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='centos',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mariadb-bin.000001',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)
主主复制完成
半同步复制
半同步是当若干从服务器中有一台与主服务器同步成功,就认为同步成功。返回结果给客户机
半同步需要安装独立的插件来实现
查看已安装插件show plugins;
semisync_master.so 主服务器插件
semisync_slave.so 从服务器插件
条件:设置为主从复制
Master
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; #安装插件
Query OK, 0 rows affected (0.01 sec)
开启master半同步复制
[root@Centos7 ~]#vim /etc/my.cnf
rpl_semi_sync_master_enabled #添加服务器选项
[root@Centos7 ~]#systemctl restart mariadb #重启服务生效
slave
MariaDB [hellodb]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; #安装插件
Query OK, 0 rows affected (0.02 sec)
###- 开启slave半同步复制
[root@Centos7 ~]#vim /etc/my.cnf
rpl_semi_sync_slave_enabled #添加服务器选项
[root@Centos7 ~]#systemctl restart mariadb #重启服务生效
半同步设置完成
MariaDB [(none)]> show global status like '%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 |
+--------------------------------------------+-------+
14 rows in set (0.44 sec)
MariaDB [(none)]> show global variables like '%semi%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON | #已开启半同步
| rpl_semi_sync_master_timeout | 10000 | #超时时长10000ms
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
4 rows in set (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> show global status like '%semi%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.02 sec)
MariaDB [(none)]> show global variables like '%semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.01 sec)