Mysql 主从热备份

工作原理

首先锁定并备份主服务器数据库,从服务器导入备份的数据库,实现两个数据库的初态一样。然后把主服务器上执行过的sql语句都记录到二进制日志 Binarylog 中,从服务器会来读取这个log, 自己再执行一遍对应的sql语句, 这样它们就一直同步了。

IP

主服务器IP:192.168.0.41

从服务器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.log

#binlog日志文件

expire_logs_days = 7

#binlog过期清理时间

max_binlog_size = 100m

#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;

注:通过以上命令可以找到主从断开的位置,以便重新关联

 

posted @ 2016-03-14 16:30  Jeff.Gao  阅读(460)  评论(0编辑  收藏  举报