개발/Mysql

[Mysql] Master-Slave 이중화

케비 2025. 2. 13. 12:06

DB 준비

우선, 총 2대의 서버가 필요하기 때문에 각각의 서버에 mysql을 설치합니다.

apt install mysql-server

 

그 후 mysql 에 접속이 잘 되는지 확인합니다.

mysql -u root

 

참고사항

Master DB Server IP : M.M.M.M

Slave DB Server IP : S.S.S.S

 

 

Master 서버 기본 설정

1. Master 서버에 my.cnf 파일을 수정합니다.

nano /etc/my.conf

아래의 내용을 작성합니다.

[mysqld]
bind-address = 0.0.0.0 

server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 90

 

2. Master 서버의 mysql 서비스를 재시작합니다.

systemctl restart mysql

 

3. Master 서버의 mysql에 slave 서버 ip 주소의 사용자 정보를 등록합니다.

CREATE USER 'repl'@'S.S.S.S' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'S.S.S.S';

 

4. Master 서버의 status 정보를 조회합니다.

SHOW BINARY LOG STATUS
mysql> SHOW BINARY LOG STATUS \G
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 621
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

 

 

Slave 서버 기본 설정

1. Slave 서버의 my.cnf 파일을 수정합니다.

nano /etc/my.conf

아래의 내용을 작성합니다.

[mysqld]
server-id = 2

 

2. Slave 서버의 mysql을 재시작합니다.

systemctl restart mysql

 

3. Slave 서버에 Master 서버에서 조회했던 정보를 입력합니다.

mysql 접속 후,

아까 SHOW BINARY LOG STATUS 때 보였던 정보들을

SOURCE_LOG_FILE 과 SOURCE_LOG_POS 에 각각 넣어주고 master의 공인IP 주소, 패스워드 등 정보를 입력해줍니다.

CHANGE REPLICATION SOURCE TO SOURCE_HOST='M.M.M.M', SOURCE_LOG_FILE='mysql-bin.000003', SOURCE_LOG_POS=621, SOURCE_USER='repl', SOURCE_PASSWORD='password', GET_SOURCE_PUBLIC_KEY=1;

 

4. Slave 서버에 해당 이중화 서버를 시작합니다.

START REPLICA;

 

5. Slave 서버에서 이중화가 잘 되었는지 확인합니다.

mysql> SHOW REPLICA STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: M.M.M.M
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 621
               Relay_Log_File: mysql-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: 621
              Relay_Log_Space: 541
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: Yes
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
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: 1
                  Master_UUID: 073e22b6-ed72-11ee-99bf-f220cd9b4d72
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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)

 

상태 확인 방법

Slave 서버에서 아래 명령어를 입력하고 Running이 둘 다 Yes 인지 확인하세요.

SHOW REPLICA STATUS\G;
Replica_IO_Running: Yes
Replica_SQL_Running: Yes

 

이후, Master 서버에서 테이블을 생성, 삭제하거나 데이터를 생성, 삭제하게 될 경우 Slave 서버에도 똑같이 적용되는지 확인하면 끝입니다!