MySQL数据库双机热备份

MySQL数据库双机热备份

1、mysql 数据库没有增量备份的机制

  当数据量太大的时候备份是一个很大的问题。还好 mysql 数据库提供了一种主从备份的机制,其实就是把主数据库的所有的数据同时写到备份数据库中。实现 mysql 数据库的热备份。

2、要想实现双机的热备首先要了解主从数据库服务器的版本的需求。

  要实现热备 mysql 的版本都要高于3.2,还有一个基本的原则就是作为从数据库的数据库版本可以高于主服务器数据库的版本,但是不可以低于主服务器的数据库版本。

3、设置主数据库服务器:

  a.首先查看主服务器的版本

  首先查看主服务器的版本是否是支持热备的版本。然后查看 my.cnf(类 unix)或者 my.ini(windows)中 mysqld 配置块的配置有没有 log-bin (记录数据库更改日志),因为 mysql 的复制机制是基于日志的复制机制,所以主服务器一定要支持更改日志才行。然后设置要写入日志的数据库或者不要写入日志的数据库。这样只有您感兴趣的数据库的更改才写入到数据库的日志中。

  b.配置

  1.创建同步用户

mysql> GRANT REPLICATION SLAVE ON *.*
    -> TO 'repl'@'%' IDENTIFIED BY 'slavepass';
    4.0.2 以前的版本, 因为不支持 REPLICATION 要使用下面的语句来实现这个功能
mysql> GRANT FILE ON *.*
    -> TO 'repl'@'%' IDENTIFIED BY 'slavepass';

  2.主从模式:A->B

  A为master
  修改A mysql的my.ini文件。在mysqld配置项中加入下面配置:

  以下是老版本的写法,我用这个配置, mysql 5.6 启动不起来,

    server-id=1 //数据库的 id 这个应该默认是1就不用改动
    log-bin=log_name //日志文件的名称,这里也可以制定日志到别的目录 如果没有设置则默认主机名的一个日志名称
    binlog-do-db=db_name //记录日志的数据库
    binlog-ignore-db=db_name //不记录日志的数据库

由于我使用的是5.6 的版本, 配置如下:

binlog_do_db=test
server_id = 1
log_bin=F:/db-data/binlog/binLog.log
log-error =F:/db-data/logs/mysql-error.log
general_log = 1
general_log_file =F:/db-data/logs/mysql.log

  c.重起数据库服务。  

  用show master status 命令看日志情况。

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

  B为slave
  老版本可以修改B mysql的my.ini文件。在mysqld配置项中加入下面配置:

  

server-id=2
master-host=xxxxxxx
#同步用户帐号
master-user=x
master-password=x
master-port=3306
#预设重试间隔60秒
master-connect-retry=60
#告诉slave只做backup数据库的更新
replicate-do-db=test 
binlog_ignore_db=mysql

但是 Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数了;

无奈之下,从数据库my.cnf修改为以下配置;(主数据库中注意ID为1,并加入要同步的库既可)

server-id=2

然后再从库执行命令:

change master to master_host='***.**.**.***', master_user='xx', master_password='xx',MASTER_CONNECT_RETRY=60;

 我们可以使用 show slave statusG; 来查看一下slave 状态

mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 115.29.36.149
                  Master_User: c
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binLog.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: Q12MB1DR67JGLJT-relay-bin.000002
                Relay_Log_Pos: 267
        Relay_Master_Log_File: binLog.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: 107
              Relay_Log_Space: 450
              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:
             Master_Info_File: F:db-datamysqlmaster.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 upd
ate 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)

很明显, 哥成功了,

   当然有时候我们也会失败,那样我们可以使用 start/stop savle 来改变 从机的状态 ,或者在 Last_SQL_Error 里找到错误日志

 

双机互备模式。

  如果在A加入slave设置,在B加入master设置,则可以做B->A的同步。

  注意:当有错误产生时*.err日志文件。同步的线程退出,当纠正错误后要让同步机制进行工作,运行slave start

  重起AB机器,即可实现双向的热备。

原文地址:https://www.cnblogs.com/mjorcen/p/3995459.html