사전 준비 사항
이 설정을 시작하기 전에 기본 MySQL 마스터-슬레이브 복제 개념을 숙지해야 합니다. 본 가이드는 기존 단방향 복제 환경을 양방향으로 확장하는 방법을 다룹니다.
환경 구성:
- 첫 번째 서버: 192.168.153.130 (기존 마스터)
- 두 번째 서버: 192.168.153.131 (기존 슬레이브)
1. 기존 슬레이브 서버에서 복제 계정 생성
192.168.153.131 서버에 접속하여, 192.168.153.130이 슬레이브로 연결할 수 있도록 복제 전용 계정을 생성합니다.
mysql -u root -p
mysql> GRANT REPLICATION SLAVE, RELOAD, SUPER ON *.*
-> TO 'replica_user'@'192.168.153.130'
-> IDENTIFIED BY 'your_password';
Query OK, 0 rows affected (0.00 sec)
2. 기존 마스터 서버에서 슬레이브 설정
192.168.153.130 서버에서 현재 바이너리 로그 위치를 확인한 후, 192.168.153.131을 마스터로 지정하여 복제를 시작합니다.
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 358 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.153.131',
-> MASTER_USER='replica_user',
-> MASTER_PASSWORD='your_password',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=358,
-> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.05 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.153.131
Master_User: replica_user
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 275
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 420
Relay_Master_Log_File: mysql-bin.000001
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: 275
Relay_Log_Space: 576
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
3. 양방향 복제 확인
설정이 완료되면 각 서버에서 데이터를 추가하여 양방향 동기화가 정상적으로 작동하는지 테스트합니다. 예를 들어, 192.168.153.130에서 INSERT를 실행하면 해당 데이터가 192.168.153.131에 자동으로 복제되고, 반대 방향도 동일하게 동작합니다.
이제 MySQL 양방향 마스터-슬레이브 복제 설정이 완료되었습니다. 읽기/쓰기 분리(Read/Write Splitting)가 필요하다면, 애플리케이션 레벨에서 프록시 또는 라우팅 계층을 추가하여 구현할 수 있습니다.