MariaDB 10.3.8 延迟复制

延迟复制的适用场景:

在大多数情况下默认的主从复制 延迟为零,但是有些情况需要从库的数据比主库的数据延迟。

1.主库误删除数据,可以延迟几个小时或者更久在从库查看到。

2.可以作为延迟测试。

3.可以查询历史数据,对准实时同步要求不高的情景。

MariaDB 10.2.3开始提供延迟复制的功能支持。

在从库设置:

查看正常的主动同步:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 172.16.1.92
                   Master_User: repl
                   Master_Port: 4000
                 Connect_Retry: 60
               Master_Log_File: node4_bin.000001
           Read_Master_Log_Pos: 1534
                Relay_Log_File: node5_relay.000002
                 Relay_Log_Pos: 1761
         Relay_Master_Log_File: node4_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: 1534
               Relay_Log_Space: 2066
               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: 40
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     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
              Slave_DDL_Groups: 6
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 1
1 row in set (0.001 sec)
可以看到SQL_Delay的值为0.

在从库进行设置:
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.192 sec)

MariaDB [(none)]>  CHANGE MASTER TO master_delay=60;
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 172.16.1.92
                   Master_User: repl
                   Master_Port: 4000
                 Connect_Retry: 60
               Master_Log_File: node4_bin.000001
           Read_Master_Log_Pos: 1534
                Relay_Log_File: node5_relay.000002
                 Relay_Log_Pos: 555
         Relay_Master_Log_File: node4_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: 1534
               Relay_Log_Space: 860
               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: 40
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 60
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 6
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 1
1 row in set (0.000 sec)
延迟的单位是秒(s).设置之后 SQL_Delay: 60。
--验证:
在主库插入数据:
MariaDB [(none)]> insert into wuhan.city(cityname)values('shenzhen !');
Query OK, 1 row affected (0.022 sec)
MariaDB [(none)]> select * from wuhan.city;
+--------+------------+
| cityid | cityname   |
+--------+------------+
|      1 | wuhan!     |
|      2 | shenzhen ! |
+--------+------------+
2 rows in set (0.000 sec)
MariaDB [(none)]> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-07-18 17:12:30 |
+---------------------+
1 row in set (0.000 sec)

--在从库验证:
MariaDB [(none)]> select * from wuhan.city;
+--------+----------+
| cityid | cityname |
+--------+----------+
|      1 | wuhan!   |
+--------+----------+
1 row in set (0.000 sec)

MariaDB [(none)]> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-07-18 17:12:41 |
+---------------------+
1 row in set (0.000 sec)
此时主库有2条记录,从库只有1条记录。
等待60s之后查询则有相同的数据:
MariaDB [(none)]> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-07-18 17:14:24 |
+---------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> select * from wuhan.city;
+--------+------------+
| cityid | cityname   |
+--------+------------+
|      1 | wuhan!     |
|      2 | shenzhen ! |
+--------+------------+
2 rows in set (0.000 sec)

延迟复制的过程中查看从库的状态:

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 172.16.1.92
                   Master_User: repl
                   Master_Port: 4000
                 Connect_Retry: 60
               Master_Log_File: node4_bin.000002
           Read_Master_Log_Pos: 777
                Relay_Log_File: node5_relay.000005
                 Relay_Log_Pos: 903
         Relay_Master_Log_File: node4_bin.000002
              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: 604
               Relay_Log_Space: 1733
               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: 4
 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: 40
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 60
           SQL_Remaining_Delay: 56
       Slave_SQL_Running_State: Waiting until MASTER_DELAY seconds after master executed event
              Slave_DDL_Groups: 6
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 3
1 row in set (0.000 sec)

延迟复制我们关注4个参数选项即可:

Seconds_Behind_Master: 29
             SQL_Delay: 60
   SQL_Remaining_Delay: 31
Slave_SQL_Running_State: Waiting until MASTER_DELAY seconds after master executed event
说明:
Seconds_Behind_Master表示从库落后于主库的时间,单位为妙。不过此参数不够严谨,仅做参考。
SQL_Delay:设置的延迟时间,单位妙。
SQL_Remaining_Delay:若主库在insert、delete、update操作从库剩余延时的时间。若没有延时的时候此选项的值为NULL。
SQL_Remaining_Delay+Seconds_Behind_Master=SQL_Delay。
Slave_SQL_Running_State:当延时复制的时候出现Waiting until MASTER_DELAY seconds after master executed event。当延时复制完毕之后显示为Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
这里出现的状态可以通过命令 show processlist查看的一样:
MariaDB [(none)]> show processlist;
+----+-----------------+-----------+------+-----------+-------+----------------------------------------------------------------+------------------+----------+
| Id | User            | Host      | db   | Command   | Time  | State                                                          | Info             | Progress |
+----+-----------------+-----------+------+-----------+-------+----------------------------------------------------------------+------------------+----------+
|  1 | system user     |           | NULL | Daemon    |  NULL | InnoDB purge coordinator                                       | NULL             |    0.000 |
|  2 | system user     |           | NULL | Daemon    |  NULL | InnoDB purge worker                                            | NULL             |    0.000 |
|  4 | system user     |           | NULL | Daemon    |  NULL | InnoDB purge worker                                            | NULL             |    0.000 |
|  3 | system user     |           | NULL | Daemon    |  NULL | InnoDB purge worker                                            | NULL             |    0.000 |
|  5 | system user     |           | NULL | Daemon    |  NULL | InnoDB shutdown handler                                        | NULL             |    0.000 |
|  7 | event_scheduler | localhost | NULL | Daemon    | 70173 | Waiting on empty queue                                         | NULL             |    0.000 |
| 20 | system user     |           | NULL | Slave_IO  | 65817 | Waiting for master to send event                               | NULL             |    0.000 |
| 21 | system user     |           | NULL | Slave_SQL |    11 | Waiting until MASTER_DELAY seconds after master executed event | NULL             |    0.000 |

猜你喜欢

转载自blog.csdn.net/vkingnew/article/details/81101509