데이터베이스 · 3 min read · Sep 27, 2025

MySQL 마스터 마스터 복제

MySQL 마스터 마스터 복제 튜토리얼

이 튜토리얼은 MySQL 마스터-마스터 복제를 설정하는 방법을 설명합니다. 고가용성(HA)을 달성하기 위해 MySQL 서버를 복제해야 합니다. 제 경우에는 서로 동기화된 두 개의 마스터가 필요합니다. 하나가 다운되면 다른 하나가 인계받아 데이터가 손실되지 않도록 합니다. 첫 번째 마스터가 다시 올라오면, 여전히 라이브 마스터의 슬레이브로 사용됩니다.

여기 기본 단계별 튜토리얼이 있습니다. 이 튜토리얼은 MySQL 마스터 및 슬레이브 복제를 다루고, MySQL 마스터 및 마스터 복제에 대해서도 설명합니다.

개념: 시스템 1을 master1 및 slave2로, 시스템 2를 master2 및 slave1로 부르겠습니다.

1단계:

master 1과 slave 1에 MySQL을 설치합니다. 두 시스템에서 네트워크 서비스를 구성합니다, 예를 들어

Master 1/Slave 2 IP: 192.168.16.4

Master 2/Slave 1 IP: 192.168.16.5

2단계:

Master 1에서 my.cnf를 수정합니다:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
old_passwords=1

log-bin
binlog-do-db=<데이터베이스 이름> # 복제할 데이터베이스 입력
binlog-ignore-db=mysql # 복제에서 무시할 데이터베이스 입력
binlog-ignore-db=test

server-id=1

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

3단계:

master 1에서 MySQL에 복제 슬레이브 계정을 생성합니다.

mysql> grant replication slave on . to ‘replication’@192.168.16.5 \
identified by ‘slave’;

그리고 MySQL master1을 재시작합니다.

4단계:

이제 Slave1 또는 Master2에서 my.cnf를 수정합니다:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
old_passwords=1

server-id=2

master-host = 192.168.16.4
master-user = replication
master-password = slave
master-port = 3306

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

5단계:

MySQL 슬레이브 1을 재시작하고

mysql> start slave;
mysql> show slave status\G;

* 1. row *

Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.16.4
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MASTERMYSQL01-bin.000009
Read_Master_Log_Pos: 4

Relay_Log_File: MASTERMYSQL02-relay-bin.000015

Relay_Log_Pos: 3630
Relay_Master_Log_File: MASTERMYSQL01-bin.000009
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: 4
Relay_Log_Space: 3630
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: 1519187

1 row in set (0.00 sec)

위의 강조된 행은 관련 로그 파일을 나타내야 하며, Slave_IO_Running 및 Slave_SQL_Running:는 YES여야 합니다.

6단계:

master 1에서:

mysql> show master status;
+————————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+————————+———-+————–+——————+
|MysqlMYSQL01-bin.000008 | 410 | adam | |
+————————+———-+————–+——————+
1 row in set (0.00 sec)

위의 시나리오는 마스터-슬레이브에 해당하며, 이제 동일한 시스템에 대해 슬레이브 마스터 시나리오를 생성할 것이며, 이는 마스터 마스터로 작동합니다.

7단계:

Master2/Slave 1에서 my.cnf를 수정하고 마스터 항목을 추가합니다:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

old_passwords=1
server-id=2

master-host = 192.168.16.4
master-user = replication
master-password = slave
master-port = 3306

log-bin # 마스터가 되기 위한 정보 추가
binlog-do-db=adam

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

8단계:

** master1에 대한 master2에서 복제 슬레이브 계정을 생성합니다:

mysql> grant replication slave on *.* to 'replication'@192.168.16.4 identified by 'slave2';

9단계:

** master1의 마스터 정보를 위해 my.cnf를 수정합니다.

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

old_passwords=1

log-bin
binlog-do-db=adam
binlog-ignore-db=mysql
binlog-ignore-db=test

server-id=1

master-host = 192.168.16.5
master-user = replication
master-password = slave2
master-port = 3306

[mysql.server]user=mysqlbasedir=/var/lib

10단계:

두 개의 MySQL master1과 master2를 재시작합니다.

MySQL master1에서:

mysql> start slave;

MySQL master2에서:

mysql > show master status;

MySQL master 1에서:

mysql> show slave status\G;

* 1. row *
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.16.5
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: Mysql1MYSQL02-bin.000008
Read_Master_Log_Pos: 410
Relay_Log_File: Mysql1MYSQL01-relay-bin.000008
Relay_Log_Pos: 445
Relay_Master_Log_File: Mysql1MYSQL02-bin.000008
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: 410
Relay_Log_Space: 445
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: 103799
1 row in set (0.00 sec)

ERROR:
No query specified

강조된 행을 확인하여 실행 중인지 확인하십시오. 이제 데이터베이스에 테이블을 생성할 수 있으며 슬레이브에서 변경 사항을 볼 수 있습니다. 즐기세요!!

Share: X/Twitter LinkedIn

새 게시물을 받은 편지함에서 받기

스팸은 없습니다. 언제든지 구독 해지 가능합니다.