MySQL 복제 · 5 min read · Jan 31, 2026

같은 머신에서의 MySQL 복제

이 방법은 같은 머신에서 MySQL의 복제를 설명합니다.

대규모 트래픽을 가진 데이터베이스 기반 사이트와 성능, 중복성, 보안이 이제 떠오릅니다. DBA는 이제 행복한 얼굴을 만들기 위한 좋은 솔루션을 찾기 위해 많은 손길이 필요합니다. 그래서 클러스터, 복제, 백업, 장애 조치와 같은 단어들이 등장합니다. 그렇다면 이들은 무엇일까요? 우리는 현재 대규모 트래픽을 위한 웹 기반 데이터베이스 사이트의 복제에 대해 논의하고 있습니다.

복제란 무엇이며 왜 필요한가?

복제는 DBA가 마스터 데이터베이스를 동일한 데이터베이스 서버의 다른 데이터베이스로 복제할 수 있게 해줍니다. 여기에는 마스터와 슬레이브의 정체성이 포함됩니다. 슬레이브는 마스터 데이터베이스 서버 및 그 데이터베이스의 정확한 복사본을 만듭니다. 마스터와 슬레이브 간에는 1:1, 1:다수, 다수:1의 관계가 있을 수 있습니다. 슬레이브는 마스터에서 이진 로그를 지속적으로 읽고(이진 로그는 마스터 데이터베이스 서버에 작성된 쿼리를 저장합니다) 슬레이브 데이터베이스 서버에 입력을 가져옵니다.

복제가 아닌 것:

백업, 성능, 보안 및 중복성에 대한 솔루션으로 고려하지 마십시오. 이를 위한 다른 기술이 있습니다.

현재 우리는 같은 머신에서 MySQL 서버의 복제를 보고 있습니다. 즉, 마스터와 슬레이브가 같은 머신에서 실행되고 있습니다. 우리는 복제와 관련된 몇 가지 문제에 대해서도 논의할 것입니다.

전제 조건:

MySQL 서버 4.1.12 이상 소스 형식. http://mysql.com에서 소스 형식으로 다운로드하십시오. 편의를 위해 http://mysql.com에서 MySQL 클라이언트를 다운로드하거나 MySQL을 환경 변수에 추가할 수 있습니다. 그렇지 않으면 MySQL을 해당 마스터/슬레이브 디렉토리에서 호출해야 합니다. 일부 리눅스 배포판. (나는 Fedora Core 2를 사용했습니다). MySQL 서버의 종속성을 제거하십시오.

MySQL 마스터 설치 및 구성:

/misc 폴더에 mysql-4.1.12 소스를 다운로드합니다.

tar xzvf mysql-4.1.12.tar.gz
cd /misc/mysql-4.1.12
./configure --prefix=/usr/local/mysql-master
make
make install
cd /usr/local/mysql-master/bin
./mysql_install_db

(변수 폴더가 생성됩니다)

cd ../var
cp /misc/mysql-4.1.12/support-files/my-medium.cnf my.cnf
cd ..
groupadd mysql
useradd -g mysql mysql
chown -R root .
chown -R mysql var
chgrp -R mysql .
[mysqld]  

port = 3306  

socket = /usr/local/mysql-master/mysql.sock  

  

#skip-networking // 우리는 같은 머신에서 마스터와 슬레이브를 수행하고 있으므로 이를 건너뜁니다.  

  

# 복제 마스터 서버 (기본값)  

# 복제를 위해 이진 로깅이 필요합니다.  

log-bin  

  

# 1과 2^32 - 1 사이의 고유 ID가 필요합니다.  

# master-host가 설정되지 않으면 기본값은 1입니다.  

# 그러나 생략하면 마스터로 작동하지 않습니다.  

server-id = 1  

필요에 따라 다른 설정을 구성하거나 그대로 두십시오. 작동할 것입니다!! :)
이제 MySQL 서버를 시작합니다:

cd /usr/local/mysql-master/bin
./mysqld_safe --defaults-file=/usr/local/mysql-master/var/my.cnf &;

MySQL 슬레이브:

이제 다른 디렉토리에 mysql-4.1.12.tar.gz를 추출합니다.

cd /opt/mysql-4.1.12
./configure --prefix=/usr/local/mysql-slave
make
make install
cd /usr/local/mysql-slave
cd bin
./mysql_install_db

(변수 폴더가 생성됩니다)

cd ../var
cp /opt/mysql-4.1.12/support-files/my-medium.cnf my.cnf
cd ..
groupadd mysql
useradd -g mysql mysql
chown -R root .
chown -R mysql var
chgrp -R mysql .

var 폴더의 my.cnf를 편집합니다.

[mysqld]  

port  = 3307  

socket  = /usr/local/mysql-slave/var/mysql.sock  

  

#skip-networking  

  

server-id = 2  

  

# 이 슬레이브의 복제 마스터 - 필수  

master-host = localhost  

master-user = slavedb  

master-password = q1w2e3r4t5  

master-port = 3306  

이제 MySQL 서버를 시작합니다:

cd /usr/local/mysql-slave/bin
./mysqld_safe --defaults-file=/usr/local/mysql-slave/var/my.cnf &

복제 구성:

MySQL 마스터에 연결합니다:

mysql --sock=/usr/local/mysql-master/mysql.sock

슬레이브를 위한 마스터에서 계정 생성:

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slavedb@"192.168.1.27/255.255.255.0" identified by 'q1w2e3r4t5'; Query OK, 0 rows affected (0.28 sec)

MySQL 슬레이브에 연결합니다:

mysql --sock=/usr/local/mysql-slave/mysql.sock
mysql> slave start;

Query OK, 0 rows affected, 1 warning (0.04 sec)

테스트:

마스터에서 MySQL 연결:

mysql> show master status\G;
************************* 1. row ***********************  
 File: adam-bin.000001  
 Position: 227  
 Binlog_Do_DB:  
Binlog_Ignore_DB:  
1 row in set (0.01 sec)  
  
ERROR:  
No query specified  

슬레이브에서 MySQL 연결:

mysql> show slave status\G;
*********************** 1. row *************************
 Slave_IO_State: Connecting to master  
 Master_Host: localhost  
 Master_User: slavedb  
 Master_Port: 3306  
 Connect_Retry: 60

Master_Log_File: adam-bin.000001

 Read_Master_Log_Pos: 4  
 Relay_Log_File: adam-relay-bin.000001  
 Relay_Log_Pos: 4  
 Relay_Master_Log_File: adam-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: 4  
 Relay_Log_Space: 4  
 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  
1 row in set (0.00 sec)  
  
ERROR:  
No query specified  

마스터의 binlog 파일과 그 위치는 MySQL 슬레이브에서 show slave status\G로 동일해야 합니다.
모든 것이 정상이라면, 당신은 같은 시스템에서 1:1 마스터와 슬레이브 관계의 작동 복사본을 실행하고 있습니다.

몇 가지 문제:

복제는 여러 문제로 인해 실패할 수 있습니다. 나는 복제와 관련된 몇 가지 경험을 공유합니다.

문제: 복제 실패, 마스터 다운.

마스터는 여러 이유로 다운될 수 있습니다. 데이터베이스의 파일 제한, SQL 쿼리 및 디스크 사용량을 확인하십시오. 어떤 이유라도 나타나면 수정하고 mysqld를 재시작한 후 마스터 상태를 확인하십시오:

mysql> show master status\G;

* 1. row *

adam-bin.000003

Position: 227

Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec)

이제 슬레이브를 확인하십시오: show slave status로:

mysql> show slave status\G;

* 1. row *
Slave_IO_State: Connecting to master
Master_Host: localhost
Master_User: slavedb
Master_Port: 3306
Connect_Retry: 60

Master_Log_File: adam-bin.000001

Read_Master_Log_Pos: 4

Relay_Log_File: adam-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: adam-bin.000001

Slave_IO_Running: No

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: 4
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
1 row in set (0.00 sec)

위의 값은 마스터가 3번째 binlog로 증가했지만 슬레이브는 여전히 binlog1을 가리키고 있음을 보여줍니다. 따라서 MySQL 슬레이브에 연결하여 이 값을 변경하십시오.

mysql>; stop slave;
mysql> change master to master_log_file='adam-bin.000003', master_log_pos=227;
mysql> start slave;

이제 show slave status\G로 확인하십시오. 잘 작동하고 있습니다.

문제 2: 슬레이브에서 중복 오류 키가 나타나는 경우

즉, 슬레이브에서

Mysql> show slave status\G;

* 1. row *
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.152
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: adam-bin.000048
Read_Master_Log_Pos: 317714810
Relay_Log_File: db4-relay-bin.000001
Relay_Log_Pos: 290512385
Relay_Master_Log_File: adam-bin.000048
Slave_IO_Running: Yes

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: 1062

Last_Error: Error ‘Duplicate entry ‘dekq5g820avnfdmar5qi9dkhv3’ for key 1’ on query. Default database:’session_sql’. Query: ‘INSERT INTO sessi ons5 VALUES (‘dekq5g820avnfdmar5qi9dkhv3’, UNIX_TIMESTAMP(NOW()) + 18000, ‘redir ect|i:1;’)’

Skip_Counter:0
Exec_Master_Log_Pos: 290512419
Relay_Log_Space: 317714776
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
1 row in set (0.00 sec)

이는 마스터의 가용성이 없거나 슬레이브의 가용성이 없을 때, 쿼리가 두 개의 기본 키를 찾았음을 의미합니다. 이는 혼란을 초래합니다.

따라서 이를 수정하는 방법은: 슬레이브에서:

Mysql> set global sql_slave_skip_counter=1;
Mysql> start slave;
Mysql> show slave status\G;

이제 마스터와 동기화된 값을 보여줍니다. 수정되었습니다.

Share: X/Twitter LinkedIn

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

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