mysql replication error - 복제 오류 복구 하기

#slave에서 로그 확인
> show status slave;
+----------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+
| Slave_IO_State | Master_Host    | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File          | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error                                                                                                                               | Last_SQL_Errno | Last_SQL_Error |
+----------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+
|                | 192.168.10.1 | repluser    |        3306 |            60 | mysql-bin.000028 |           967473320 | mysqld-relay-bin.000075 |     967473465 | mysql-bin.000028      | No               | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            1 |           967473320 |       967473664 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                  NULL | No                            |          1236 | Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible position' |              0 |                |
+----------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+

#에러 스킵 시도
> stop slave;SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;start slave;
--> 복제 오류 지속됨.


#현재 master binlog 포지션 이후 로그 기록 확인
> show binlog events in 'mysql-bin.000028' from 967473320 limit 3\G;
Empty set (0.00 sec)

ERROR:
No query specified
--> 데이타가 없다고 함


#현재 binlog파일의 마지막 로그 추출후 확인
> mysqlbinlog --no-defaults --database=daara_db --start-date="2021-10-18 04:30:00" --stop-date="2021-10-18 04:40:00" /var/lib/mysql/mysql-bin.000028 > binlog1018.sql
--> 최종 로그 포지션 안맞음.(더이상없음)
#새로운 binlog파일의 시작 체크
> mysqlbinlog --no-defaults --database=daara_db --start-date="2021-10-18 04:30:00" --stop-date="2021-10-18 04:40:00" /var/lib/mysql/mysql-bin.000029 > binlog1018.sql
--> 해당 시작 포지션 확인후 slave에서 master position 변경

--Slave
> STOP SLAVE;
> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000029', MASTER_LOG_POS=4;
> START SLAVE;
> SHOW SLAVE STATUS\G; -- 에러확인
--> 복제 정상 동작

 

 

 

잘 진행되던 복제가 오류 났을때, 전에는 일반적으로 "SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1"를 실행해서 해결하고는 했다. 그런데 이번에는 그렇게 되지 않아 좀 애먹었다. 

위에서 보는거 처럼, 오류난 로그 포지션을 스킵을 해줘도 안되서, 현재 오류나는 곳의 binlog파일과, master position을 확인해서 해당 binlog파일을 열어 최종 로그를 확인해보니, 오류나는 binlog에는 해당 포지션이 존재하지 않았다.

아마도, 서버의 오류로 인해 binlog 기록이 잘못되었던거 같다.

 

binlog파일들을 확인해서 현재 오류나는 binlog 다음 파일을 확인 해서 position을 변경해주고 시작하니 이상 없이 복제가 잘되었다.

 

 

mysql binlog에서 특정 위치(position) 읽어보기

mysql 서버 쌓이는 binlog를 특정 position에서 최근 로그를 읽어보자.

 

mysql> show binlog events in 'mysql-bin.000011' from 850378398 limit 3\G;

*************************** 1. row ***************************
   Log_name: mysql-bin.000011
        Pos: 850378398
 Event_type: Intvar
  Server_id: 154
End_log_pos: 850378426
       Info: INSERT_ID=6631643
*************************** 2. row ***************************
   Log_name: mysql-bin.000011
        Pos: 850378426
 Event_type: Query
  Server_id: 154
End_log_pos: 850378632
       Info: use `db`; insert LOW_PRIORITY into log (admin_id, admin_log, remote_addr, im_log_seq) values ('best','message:view','61.250.146.69', 727044)
*************************** 3. row ***************************
   Log_name: mysql-bin.000011
        Pos: 850378632
 Event_type: Intvar
  Server_id: 154
End_log_pos: 850378660
       Info: INSERT_ID=679994370
3 rows in set (0.00 sec)

 

 

위와 같이 하면 결과가 검색하는 포지션 위치에서 최근 3개의 로그를 읽을 수 있다.

특정 포지션에서 로그를 확인할때 사용할수 있겠다.

 

 

* binlog - mysql binary log는 DDL(create, drom, alter)문과, DML(insert, update, delete)를 통해 수행되는 db query 문들의 정보를 저장한다고 보면 된다. 단, show 나 select문은 저장되지 않는다. 

이 binlog는 DB를 복제(replication)하거나, 특정 시점에 있었던 query를 확인하고 복구하는데도 사용이 가능하다.

 

아래 해당 내용이 잘 소개된 이웃글이 있으니 상세한 부분이 궁금하면 방문해길..

https://myinfrabox.tistory.com/20