加载中...

Mysql延迟复制、半同步复制、过滤复制、GTID复制


一、延迟复制

主库操作

修改配置文件

[mysqld]
#开启binlog
log-bin=mysql-bin
binlog_format=row
#设置server_id
server_id=1

重启db

[root@db data]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!

数据库中创建复制用户

grant replication slave on *.* to rep@'%' indetified by '123456';

从库操作

修改配置文件

#设置server_id 需要不等于1
server_id=7

查看主库binlog

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从库change master

mysql> change master to master_host='192.168.0.49',master_user='rep',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=120,master_delay=3600;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.49
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006   #主库目前的binlog日志
          Read_Master_Log_Pos: 120              #主库目前的binlog日志pos点
               Relay_Log_File: db-relay-bin.000002                                
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000006
             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: 120
              Relay_Log_Space: 453
              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: 32ab32d8-414f-11ec-9cba-5254003a3847
             Master_Info_File: /data/3307/data/master.info
                    SQL_Delay: 3600                  #从库延迟3600s执行sql
          SQL_Remaining_Delay: NULL        #距离下次执行sql的时间
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)

二、半同步复制

主库操作

开启半同步配置

#查看是否支持动态开启
mysql> show global variables like 'have_dynamic_loading';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| have_dynamic_loading | YES   |
+----------------------+-------+
1 row in set (0.00 sec)

#安装插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)

#开启插件
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)

#设置默认的超时时间
mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
Query OK, 0 rows affected (0.00 sec)

#查看
mysql> show variables like'rpl%';
+------------------------------------+----------+
| Variable_name                      | Value    |
+------------------------------------+----------+
| rpl_semi_sync_master_enabled       | ON       |
| rpl_semi_sync_master_timeout       | 1000     |
| rpl_semi_sync_master_trace_level   | 32       |
| rpl_semi_sync_master_wait_no_slave | ON       |
| rpl_stop_slave_timeout             | 31536000 |
+------------------------------------+----------+
5 rows in set (0.00 sec)

mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| 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.00 sec)

永久开启

[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000

从库操作

开启配置

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
Query OK, 0 rows affected (0.00 sec)

#重启io线程(如果之前没有配置主从的话 还需要change master)
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.01 sec)

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

查看并验证

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     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

#主库添加数据测试同步情况
mysql> create database test3;
Query OK, 1 row affected (0.00 sec)

mysql> create database test4;
Query OK, 1 row affected (0.00 sec)

mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 295   |
| Rpl_semi_sync_master_net_wait_time         | 590   |
| Rpl_semi_sync_master_net_waits             | 2     |
| 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      | 416   |
| Rpl_semi_sync_master_tx_wait_time          | 833   |
| Rpl_semi_sync_master_tx_waits              | 2     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
#这里是2 证明刚才两条sql走了半同步复制
| Rpl_semi_sync_master_yes_tx                | 2     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

#关闭半同步在测试
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> create database test5;
Query OK, 1 row affected (0.00 sec)

mysql> create database test6;
Query OK, 1 row affected (0.00 sec)

mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 295   |
| Rpl_semi_sync_master_net_wait_time         | 590   |
| Rpl_semi_sync_master_net_waits             | 2     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 0     |
#OFF表示半同步关闭
| Rpl_semi_sync_master_status                | OFF   |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 416   |
| Rpl_semi_sync_master_tx_wait_time          | 833   |
| Rpl_semi_sync_master_tx_waits              | 2     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
#还是2证明刚才两条sql 没有走半同步
| Rpl_semi_sync_master_yes_tx                | 2     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

三、过滤复制

配置参数

可以选择在主库配置 也可以在从库

主库:

白名单:只记录白名单中列出的库的二进制日志

binlog-do-db

黑名单:不记录黑名单列出的库的二进制日志

binlog-ignore-db

从库:

白名单:只执行白名单中列出的库或者表的中继日志

--replicate-do-db=test
--replicate-do-table=test.t1
--replicate-wild-do-table=test.t2

黑名单:不执行黑名单中列出的库或者表的中继日志

--replicate-ignore-db
--replicate-ignore-table
--replicate-wild-ignore-table

这里测试从库配置

#配置文件添加
replicate-do-db=wangzherongyao

#重启从库生效
mysqladmin -uroot -p3308 -S /data/3308/data/mysql.sock shutdown
mysqld_safe --defaults-file=/data/3308/my.conf &

查看复制状态(一样需要之前配置主从)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.49
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 335
               Relay_Log_File: db-relay-bin.000010
                Relay_Log_Pos: 498
        Relay_Master_Log_File: mysql-bin.000009
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: wangzherongyao  #这里显示的就是复制白名单

测试

#主库创建数据
mysql> create database wangzherongyao;
Query OK, 1 row affected (0.00 sec)

mysql> create database lol;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| backup             |
| binlog             |
| blog               |
| ceshi              |
| chayi1             |
| chayi2             |
| huhuhahei          |
| huhuhahei1         |
| lol                |
| mysql              |
| oldboy             |
| performance_schema |
| slow_query_log     |
| test               |
| test1              |
| test2              |
| test3              |
| test4              |
| test5              |
| test6              |
| test_binlog        |
| wangzherongyao     |
| wordpress          |
| world              |
+--------------------+
25 rows in set (0.00 sec)

#从库查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| backup             |
| binlog             |
| blog               |
| ceshi              |
| chayi1             |
| chayi2             |
| huhuhahei          |
| huhuhahei1         |
| mysql              |
| oldboy             |
| performance_schema |
| slow_query_log     |
| test               |
| test1              |
| test2              |
| test3              |
| test4              |
| test5              |
| test6              |
| test_binlog        |
| wangzherongyao     |
| wordpress          |
| world              |
+--------------------+
24 rows in set (0.00 sec)

#可以发现从库是没有新创建的lol库的 只复制了wangzherongyao

四、GTID复制

配置文件需要添加参数

主库

#mysqld下添加 
[mysqld]
gtid_mode=ON
enforce_gtid_consistency
log-slave-updates
binlog_format=row
log-bin=mysql-bin
server_id=1

从库

[mysqld]
gtid_mode=ON
enforce_gtid_consistency
log-slave-updates
server_id=5
log-bin=mysql-bin
binlog_format=row

修改后都需要重启

/etc/init.d/mysqld restart

创建复制用户

grant replication slave on *.* to rep@'%' identified by '123';

从库change master

change master to master_host='10.60.222.142', master_user='rep',master_password='123',master_auto_position=1;

查看

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.60.222.142
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2009
               Relay_Log_File: 10-60-92-80-relay-bin.000002
                Relay_Log_Pos: 2219
        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: 2009
              Relay_Log_Space: 2429
              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: 4c8ebc2b-42d5-11ec-a6aa-52540094f3a0
             Master_Info_File: /application/mysql-5.6.40/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 4c8ebc2b-42d5-11ec-a6aa-52540094f3a0:1-5     #io线程收到的GTID
            Executed_Gtid_Set: 4c8ebc2b-42d5-11ec-a6aa-52540094f3a0:1-5     #sql线程执行的GTID
                Auto_Position: 1              #是否开启GTID
1 row in set (0.00 sec)

主库查看

mysql> select * from information_schema.processlist where Command like '%Binlog%';
+----+------+---------------------+------+------------------+------+------------------------------------------------------------------+------+
| ID | USER | HOST                | DB   | COMMAND          | TIME | STATE                                                            | INFO |
+----+------+---------------------+------+------------------+------+------------------------------------------------------------------+------+
|  7 | rep  | 10.60.157.113:24552 | NULL | Binlog Dump GTID | 1284 | Master has sent all binlog to slave; waiting for binlog to be up | NULL |
|  8 | rep  | 10.60.92.80:40700   | NULL | Binlog Dump GTID | 1262 | Master has sent all binlog to slave; waiting for binlog to be up | NULL |
+----+------+---------------------+------+------------------+------+------------------------------------------------------------------+------+
2 rows in set (0.01 sec)

文章作者: huhuhahei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 huhuhahei !
评论
  目录