目录

查看主从是否同步

详解Slave_IO、Slave_SQL 

判断主从完全同步

各个 Log_File 和 Log_Pos的关系

修复命令


查看主从是否同步

show slave status;

Slave_IO_Running、Slave_SQL_Running,这两个值是Yes表示正常,No是异常

使用竖排显示:

Name                         |Value                                                 |
-----------------------------+------------------------------------------------------+
Slave_IO_State               |Waiting for master to send event                      |
Master_Host                  |192.168.20.161                                        |
Master_User                  |root                                                  |
Master_Port                  |3306                                                  |
Connect_Retry                |60                                                    |
Master_Log_File              |mysql-bin.004651                                      |
Read_Master_Log_Pos          |300004719                                             |
Relay_Log_File               |xxxx-relay-bin.002999                                 |
Relay_Log_Pos                |300004932                                             |
Relay_Master_Log_File        |mysql-bin.004651                                      |
Slave_IO_Running             |Yes                                                   |
Slave_SQL_Running            |Yes                                                   |
Replicate_Do_DB              |alisaas                                               |
Replicate_Ignore_DB          |mysql,information_schema,performation_schema,sys      |
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          |300004719                                             |
Relay_Log_Space              |300005190                                             |
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             |161                                                   |
Master_UUID                  |000fe6d4-25cd-21eb-a102-1111efa99c99                  |
Master_Info_File             |/sata01/data/mysql/master.info                        |
SQL_Delay                    |0                                                     |
SQL_Remaining_Delay          |                                                      |
Slave_SQL_Running_State      |Slave has read all relay log; waiting for more updates|
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           |                                                      |

详解Slave_IO、Slave_SQL 

Slave_IO线程负责把主库的bin日志(Master_Log)内容,抄写到从库的中继日志上(Relay_Log)。
Slave_SQL线程负责把中继日志上的语句在从库上执行一遍
 

Slave_IO线程相对比较简单,一般不容易出错。如果Slave_IO_Running显示为No,多为网络连接不上权限不够等环境问题。

Slave_SQL线程相对容易出错,例如人为手动的在从库插入一条数据,造成主从不一致。但此时两个线程的状态仍然是正常的。等到主库也插入一条同样的数据时,通知从库做相同操作,从库会出现主键重复的错误。此时Slave_SQL_Running的状态会变为No,而Last_SQL_ErrorLast_SQL_Error_Timestamp会记录错误的原因和发生时间

Slave_SQL线程会停止后续的SQL语句执行,因为它意识到往后执行会导致错误修复的难度增加。但Slave_IO线程不会停止,会继续抄log的工作。所以中继log依然是最新的。

这时候Master_Log_File: mysql-bin.000002 和  Relay_Master_Log_File: mysql-bin.000001之间就产生了偏差。

以上面为例,主库的日志已经写到了 mysql-bin.000001,而从库依然停留在 mysql-bin.000002等待人工修复错误。

那么当错误修复后,只需用stop slavestart slave重启下同步。Slave_SQL线程会重新的尝试工作。

如果没有问题,那么Slave_SQL的状态会变回Yes。但此时主从并没有完全同步,需要一点时间

判断主从完全同步

若完全同步,可通过以下几条判断:

1. 首先 Master_Log_FileRelay_Master_Log_File 所指向的文件必须一致。

例如本案例中是mysql-bin.004651

2. Read_Master_Log_Pos = Exec_Master_Log_Pos 时,则表明 slave 和 master 处于完全同步的状态。例如本案例中是:300004719

Read_Master_Log_Pos 是io读到的位置,Exec_Master_Log_Pos 是sql执行到的位置。

1和2合并即:

3. 此时,有几个字段值如下:

Slave_IO_State: Waiting for master to send event。意思是等待主库送事件过来

Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it。意思是从库已经读完所有的relay log,等待从库的IO线程继续更新。

各个 Log_File 和 Log_Pos的关系

1) The position, ON THE MASTER, from which the I/O thread is reading:

Master_Log_File/Read_Master_Log_Pos. -----相对于主库,从库读取主库的二进制日志的位置,是IO线程

2) The position, IN THE RELAY LOGS, at which the SQL thread is executing:

Relay_Log_File/Relay_Log_Pos ----相对于从库,是从库的sql线程执行到的位置

3) The position, ON THE MASTER, at which the SQL thread is executing:

Relay_Master_Log_File/Exec_Master_Log_Pos ----相对于主库,是从库的sql线程执行到的位置

show slave status\G中的Read_Master_Log_Pos和Relay_Log_Pos的(大小)关系_lovely可爱欧辰的博客-CSDN博客

修复命令

执行以下3条命令。可以在navicat、dbeaver、或者cmd窗口,都行。

stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
 

一次不行,多执行几次。或者修改数值1,改成2,或者3。前提是你知道怎么玩,不会玩的话,就用1,多执行几次。

Mysql主从错误之Slave_SQL_Running No - 简书

关于set global sql_slave_skip_counter=N 命令的解释_Mr. Sun_的博客-CSDN博客

Mysql主从同步时Slave_IO_Running:Connecting ; Slave_SQL_Running:Yes的情况故障排除_MegaBytes的博客-CSDN博客

Mysql主从同步时Slave_IO_Running:Connecting ; Slave_SQL_Running:Yes的情况故障排除_爽姐想退休的博客-CSDN博客

show slave status判断主从同步状态_chagaostu的博客-CSDN博客

mysql查看主从同步状态的方法_mysql_Full Stack Developme-华为云开发者联盟

MySQL :: MySQL 5.7 Reference Manual :: 16.1.2 Setting Up Binary Log File Position Based Replication

=========================分割线============================ 

文章到此已经结束,以下是紫薯布丁

show slave status;

stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;

Name                         |Value                                                 |
-----------------------------+------------------------------------------------------+
Slave_IO_State               |Waiting for master to send event                      |
Master_Host                  |192.168.20.161                                        |
Master_User                  |root                                                  |
Master_Port                  |3306                                                  |
Connect_Retry                |60                                                    |
Master_Log_File              |mysql-bin.004651                                      |
Read_Master_Log_Pos          |300004719                                             |
Relay_Log_File               |xxxx-relay-bin.002999                                 |
Relay_Log_Pos                |300004932                                             |
Relay_Master_Log_File        |mysql-bin.004651                                      |
Slave_IO_Running             |Yes                                                   |
Slave_SQL_Running            |Yes                                                   |
Replicate_Do_DB              |alisaas                                               |
Replicate_Ignore_DB          |mysql,information_schema,performation_schema,sys      |
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          |300004719                                             |
Relay_Log_Space              |300005190                                             |
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             |161                                                   |
Master_UUID                  |000fe6d4-25cd-21eb-a102-1111efa99c99                  |
Master_Info_File             |/sata01/data/mysql/master.info                        |
SQL_Delay                    |0                                                     |
SQL_Remaining_Delay          |                                                      |
Slave_SQL_Running_State      |Slave has read all relay log; waiting for more updates|
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           |                                                      |

07-27 11:16