半同步复制模型:顾名思义就是复制一部分,跟主库的数据有可能不一样。再完整的主从机制,从库有时也会落后于主库。
如何实现半同步复制呢?
半同步复制步骤:
(1)在半同步之前设置一主一从,也可以一主多从。
(2)主从复制之后,主从分别安装所需插件。通过rpm -ql mariadb-server查询所需插件。
(3)安装插件之前,关闭从数据库同步功能。
配置过程:两台主机
主库 | 从库 |
---|---|
10.5.100.207 | 10.5.100.208 |
NODE1节点
[root@node1 ~]# vim /etc/my.cnf
server-id=1
skip_name_resolve=ON
innodb_file_per_table=ON
log-bin=mysql-bin
[root@node1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.41-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.5.100.208' identified by 'replpass';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 498 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
NODE2从节点;
[root@node2 mysql]# vim /etc/my.cnf
server-id=1
skip_name_resolve=ON
innodb_file_per_table=ON
log-bin=mysql-bin
[root@node2 mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to
->
-> master_host='10.5.100.207',
-> master_user='repluser',
-> master_password='replpass',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=498;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.5.100.207
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 498
Relay_Log_File: mysql-log.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
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: 498
Relay_Log_Space: 817
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
1 row in set (0.00 sec)
NODE2节点
安装所需插件,关闭从数据库同步功能。
MariaDB [(none)]> stop slave; Query OK, 0 rows affected (0.00 sec)
[root@node1 ~]# rpm -ql mariadb-server #查看主从库所需插件
MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.02 sec)
安装从库插件
MariaDB [(none)]> show global variables like '%semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | OFF | 从库半同步功能没有开启
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=ON; 开启半同步复制功能
Query OK, 0 rows affected (0.00 sec)
NODE1节点;
MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.03 sec)
安装主库所需插件
MariaDB [(none)]> show global variables like '%semi%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | OFF | 主从半同步功能未开启。
| rpl_semi_sync_master_timeout | 10000 | 从库同步的超时时长
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON | 主库是否等待从库同步完成
+------------------------------------+-------+
4 rows in set (0.00 sec)
MariaDB [(none)]> set global rpl_semi_sync_master_enabled=ON; 开启主库半同步功能。
Query OK, 0 rows affected (0.00 sec)
开启从库同步功能,在NODE1中写入数据,验证从库同步情况
MariaDB [(none)]> create database mohan;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show global status like '%semi%';
+--------------------------------------------+---------+
| Variable_name | Value |
+--------------------------------------------+---------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 743 | 表示主库平均等待时长
| Rpl_semi_sync_master_net_wait_time | 2231 |
| Rpl_semi_sync_master_net_waits | 3 |
| 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 | 2010996 |
| Rpl_semi_sync_master_tx_wait_time | 6032989 |
| Rpl_semi_sync_master_tx_waits | 3 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 3 |
+--------------------------------------------+---------+
14 rows in set (0.00 sec)
如何定制复制过滤器:让从节点复制指定的数据库,或指定数据库的指定表
讲述如何让从节点复制指定的数据库。
NODE1主节点;
[root@node1 ~]# vim /etc/my.cnf
server-id=1
skip_name_resolve=ON
innodb_file_per_table=ON
log-bin=mysql-bin
binlog_do_db=yan 这是指定从数据库复制的白名单
binlog_ignore_db=shuo 这是指定从数据库复制的黑名单。
[root@node1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.41-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mohan |
| mysql |
| performance_schema |
| test |
| yan |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 245 | yan | shuo |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> create database shuo;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 245 | yan | shuo |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
验证从节点的复制情况;
MariaDB [mohan]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mohan |
| mysql |
| performance_schema |
| test |
| yan | 这里可以看出从数据库只复制了yan,没有复制shuo这个数据库。
+--------------------+
6 rows in set (0.00 sec)
总结一点:半同步复制是说当客户向主库写入数据时是否,主库是否等待从库复制完成后,在接受新的数据