MySQL · 3 min read · Nov 02, 2025
Como Reparar a Replicação MySQL

Se você configurou a replicação MySQL, provavelmente conhece este problema: às vezes há consultas MySQL inválidas que fazem com que a replicação pare de funcionar. Neste guia curto, explico como você pode reparar a replicação no escravo MySQL sem a necessidade de configurá-la do zero novamente. Este guia é para MySQL e MariaDB.
1 Identificando o Problema
Para descobrir se a replicação está/não está funcionando e o que causou a parada, você pode dar uma olhada nos logs. No Debian, por exemplo, o MySQL registra em /var/log/syslog:
grep mysql /var/log/syslogserver1:/home/admin# grep mysql /var/log/syslog
MAR 19 09:56:08 http2 mysqld[1380]: 080529 9:56:08 [ERROR] Slave: Error 'Table 'mydb.taggregate_temp_1212047760' doesn't exist' on query. Default database: 'mydb'. Query: 'UPDATE thread AS thread,taggregate_temp_1212047760 AS aggregate
MAR 19 09:56:08 http2 mysqld[1380]: ^ISET thread.views = thread.views + aggregate.views
MAR 19 09:56:08 http2 mysqld[1380]: ^IWHERE thread.threadid = aggregate.threadid', Error_code: 1146
MAR 19 09:56:08 http2 mysqld[1380]: 080529 9:56:08 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.001079' position 203015142
server1:/home/admin#Você pode ver qual consulta causou o erro e em qual posição do log a replicação parou.
Para verificar se a replicação realmente não está funcionando, faça login no MySQL:
mysql -u root -pNo shell do MySQL, execute:
mysql> SHOW SLAVE STATUS \GSe um dos Slave_IO_Running ou Slave_SQL_Running estiver definido como Não, então a replicação está quebrada:
mysql> SHOW SLAVE STATUS \G
************************* 1. row ***********************
Slave_IO_State: Waiting for master to send event
Master_Host: 1.2.3.4
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.001079
Read_Master_Log_Pos: 269214454
Relay_Log_File: slave-relay.000130
Relay_Log_Pos: 100125935
Relay_Master_Log_File: mysql-bin.001079
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB: mydb
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1146
Last_Error: Error 'Table 'mydb.taggregate_temp_1212047760' doesn't exist' on query. Default database: 'mydb'.
Query: 'UPDATE thread AS thread,taggregate_temp_1212047760 AS aggregate
SET thread.views = thread.views + aggregate.views
WHERE thread.threadid = aggregate.threadid'
Skip_Counter: 0
Exec_Master_Log_Pos: 203015142
Relay_Log_Space: 166325247
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: NULL
1 row in set (0.00 sec)
mysql>2 Reparar a Replicação MySQL
Só para ter certeza, paramos o escravo:
mysql> STOP SLAVE;Corrigir o problema é na verdade bem fácil. Dizemos ao escravo para simplesmente ignorar a consulta SQL inválida:
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;Isso diz ao escravo para ignorar uma consulta (que é a inválida que causou a parada da replicação). Se você quiser ignorar duas consultas, você usaria SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 2; em vez disso e assim por diante.
É isso. Agora podemos iniciar o escravo novamente…
mysql> START SLAVE;… e verificar se a replicação está funcionando novamente:
mysql> SHOW SLAVE STATUS \Gmysql> SHOW SLAVE STATUS \G*********************** 1. row ************************* Slave_IO_State: Waiting for master to send event Master_Host: 1.2.3.4 Master_User: slave_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.001079 Read_Master_Log_Pos: 447560366 Relay_Log_File: slave-relay.000130 Relay_Log_Pos: 225644062 Relay_Master_Log_File: mysql-bin.001079 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: mydb 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: 447560366 Relay_Log_Space: 225644062 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 1 row in set (0.00 sec) mysql>
Como você vê, tanto Slave_IO_Running quanto Slave_SQL_Running estão definidos como Sim agora.
Agora saia do shell do MySQL…
mysql> quit;… e verifique o log novamente:
grep mysql /var/log/syslogserver1:/home/admin# grep mysql /var/log/syslog
MAR 19 09:56:08 http2 mysqld[1380]: 080529 9:56:08 [ERROR] Slave: Error 'Table 'mydb.taggregate_temp_1212047760' doesn't exist' on query. Default database: 'mydb'. Query: 'UPDATE thread AS thread,taggregate_temp_1212047760 AS aggregate
MAR 19 09:56:08 http2 mysqld[1380]: ^ISET thread.views = thread.views + aggregate.views
MAR 19 09:56:08 http2 mysqld[1380]: ^IWHERE thread.threadid = aggregate.threadid', Error_code: 1146
MAR 19 09:56:08 http2 mysqld[1380]: 080529 9:56:08 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.001079' position 203015142
MAR 19 11:42:13 http2 mysqld[1380]: 080529 11:42:13 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.001079' at position 203015142, relay log '/var/lib/mysql/slave-relay.000130' position: 100125935
server1:/home/admin#A última linha diz que a replicação foi iniciada novamente, e se você não vir erros após essa linha, tudo está ok.
3 Links
- MySQL: http://www.mysql.com
Receba novas postagens na sua caixa de entrada
Sem spam. Cancele a assinatura a qualquer momento.