データベース · 2 min read · Nov 02, 2025

MySQLレプリケーションの修復方法

MySQLレプリケーションを設定した場合、この問題を知っているかもしれません: 時々、無効なMySQLクエリが発生し、レプリケーションが機能しなくなります。この短いガイドでは、MySQLスレーブのレプリケーションを最初から設定し直すことなく修復する方法を説明します。このガイドはMySQLとMariaDB向けです。

1 問題の特定

レプリケーションが機能しているかどうか、またそれを停止させた原因を確認するには、ログを確認できます。たとえば、DebianではMySQLは/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#

どのクエリがエラーを引き起こしたか、レプリケーションが停止したログの位置を確認できます。

レプリケーションが本当に機能していないか確認するには、MySQLにログインします:

mysql -u root -p

MySQLシェルで、次のコマンドを実行します:

mysql> SHOW SLAVE STATUS \G

もしSlave_IO_RunningまたはSlave_SQL_RunningのいずれかがNoに設定されている場合、レプリケーションは壊れています:

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 MySQLレプリケーションの修復

念のため、スレーブを停止します:

mysql> STOP SLAVE;

問題を修正するのは実際には非常に簡単です。スレーブに無効なSQLクエリを単にスキップするように指示します:

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;

これにより、スレーブは1つのクエリ(レプリケーションを停止させた無効なクエリ)をスキップするように指示されます。2つのクエリをスキップしたい場合は、代わりにSET GLOBAL SQL_SLAVE_SKIP_COUNTER = 2;を使用します。

これで完了です。スレーブを再起動できます…

mysql> START SLAVE;

… そしてレプリケーションが再び機能しているか確認します:

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>

ご覧の通り、Slave_IO_RunningとSlave_SQL_Runningの両方がYesに設定されています。

MySQLシェルを終了します…

mysql> quit;

… そして再度ログを確認します:

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#

最後の行はレプリケーションが再び開始されたことを示しており、その行の後にエラーが表示されなければ、すべてが正常です。

3 リンク

Share: X/Twitter LinkedIn

新しい投稿を受信箱で受け取る

スパムはありません。いつでも購読を解除できます。