웹사이트 검색

MySQL 복제를 복구하는 방법


이 페이지에서

  1. 1 문제 식별
  2. 2 MySQL 복제 복구
  3. 3개의 링크

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;

이것은 슬레이브에게 하나의 쿼리를 건너뛰도록 지시합니다(이는 복제를 중지시키는 잘못된 쿼리입니다). 두 쿼리를 건너뛰려면 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 링크

  • MySQL: http://www.mysql.com