注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

PostgreSQL 中文网

 
 
 

日志

 
 

MySQL:延迟的主从复制 ( Delayed Replication )  

2014-12-12 15:16:45|  分类: MySQL |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

         MySQL 5.6 已经支持延迟的流复制, 可设置备节点的延迟时间, 延迟复制是有意义的,例如防止主节点数据误删,查看数据库历史状态等, PostgreSQL 在即将发行的 9.4 版本也支持延迟复制,有兴趣的朋友可以查看之前写的 blog:  PostgreSQL9.4 : 支持备库延迟复制(delayed standbys)  , 这里简单介绍 MySQL 延迟复制的配置。
    
        MySQL 的主从搭建本文略。 CHANGE MASTER TO  命令指定 MASTER_DELAY 选项即可设置,如下: 
    
一 语法

CHANGE MASTER TO MASTER_DELAY = 30;

备注:设置备节点延迟的时间,单位秒。


二 配置延迟复制
--从节点设置

root@localhost:mysql>stop slave;
Query OK, 0 rows affected (0.21 sec)

root@localhost:mysql>CHANGE MASTER TO MASTER_DELAY = 30;
Query OK, 0 rows affected (0.17 sec)

root@localhost:mysql>start slave;
Query OK, 0 rows affected (0.27 sec)


--查看从节点状态

root@localhost:mysql>select * from slave_relay_log_info\G
*************************** 1. row ***************************
Number_of_lines: 7
Relay_log_name: ./db2-relay-bin.000007
Relay_log_pos: 281
Master_log_name: bin-log.000041
Master_log_pos: 361
Sql_delay: 30
Number_of_workers: 0
Id: 1
1 row in set (0.00 sec)


root@localhost:mysql>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.37
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: bin-log.000040
          Read_Master_Log_Pos: 604
               Relay_Log_File: db2-relay-bin.000002
                Relay_Log_Pos: 281
        Relay_Master_Log_File: bin-log.000040
             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: 452
              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: 0c130d47-22bb-11e4-aaaa-000c2986ac80
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 30
          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:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)


--主节点创建表测试

root@localhost:mysql>create table test_delay(id int4 primary key, create_time datetime default current_timestamp);  
Query OK, 0 rows affected (0.59 sec)

root@localhost:mysql>insert into test_delay (id) values(1);
Query OK, 1 row affected (0.07 sec)

备注:在主节点上创建一张测试表并插入一条测试数据。

--备节点查询

root@localhost:mysql>select * from test_delay;
Empty set (0.00 sec)

root@localhost:mysql>select * from test_delay;
Empty set (0.00 sec)
这时在备节点上还查不到表。

root@localhost:mysql>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.37
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: bin-log.000040
          Read_Master_Log_Pos: 1013
               Relay_Log_File: db2-relay-bin.000002
                Relay_Log_Pos: 281
        Relay_Master_Log_File: bin-log.000040
             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: 861
              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: 15
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: 0c130d47-22bb-11e4-aaaa-000c2986ac80
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 30
          SQL_Remaining_Delay: 14
      Slave_SQL_Running_State: Waiting until MASTER_DELAY seconds after master executed event
           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)

root@localhost:mysql>select * from test_delay;
+----+---------------------+
| id | create_time         |
+----+---------------------+
|  1 | 2014-12-12 14:38:47 |
+----+---------------------+
1 row in set (0.00 sec)

备注: 发现备节点在 30 秒后才能查到新建的表,这里有三个字段显示延迟的信息:
       SQL_Delay: 显示已设置的主备延迟的时间,单位秒;
       SQL_Remaining_Delay: 显示剩余的主备延迟时间,单位秒;
       Slave_SQL_Running_State: 表示 SQL thread 状态;

       
三 参考
  评论这张
 
阅读(1742)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016