且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mysql主从同步异常处理

更新时间:2021-12-14 18:50:52

一、在Mysql中,如果是主从(包括一主多从)模式的数据库配置。请在创建数据库和对数控库进行增删改查操作,一定要在主库进行,从库主要提供主库备份和数据查询功能,请勿直接在从库进行增删改查。

二、如果因为操作不当,导致从库不再同步主库了。
那这就需要从新配置从库了。

三、登陆主库,查看主库的信息

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

四、登陆从库查看从库是否在同步主库

mysql>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 39.107.227.105
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2021
               Relay_Log_File: iZ2ze451u03knf2sumyavyZ-relay-bin.014660
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
            ......
            Seconds_Behind_Master:NULL

五、你会发现下面的参数变成No和NULL了,就说明从库同步出现了问题

ead_Master_Log_Pos: 2021 。端口都不一致了
Slave_IO_Running: No
Slave_SQL_Running: No
Seconds_Behind_Master:NULL

六、从新配置从库同步主库
1.先停止从库同步线程

mysql> Stop SLAVE;
Query OK, 0 rows affected (0.01 sec)

2.配置需要同步的主库信息

mysql> CHANGE MASTER TO MASTER_HOST='39.107.227.105', MASTER_USER='backup', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

3.启动从库同步线程

mysql> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

七、查看从库的同步状态

mysql>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 39.107.227.105
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: iZ2ze451u03knf2sumyavyZ-relay-bin.014660
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
            ......
            Seconds_Behind_Master:0

参数一切正常:

Read_Master_Log_Pos: 154
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master:0

好了,主从同步异常的问题就这样解决了。