Mysql 主從熱備份

工做原理

首先鎖定並備份主服務器數據庫,從服務器導入備份的數據庫,實現兩個數據庫的初態同樣。而後把主服務器上執行過的sql語句都記錄到二進制日誌 Binarylog 中,從服務器會來讀取這個log, 本身再執行一遍對應的sql語句, 這樣它們就一直同步了。mysql

IP

主服務器IP:192.168.0.41sql

從服務器IP:192.168.0.42數據庫

主服務器配置

[mysqld]
#主從備份
log-bin=mysql-bin
binlog_format=mixed
server-id=1

read-only=0
binlog-do-db=mydb
binlog-ignore-db=mysql
binlog-ignore-db=test
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
#雙主備份
#auto-increment-increment=10
#auto-increment-offset=1

 

binlog_format = MIXED緩存

#binlog日誌格式,mysql默認採用statement,建議使用mixed服務器

log-bin = /data/mysql/mysql-bin.logide

#binlog日誌文件spa

expire_logs_days = 7線程

#binlog過時清理時間日誌

max_binlog_size = 100mcode

#binlog每一個日誌文件大小

binlog_cache_size = 4m

#binlog緩存大小

max_binlog_cache_size = 512m

#最大binlog緩存大小

從服務器配置

[mysqld]
#主從備份
#log-bin=mysql-bin
#binlog_format=mixed
server-id=2

replicate-do-db=mydb
replicate-ignore-db=mysql
replicate-ignore-db=test
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-wild-ignore-table=mydb.000%
replicate-wild-ignore-table=mydb.tmp%
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
replicate-wild-ignore-table=performance_schema.%
relay_log=mysql-relay-bin
log-slave-updates=ON

 

建立備份用戶

grant replication slave on *.* to 'backup'@'192.168.0.42' identified by '123456';

binary日誌

查看主服務器binary日誌,開啓同步要用

mysql> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 106
    Binlog_Do_DB: mydb
Binlog_Ignore_DB: mysql,information_schema,performance_schema,test
1 row in set (0.00 sec)

開啓同步

在從服務器執行以下命令開啓同步並重啓mysql

mysql> CHANGE MASTER TO 
           MASTER_HOST='192.168.0.41', 
           MASTER_USER='backup', 
           MASTER_PASSWORD='123456', 
           MASTER_LOG_FILE='mysql-bin.000001', 
           MASTER_LOG_POS=106;

注:開啓同步時,默認主從服務器初態同樣

驗證同步

在從服務器執行以下命令驗證同步

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.41
                  Master_User: cifang
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysql-relay-bin.000001
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: mydb
          Replicate_Ignore_DB: mysql,information_schema,performance_schema,test
           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: 106
              Relay_Log_Space: 551
              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)

Master_Log_File: 主服務器上的日誌文件名

Read_Master_Log_Pos: 主服務器的日誌記錄位置

Slave_IO_Running: 主從服務器鏈接線程狀態

Slave_SQL_Running:從服務器執行sql的線程

注:若是主從服務器的日誌文件名和記錄位置一致,且兩個狀態都爲Yes,表示主從備份正常。

其餘

查找當前有哪些二進制日誌文件:

mysql> show binary logs;

刪除mysql-bin.000001以前的全部二進制日誌文件

mysql> purge binary logs to 'mysql-bin.000001';
Query OK, 0 rows affected (0.08 sec)

查看最前的二進制日誌文件內容

show binlog events;

注:經過以上命令能夠找到主從斷開的位置,以便從新關聯

相關文章
相關標籤/搜索