웹사이트 검색

Debian 8에서 MySQL로 마스터-마스터 복제 설정(Jessie)


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • 데비안 8(제시)
  • 데비안 4(에치)

이 페이지에서

  1. 1 서문
  2. 2 MySQL 5.5 설치
  3. 3가지 참고 사항\n
  4. 4 복제 설정
  5. 5 복제 테스트
  6. 6개의 링크

이 자습서에서는 동시에 두 노드에서 데이터를 읽고 쓸 수 있는 2개의 노드가 있는 복제된 MySQL 설정(Mater/Master 복제)에 대해 설명합니다. MySQL은 데이터를 다른 노드에 복제하고 기본 자동 증분 키가 충돌하지 않도록 주의합니다.

버전 5부터 MySQL은 마스터-마스터 복제를 기본적으로 지원하여 자체 생성 키로 발생할 수 있는 문제를 해결합니다. 이전 MySQL 버전에서 마스터-마스터 복제의 문제는 노드 A와 노드 B가 모두 동일한 테이블에 자동 증가 키를 삽입하면 즉시 충돌이 발생한다는 것입니다. 기존의 마스터-슬레이브 복제에 비해 마스터-마스터 복제의 장점은 마스터에만 쓰기 액세스를 허용하도록 애플리케이션을 수정할 필요가 없으며 마스터가 실패하더라도 여전히 다른 마스터가 있습니다.

1 서문

이 튜토리얼에서는 IP 주소가 192.168.1.101인 서버 server1.example.com에서 IP 주소가 192.168.1.102인 서버 server2.example.com으로 exampledb 데이터베이스를 복제하는 방법을 보여줍니다. 각 시스템은 다른 마스터의 슬레이브인 동시에 다른 슬레이브의 마스터입니다. 두 시스템 모두 데비안 8을 실행하고 있습니다. 그러나 구성은 거의 또는 전혀 수정하지 않고 거의 모든 배포판에 적용되어야 합니다.

2 MySQL 5.5 설치

MySQL이 server1 및 server2에 아직 설치되지 않은 경우 지금 설치하십시오.

apt-get -y install mysql-server-5.5 mysql-client-5.5

복제가 작동할 수 있도록 하려면 MySQL이 모든 인터페이스에서 수신 대기하도록 해야 하므로 /etc/mysql/my.cnf에서 bind-address = 127.0.0.1 행을 주석 처리합니다.

서버1/서버2:

nano /etc/mysql/my.cnf
[...]
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
[...]

나중에 MySQL을 다시 시작하십시오.

서버1/서버2:

service mysql restart

그런 다음 확인

서버1/서버2:

netstat -tap | grep mysql

MySQL은 실제로 모든 인터페이스에서 수신 대기 중입니다.

netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 15437/mysqld
server1:~#

이제 server1의 MySQL 데이터베이스에 액세스하기 위해 server2에서 사용할 수 있는 복제 사용자 slave2_user를 설정합니다.

서버1:

MySQL 셸에 로그인합니다.

mysql --defaults-file=/etc/mysql/debian.cnf

MySQL 셸에서 다음 명령을 실행합니다.

서버1:

GRANT REPLICATION SLAVE ON *.* TO '%' IDENTIFIED BY 'secretpassword';
FLUSH PRIVILEGES;
quit;

"secretpassword"라는 단어를 선택한 보안 암호로 바꾸십시오. 이제 server2에서 마지막 두 단계를 다시 수행합니다.

mysql --defaults-file=/etc/mysql/debian.cnf
GRANT REPLICATION SLAVE ON *.* TO '%' IDENTIFIED BY 'secretpassword';
FLUSH PRIVILEGES;
quit;

여기서도 "secretpassword"라는 단어를 보안 암호로 바꾸십시오. 나중에 필요할 때 암호를 기록해 둡니다.

3 참고 사항

다음에서는 두 MySQL 서버가 모두 비어 있다고 가정합니다(mysql 데이터베이스를 제외하고 아직 어떤 데이터베이스도 포함하지 않음).

서버에서 그렇지 않은 경우 계속하기 전에 첫 번째 서버에서 데이터베이스를 잠그고 덤프하고 두 번째 서버에서 가져와야 합니다. 복제가 설정되기 전에 데이터베이스를 잠금 해제하지 마십시오. 다음은 "깨끗한" MySQL 설정으로 시작하지 않는 경우 모든 데이터베이스를 새 서버로 복사하는 방법을 보여주는 몇 가지 명령입니다.

MySQL 데이터베이스의 모든 데이터베이스 테이블을 잠그는 방법에 대한 예입니다.

FLUSH TABLES WITH READ LOCK;
SET GLOBAL read_only = ON;

모든 데이터베이스를 all_databases.sql 파일로 덤프하는 방법에 대한 예입니다.

mysqldump --defaults-file=/etc/mysql/debian.cnf -cCeQ --hex-blob --quote-names --routines --events --triggers --all-databases -r all_databases.sql

all_databses.sql 파일에서 두 번째 서버의 모든 테이블을 가져오는 방법에 대한 예입니다.

mysql --defaults-file=/etc/mysql/debian.cnf < all_databases.sql

4 복제 설정

이제 /etc/mysql/my.cnf에서 마스터-마스터 복제를 설정합니다. 마스터-마스터 복제를 위한 중요한 구성 옵션은 auto_increment_increment 및 auto_increment_offset입니다.

  • auto_increment_increment는 연속적인 AUTO_INCREMENT 값 사이의 증분을 제어합니다.\n
  • auto_increment_offset은 AUTO_INCREMENT 열 값의 시작점을 결정합니다.\n

N개의 MySQL 노드(이 예에서는 N=2)가 있다고 가정하면 auto_increment_increment는 모든 노드에서 N 값을 가지며 각 노드는 auto_increment_offset(1, 2, ..., N)에 대해 서로 다른 값을 가져야 합니다.

이제 두 개의 MySQL 노드를 구성하겠습니다.

nano /etc/mysql/my.cnf

[mysqld]로 시작하는 섹션을 검색하고 다음 옵션을 입력합니다(기존의 모든 충돌 옵션은 주석 처리).

[...]
[mysqld]

# Unique Server ID
server-id = 1

# Do not replicate the following databases
binlog-ignore-db = mysql
replicate-ignore-db = mysql

# Auto increment offset
auto-increment-increment = 2

# Do not replicate sql queries for the local server ID
replicate-same-server-id = 0

# Beginne automatisch inkrementelle Werte mit 1
auto-increment-offset = 1

# Delete binlog data after 10 days
expire_logs_days = 10

# Max binlog size
max_binlog_size = 500M

# Binlog file path
log_bin = /var/log/mysql/mysql-bin.log

[...]

그런 다음 MySQL을 다시 시작합니다.

서버1:

service mysql restart

이제 server2에서 동일한 작업을 수행합니다.

nano /etc/mysql/my.cnf
[...]

# Unique Server ID
server-id = 2

# Do not replicate the following databases
binlog-ignore-db = mysql
replicate-ignore-db = mysql

# Auto increment offset
auto-increment-increment = 2

# Do not replicate sql queries for the local server ID
replicate-same-server-id = 0

# Beginne automatisch inkrementelle Werte mit 1
auto-increment-offset = 2

# Delete binlog data after 10 days
expire_logs_days = 10

# Max binlog size
max_binlog_size = 500M

# Binlog file path
log_bin = /var/log/mysql/mysql-bin.log

[...]

서버2:

service mysql restart

다음으로 우리는 server1의 exampledb 데이터베이스를 잠그고, server1의 마스터 상태에 대해 알아보고, exampledb의 SQL 덤프를 생성하고(두 데이터베이스가 동일한 데이터를 포함하도록 server2의 exampledb로 가져올 것임), 데이터베이스 잠금을 해제합니다. 다시 사용할 수 있습니다:

이제 서버 2에서 복제를 시작합니다. MySQL 셸을 엽니다.

mysql --defaults-file=/etc/mysql/debian.cnf

그리고 다음 SQL 명령을 실행하여 server1에서 server2로의 복제를 활성화합니다.

CHANGE MASTER TO MASTER_HOST='192.168.1.101', MASTER_USER='repl', MASTER_PASSWORD='secretpassword';

secretpassword를 2장에서 설정한 repl MySQL 사용자의 비밀번호로 바꾸세요.

이제 MySQL 쉘에서 "show slave status\\G" 명령을 실행하여 슬레이브 상태를 확인하십시오.

show slave status\G

출력은 다음과 유사합니다.

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.101
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: mysqld-relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
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: 107
Relay_Log_Space: 410
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
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
1 row in set (0.00 sec)

확인해야 하는 라인은 다음과 같습니다.

Master_Host: 192.168.1.101
Master_User: repl
Master_Port: 3306
Master_Log_File: mysql-bin.000001
Relay_Log_File: mysqld-relay-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: No

이제 MySQL 셸에서 다음 명령으로 복제를 시작합니다.

start slave;

그런 다음 슬레이브 상태를 다시 확인하십시오.

show slave status\G

이제 다음 두 줄에 "yes"가 표시되어야 합니다.

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0

"Seconds_Behind_Master"가 0이 아닌 경우 몇 초간 기다린 후 상태를 다시 확인하십시오. 이 필드는 마스터와 슬레이브가 동기화되었는지 여부를 보여줍니다.

다음 단계에서는 "show slave status\\G" 명령의 "Master_Log_File" 및 "Read_Master_Log_Pos" 값을 알아야 합니다. 제 경우에는 다음과 같습니다.

Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107

서버에서 얻은 값을 적어 두십시오. 서버 1의 다음 단계에 필요합니다.

나중에 MySQL 셸을 떠날 수 있습니다.

quit

서버1:

첫 번째 서버에서 계속해서 server1에서 MySQL 셸을 엽니다.

mysql --defaults-file=/etc/mysql/debian.cnf

그리고 다음 MySQL 명령을 실행합니다.

CHANGE MASTER TO MASTER_HOST='192.168.1.102', MASTER_USER='repl', MASTER_PASSWORD='secretpassword', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;

위 명령에서 몇 가지를 바꿔야 합니다.

  1. IP 주소는 두 번째 MySQL 서버의 IP여야 합니다.\n
  2. 비밀번호 "secretpassword"는 사용자 repl에 대해 2장에서 선택한 비밀번호여야 합니다.\n
  3. MASTER_LOG_FILE 및 MASTER_LOG_POS는 마지막 단계에서 기록한 값이어야 합니다.\n

이제 다음을 확인하십시오.

show slave status\G

오류가 없는 경우 MySQL 셸에서.

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.102
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: mysqld-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
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: 107
Relay_Log_Space: 107
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
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: 0
1 row in set (0.00 sec)

그리고 슬레이브를 시작하십시오.

start slave;

슬레이브 상태를 다시 확인하십시오.

show slave status\G

이제 다음 두 줄에 "yes"가 표시되어야 합니다.

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

나중에 MySQL 셸을 떠날 수 있습니다.

quit

아무 것도 잘못되지 않았다면 이제 MySQL 마스터-마스터 복제가 작동해야 합니다. 그렇지 않은 경우 server1 및 server2의 MySQL 오류에 대해 /var/log/syslog를 확인하십시오.

5 복제 테스트

이제 복제 설정을 테스트할 시간입니다. server1에 exampledb1 데이터베이스를 만든 다음 데이터베이스가 두 번째 서버에 복제되었는지 server2에서 확인합니다.

server1의 MySQL 콘솔에 로그인하고 데이터베이스를 생성합니다.

mysql --defaults-file=/etc/mysql/debian.cnf
CREATE DATABASE exampledb1;

서버2

이제 server2의 MySQL 콘솔에 로그인하고 exampledb1이 있는지 확인합니다.

mysql --defaults-file=/etc/mysql/debian.cnf
show databases;

보시다시피 새 데이터베이스는 server2에도 나타납니다.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| exampledb1 |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

다음으로 복제가 다른 방향에서도 작동하는지 테스트하겠습니다. 여전히 server2에 로그인되어 있으며 exampledb2 데이터베이스를 생성합니다.

CREATE DATABASE exampledb2;

이제 server1으로 돌아가서 MySQL 콘솔에서 "show databases"를 실행합니다.

서버1

show databases;

결과는 새 데이터베이스 exampledb2를 보여주므로 복제가 양방향으로 작동합니다.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| exampledb1 |
| exampledb2 |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.01 sec)

6 링크

  • MySQL: http://www.mysql.com
  • 데비안: http://www.debian.org