DBA

MySQL Replication 복구

엘리후 2024. 2. 14. 22:16

얼마전 회사 DB서버의 Replication이 깨지는 바람에 복구하게 됐다.


실제 운영중인 서버를 다운 시키고 백업 받을수는 없었기 때문에 최단시간동안 Write Lock을 걸고 백업을 받은 후 Write Lock을 해제하고 리플리케이션을 설정하는것이 관건 이었다.

그래서 다음과 같은 작업을 진행..

/usr/local/mysql/bin/mysqladmin -uroot -p flush-logs
/usr/local/mysql/bin/mysqladmin -uroot -p -C "flush tables with read lock"
/usr/local/mysql/bin/mysqldump --single-transaction --all-databases --extended-insert=FALSE -c -uroot -p -R > all-databases.sql


또는

/usr/local/mysql/bin/mysqladmin -uroot -p flush-logs
/usr/local/mysql/bin/mysql -u root -p -h localhost
Passwd :
MySQL > flush tables with read lock;
MySQL > show master status\G
+------------------+----------+--------------+------------------+
| File                     | Position   | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000011  |   1024026  |                      |                          |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
# 위의 MASTER STATUS 정보는 별도로 저장해 두자.

/usr/local/mysql/bin/mysqldump --single-transaction --all-databases --extended-insert=FALSE -c -uroot -p -R > all-databases.sql

이제 백업된 파일이 생성이 되었다.
그렇다면, 슬레이브 서버에 접속하여 백업된 파일을 업로드 하고 Slave 셋팅 설정을 확인한다.

server-id = 2

master-host = 마스터서버IP

master-user = <계정>

master-password = <비밀번호>

셋팅값에 이상이 없으면 백업한 파일을 Restore 한다.

/etc/init.d/mysql restart
/usr/local/mysql/bin/mysql -u root -p
/usr/local/mysql/bin/mysql -u root -p < all-databases.sql

복구 작업이 모두 완료 되면 다음 명령어를 통해 recplication을 실행한다.

mysql> stop slave;
mysql> reset slave;
mysql> CHANGE MASTER TO MASTER_LOG_FILE="mysql-bin.000011", MASTER_POS='1024026';
mysql> start slave;
mysql> show slave status\G
            Slave_IO_State: Waiting for master to send event
               Master_Host: 마스터서버IP
               Master_User: repl
               Master_Port: 3306
             Connect_Retry: 60
           Master_Log_File: mysql-bin.000011
       Read_Master_Log_Pos: 1024026
            Relay_Log_File: mysqld-relay-bin.000002
             Relay_Log_Pos: 402402
     Relay_Master_Log_File: mysql-bin.000011
          Slave_IO_Running: Yes
         Slave_SQL_Running: Yes
           Replicate_Do_DB:
       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: 1024026
           Relay_Log_Space: 402402
           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)


출력된 데이터 중 Slave_IO_State가 Waiting for master to send event와 같으면 정상적인 리플리케이션이 동작하는 것이다.

만약 DB데이터의 양이 너무 많아 mysqldump로 백업 및 복구에 시간이 많이 소요된다면, MySQL의 MASTER/SLAVE 서버간의 버전이 동일하다는 가정하에, DB_DATA폴더를 통째로 압축하여 전송한뒤 압축 해제를 통해 복사하는것도 좋은 방법일 수 있다.

 

해당 출처

http://blog.lovetonight.net/106