MySQL Replication 구성(MySQL 복제)
MySQL Replication을 이용해 Master–Slave(Primary–Replica) 구조의 데이터 복제 환경을 구성하는 방법입니다.
테스트 환경
| Hostname | IP | Role | 비고 |
| node1 | 192.168.56.101 | master | |
| node2 | 192.168.56.102 | slave |
- MySQL 설치 경로 : /usr/local/mysql
- Socket : /tmp/mysql.sock
- Replication 방식 : Binary Log 기반
- Binlog Format : ROW
1. MySQL 설정 (my.cnf)
Master 서버 설정 (node1)
vim /usr/local/mysql/my.cnf
# /usr/local/mysql/my.cnf
[mysqld]
bind-address = 0.0.0.0
port = 3306
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
pid-file = /var/run/mysqld/mysqld.pid
log-error = /usr/local/mysql/data/error.log
log-error-verbosity = 3
explicit_defaults_for_timestamp = 1
symbolic-links = 0
server-id = 1
log-bin = mysql-bin
binlog-format = ROW
expire_logs_days = 2
[client]
port = 3306
socket = /tmp/mysql.sock
Slave 서버 설정 (node2)
vim /usr/local/mysql/my.cnf
# /usr/local/mysql/my.cnf
[mysqld]
bind-address = 0.0.0.0
port = 3306
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
pid-file = /var/run/mysqld/mysqld.pid
log-error = /usr/local/mysql/data/error.log
log-error-verbosity = 3
explicit_defaults_for_timestamp = 1
symbolic-links = 0
server-id = 2
log-bin = mysql-bin
binlog-format = ROW
expire_logs_days = 2
relay_log = relay-bin
log_slave_updates = 1
read_only = 1
[client]
port = 3306
socket = /tmp/mysql.sock
2. MySQL 데몬 시작/종료
시작
mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &
종료
mysqladmin -u root shutdown --socket /tmp/mysql.sock
3. Master 서버 설정 확인
Binary Log 파일 및 Position 확인
mysql -h localhost -uroot -p'mysqlpassword' -e "SHOW MASTER STATUS\G" | egrep 'File|Position'
mysql: [Warning] Using a password on the command line interface can be insecure.
File: mysql-bin.000007
Position: 154
이 값은 Slave 설정 시 반드시 필요
4. Slave 서버 설정
기존 복제 중지 및 초기화
mysql -h localhost -uroot -p'mysqlpassword' -e "STOP SLAVE;"
mysql -h localhost -uroot -p'mysqlpassword' -e "RESET SLAVE;"
Master 정보 설정
mysql -h localhost -uroot -p'mysqlpassword' -e "
CHANGE MASTER TO
MASTER_HOST='192.168.56.101',
MASTER_USER='replication_user',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000007',
MASTER_LOG_POS=154;
"
mysql: [Warning] Using a password on the command line interface can be insecure.
보안상 CLI에서 패스워드 직접 입력은 권장하지 않음 → .my.cnf 또는 환경 변수 사용 권장
Slave 복제 시작
mysql -h localhost -uroot -p'mysqlpassword' -e "START SLAVE;"
mysql: [Warning] Using a password on the command line interface can be insecure.
5. Slave 상태 확인
핵심 상태 확인
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
전체 상태 확인
mysql -hlocalhost -uroot -p'mysqlpassword' -e "SHOW SLAVE STATUS\G"
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.101
Master_User: replication_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000007
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: 154
Relay_Log_Space: 521
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 1e320772-f6c4-11ed-a93d-0800278c2c47
Master_Info_File: /usr/local/mysql/data/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:
- Last_IO_Error, Last_SQL_Error → 비어 있어야 정상
- Slave_SQL_Running_State → waiting for more updates
6. Master 서버에서 Slave 확인
mysql -hlocalhost -uroot -p'mysqlpassword' -e "SHOW SLAVE HOSTS;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 3 | | 3306 | 1 | 8802cba8-f8ab-11ed-aa0c-080027dcd0c8 |
| 2 | | 3306 | 1 | 81a1c980-f8ab-11ed-9f7e-0800273d1990 |
+-----------+------+------+-----------+--------------------------------------+
MySQL Replication은 읽기 분산, 백업, 장애 대응을 위한 가장 기본적인 고가용성 구성이다.
'리눅스' 카테고리의 다른 글
| [draft] docker nettools 컨테이너 실행 (0) | 2026.03.02 |
|---|---|
| [draft] Ansible SSH Fingerprint 접속 오류를 해결하는 방법 (0) | 2026.03.02 |
| [draft] 우분투에서 Ansible을 위한 기본 Bash 자동 완성을 설치하는 방법 (0) | 2026.03.02 |
| [draft] Ansible을 위한 기본 Bash 자동 완성을 설치하는 방법 (0) | 2026.03.02 |
| [draft] Ansible ansible-playbook 명령어를 사용하는 방법 (0) | 2026.03.02 |