Base de données · 3 min read · Nov 02, 2025

Comment réparer la réplication MySQL

Si vous avez configuré la réplication MySQL, vous connaissez probablement ce problème : parfois, il y a des requêtes MySQL invalides qui empêchent la réplication de fonctionner. Dans ce court guide, j’explique comment vous pouvez réparer la réplication sur l’esclave MySQL sans avoir besoin de la configurer à nouveau depuis le début. Ce guide est pour MySQL et MariaDB.

1 Identifier le problème

Pour savoir si la réplication fonctionne ou non et ce qui a causé son arrêt, vous pouvez jeter un œil aux journaux. Sur Debian, par exemple, MySQL journalise dans /var/log/syslog :

grep mysql /var/log/syslog
server1:/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#

Vous pouvez voir quelle requête a causé l’erreur et à quelle position du journal la réplication s’est arrêtée.

Pour vérifier que la réplication ne fonctionne vraiment pas, connectez-vous à MySQL :

mysql -u root -p

Dans le shell MySQL, exécutez :

mysql> SHOW SLAVE STATUS \G

Si l’un des Slave_IO_Running ou Slave_SQL_Running est défini sur No, alors la réplication est cassée :

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 Réparer la réplication MySQL

Juste pour être sûr, nous arrêtons l’esclave :

mysql> STOP SLAVE;

Réparer le problème est en fait assez facile. Nous disons à l’esclave de simplement ignorer la requête SQL invalide :

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;

Cela dit à l’esclave d’ignorer une requête (qui est celle qui a causé l’arrêt de la réplication). Si vous souhaitez ignorer deux requêtes, vous utiliseriez SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 2; à la place, et ainsi de suite.

C’est déjà tout. Maintenant, nous pouvons redémarrer l’esclave…

mysql> START SLAVE;

… et vérifier si la réplication fonctionne à nouveau :

mysql> SHOW SLAVE STATUS \G
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: 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>

Comme vous le voyez, les deux Slave_IO_Running et Slave_SQL_Running sont maintenant définis sur Yes.

Maintenant, quittez le shell MySQL…

mysql> quit;

… et vérifiez à nouveau le journal :

grep mysql /var/log/syslog
server1:/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#

La dernière ligne indique que la réplication a recommencé, et si vous ne voyez pas d’erreurs après cette ligne, tout va bien.

3 Liens

Share: X/Twitter LinkedIn

Recevez de nouveaux articles dans votre boîte de réception.

Aucun spam. Désabonnez-vous à tout moment.