본문 바로가기

리눅스

[리눅스] MySQL에서 리플리케이션 다시 연결하기

728x90

MySQL에서 리플리케이션 다시 연결하기

MySQL 리플리케이션의 정상 여부를 확인하는 방법

슬레이브 서버 상태 확인

show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.56.101
                  Master_User: replication_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: node2-relay-bin.000016
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 154
              Relay_Log_Space: 0
              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
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 3ef5caf4-f619-11ed-96e6-080027704bff
             Master_Info_File: /usr/local/mysql-5.7.41/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

리눅스 쉘 명령어로 한 줄로 슬레이브 서버 상태 확인

mysql -hlocalhost -uroot -p'mysqlpassword' -e "show slave status\G" | egrep 'Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_Master'
$ mysql -hlocalhost -uroot -p'mysqlpassword' -e "show slave status\G" | egrep 'Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_Master'
mysql: [Warning] Using a password on the command line interface can be insecure.
             Slave_IO_Running: No
            Slave_SQL_Running: No
        Seconds_Behind_Master: NULL
      Slave_SQL_Running_State:
  • Slave_IO_RunningSlave_SQL_Running 필드를 확인하여 I/O 스레드와 SQL 스레드가 실행 중인지 여부를 파악합니다.
  • Seconds_Behind_Master 필드를 통해 Master와 Slave 사이의 지연 시간을 확인할 수 있습니다. 값이 0이면 동기화가 완료된 것을 의미합니다.

마스터 서버에서 File, Position 정보 확인

show master status\G
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000012
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

슬레이브 서버 리플리케이션 재구성

  • 복제 중지
STOP SLAVE;
  • 복제 설정을 재설정
RESET SLAVE;
  • 새로운 복제 설정
CHANGE MASTER TO
MASTER_HOST='192.168.56.101',
MASTER_USER='replication_user',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000012',
MASTER_LOG_POS=154;
  • 슬레이브 서버 다시 시작
START SLAVE;

 

슬레이브 서버 상태 확인

mysql -hlocalhost -uroot -p'mysqlpassword' -e "show slave status\G" | egrep 'Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_Master'
$ mysql -hlocalhost -uroot -p'mysqlpassword' -e "show slave status\G" | egrep 'Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_Master'
mysql: [Warning] Using a password on the command line interface can be insecure.
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        Seconds_Behind_Master: 0
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

 

마스터 서버에서 슬레이브 호스트 확인

SHOW SLAVE HOSTS;
mysql> SHOW SLAVE HOSTS;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|         3 |      | 3306 |         1 | 3ebc580f-f8f7-11ed-adcf-080027704bff |
|         2 |      | 3306 |         1 | 168e9cb8-f8f7-11ed-993f-080027182df0 |
+-----------+------+------+-----------+--------------------------------------+
2 rows in set (0.00 sec)

 

728x90