Database · 3 min read · Nov 02, 2025

Come riparare la replica MySQL

Se hai impostato la replica MySQL, probabilmente conosci questo problema: a volte ci sono query MySQL non valide che fanno sì che la replica non funzioni più. In questa breve guida, spiego come puoi riparare la replica sullo slave MySQL senza doverla impostare da zero. Questa guida è per MySQL e MariaDB.

1 Identificare il Problema

Per scoprire se la replica sta funzionando o meno e cosa ha causato il suo arresto, puoi dare un’occhiata ai log. Su Debian, ad esempio, MySQL registra in /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#

Puoi vedere quale query ha causato l’errore e a quale posizione del log la replica si è fermata.

Per verificare che la replica non stia davvero funzionando, accedi a MySQL:

mysql -u root -p

Nella shell MySQL, esegui:

mysql> SHOW SLAVE STATUS \G

Se uno dei Slave_IO_Running o Slave_SQL_Running è impostato su No, allora la replica è rotta:

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 Riparare la Replica MySQL

Solo per essere sicuri, fermiamo lo slave:

mysql> STOP SLAVE;

Riparare il problema è in realtà abbastanza facile. Diciamo allo slave di semplicemente saltare la query SQL non valida:

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;

Questo dice allo slave di saltare una query (che è quella non valida che ha causato l’arresto della replica). Se desideri saltare due query, utilizzeresti SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 2; invece e così via.

Ecco fatto. Ora possiamo avviare di nuovo lo slave…

mysql> START SLAVE;

… e controllare se la replica sta funzionando di nuovo:

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>

Come vedi, sia Slave_IO_Running che Slave_SQL_Running sono impostati su Yes ora.

Ora esci dalla shell MySQL…

mysql> quit;

… e controlla di nuovo il log:

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#

L’ultima riga dice che la replica è ripartita, e se non vedi errori dopo quella riga, tutto va bene.

3 Link

Share: X/Twitter LinkedIn

Ricevi i nuovi post nella tua casella di posta.

Nessuno spam. Disiscriviti in qualsiasi momento.