• MySQL双机热备份


    系统: CentOS release 6.6 (Final)

    MySQL: mysql  Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1

    主从双机热备份,master <=> slave,任何一方增删改的数据都会被同步到另一方。假设A有数据库 test,ip地址为192.168.0.110,现在要将B建立起与A的双机热备份,ip地址为192.168.1.120。

    1、注意事项

    a.主机的mysql版本不能高于从机的mysql版本,可以是一样的。

    b.主机和从机的mysql使用的端口号必须是一样的。

    2、打开AB防火墙的3306端口:

    sudo iptables -I IINPUT -p tcp --dport 3306 -j ACCEPT
    
    sudo service iptables save

    3、设置MySQL复制用户:

    Server A

    grant replcation slave on *.* to 'dbrep'@'192.168.0.110';

    Server B

    grant replcation slave on *.* to 'dbrep'@'192.168.0.120';

    4、配置mysql

    Server A:

    sudo vim /etc/my.cnf
    
    #添加到[mysqld]部分
    
    [mysqld]
    
    #...
    
    server-id=1
    log-bin=mysql-log-bin
    binlog_format=mixed
    
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    
    replicate-do-db=test
    relay-log=mysql-relay-log
    log-slave-updates=true
    read-only=0
    
    auto-increment-increment=2
    auto-increment-offset=1

    Server B:

    sudo vim /etc/my.cnf
    
    #添加到[mysqld]部分
    
    [mysqld]
    
    #...
    
    server-id=2
    log-bin=mysql-log-bin
    binlog_format=mixed
    
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    
    replicate-do-db=test
    relay-log=mysql-relay-log
    log-slave-updates=true
    read-only=0
    
    auto-increment-increment=2
    auto-increment-offset=2

     注释:

    第一组,每个mysql server设定不同的server-id以区分,log-bin指定二进制日志的命名,保存的格式是mixed。

    第二组,忽略不保存mysqlinformation_schema的二进制日志。

    第三组,执行更改的数据库。

    第四组,防止AB都写数据时发生冲突,设置id auto-increment的步进是2,偏移分别为1和2。这里只有两台服务器,如果有多台则可增大步进,并分别设定各自的偏移。

    以上参数详见MySQL manual 复制配置部分.

    重启mysqld,使配置生效。

    5、从A复制数据到B,保持两者初态的数据相同。

    Server A

    mysql> flush tables with read lock;
    mysqldump -uroot -p iksdb > dbtest.sql
    mysql -uroot -p

    注:多个数据库,mysqldump -uroot -p --databases test test2 test3 > dbtest123.sql。所有数据库,mysqldump -uroot -p --all-database > dball.sql。某个数据库的表,mysqldump -uroot -p db1 db1_tb1 db1_tb2 > db1_tb1_tb2.sql

    Server B

    mysql -uroot -p test < dbtest.sql

    6、建立A到B的复制

    Server A MySQL shell

    mysql> show master status;
    +----------------------+----------+--------------+--------------------------+
    | File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
    +----------------------+----------+--------------+--------------------------+
    | mysql-log-bin.000003 |  2526733 |              | mysql,information_schema |
    +----------------------+----------+--------------+--------------------------+
    1 row in set (0.00 sec)
    
    mysql> unlock tables;

    注:在备份数据库到show master status期间,要保持数据库的锁定状态,防止数据的改变,导致因为初态不一致产生的复制错误。

    Server B MySQL shell

    mysql> change master to
        -> master_host='192.168.0.110',
        -> master_user='dbrep',
        -> master_password='123456',
        -> master_log_file='mysql-log-bin.000003',
        -> master_log_pos=2526733;
    
    mysql> slave start;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show master statusG
    *************************** 1. row ***************************
                File: mysql-log-bin.000001
            Position: 518
        Binlog_Do_DB: 
    Binlog_Ignore_DB: mysql,information_schema
    1 row in set (0.00 sec)
    
    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.0.110
                      Master_User: dbrep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-log-bin.000003
              Read_Master_Log_Pos: 2526733
                   Relay_Log_File: mysql-relay-log.000210
                    Relay_Log_Pos: 255
            Relay_Master_Log_File: mysql-log-bin.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: test
              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: 2526733
                  Relay_Log_Space: 559
                  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: 
    1 row in set (0.00 sec)
    
    mysql> 

    注意到 Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes,则表示成功复制。否则,可能是连接、用户或数据库初态不一致的问题。

    7、建立B到A的复制,完成双向备份

    Server B MySQL shell

    mysql> show master status;
    +----------------------+----------+--------------+--------------------------+
    | File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
    +----------------------+----------+--------------+--------------------------+
    | mysql-log-bin.000001 |      518 |              | mysql,information_schema |
    +----------------------+----------+--------------+--------------------------+
    1 row in set (0.00 sec)

    Server A MySQL shell

    mysql> slave stop;
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> change master to
        -> master_host='192.168.0.120',
        -> master_user='dbrep',
        -> master_password='123456',
        -> master_log_file='mysql-log-bin.000001',
        -> master_log_pos=518;
    
    mysql> slave start;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.0.120
                      Master_User: dbrep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-log-bin.000001
              Read_Master_Log_Pos: 518
                   Relay_Log_File: mysql-relay-log.000226
                    Relay_Log_Pos: 255
            Relay_Master_Log_File: mysql-log-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: test
              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: 518
                  Relay_Log_Space: 559
                  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: 
    1 row in set (0.00 sec)
    
    mysql> 

     注:以上在B的数据不会被操作的环境下操作,如有必要,同样得锁定数据库

    同样看到 Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes,表示成功复制。

    注意:在数据库操作时,如果没有 选择数据的操作, 像 use test, 这个操作将不会写入到二进制日志,这样就不会完成复制。像 "insert into test.test values(1,2,3);",这样虽然能写到master数据库,但是在slave不会有任何改变。

    参考:

      学一点 mysql 双机异地热备份----快速理解mysql主从,主主备份原理及实践

      MySQL备份与恢复之热备

      MySQL 5.5 Reference Manual  / Replication

      MySQL binlog

  • 相关阅读:
    SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较
    LINQ to List泛型的几种简单查询
    sql语句精选
    C#利用QrCode.Net生成二维码(Qr码)
    LINQ to XML CRUD,并且封装为DAL方法
    vim编辑器
    去年的烟花特别多……
    年初七
    活死人黎明 Dawn of the Dead
    在碟片里奔驰我的看碟人生
  • 原文地址:https://www.cnblogs.com/hubery/p/4894411.html
Copyright © 2020-2023  润新知