MySQL主从热备份

/ Linux / 0 条评论 / 300 浏览

MySQL5.7主从配置

前提:防火墙关闭,可以相互ping通,所有机器安装完成MySQL5.7

安装教程:http://blog.lianglianglee.com/article/centos-install-mysql

如果使用虚拟机复制的,请关闭MySQL后删除/var/lib/mysql/auto.cnf并重启

MySQL会在第一次启动生成一个UUID,虚拟机复制会导致UUID一致,主从不可启用

主数据库配置修改

主数据库配置/etc/my/conf 在mysqld下增加以下内容

log-bin           = mysql-bin  #[必须]启用二进制日志
server-id         = 41          #[必须]服务器唯一ID,默认是1,最好取ip的后3位
binlog-ignore-db  = mysql       #不备份的数据库
binlog-ignore-db  = information_schema
binlog-ignore-db  = performation_schema
binlog-ignore-db  = sys
binlog-do-db=pm25 #需要做复制的数据库名

重启,主数据库中执行

show master status \G

返回:

mysql> show master status \G
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 315
     Binlog_Do_DB: pm25
 Binlog_Ignore_DB: mysql,information_schema,performation_schema,sys
Executed_Gtid_Set:
1 row in set (0.00 sec)

此处的File,Position重要,在从库中会用到

从数据库配置

修改/etc/my.conf

log-bin           = mysql-bin  #[必须]启用二进制日志
server-id         = 40         #[必须]服务器唯一ID,默认是1,最好取ip的后3位
binlog-ignore-db  = mysql       #不备份的数据库
binlog-ignore-db  = information_schema
binlog-ignore-db  = performation_schema
binlog-ignore-db  = sys
binlog-do-db=pm25  #需要做复制的数据库名

配置从库

连接从库执行

change master to master_host='172.168.0.41', master_user='root' ,master_password='123456', master_log_file='mysql-bin.000003' ,master_log_pos=315;

主库的show master status \G执行结果中,Filemaster_log_filePositionmaster_log_pos

开启从库

start slave;

查看状态

 show slave status \G

返回:

mysql>  show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: rabbit02
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 119326
               Relay_Log_File: rabbit01-relay-bin.000004
                Relay_Log_Pos: 607
        Relay_Master_Log_File: mysql-bin.000005
             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: 119326
              Relay_Log_Space: 817
              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: 41
                  Master_UUID: 20b968c6-2079-11e9-afec-00155d00685a
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updat                                                                                                                                                          es
           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
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

以下两项必须是Yes

Slave_IO_Running: Yes   
Slave_SQL_Running: Yes

测试

在主库的表中创建表,在从库查看是否同步