본문 바로가기

리눅스

MySQL Replication 설정하는 방법

반응형

MySQL Replication 설정하는 방법

MySQL Replication은 한 서버(Master)의 데이터를 다른 서버(Slave)로 실시간 복제하여 고가용성(HA), 백업, 읽기 분산(Read Scalability) 등을 실현할 수 있게 해줍니다.

Master 서버 설정

1. 환경설정(/etc/my.cnf)

[root@svm101 ~]$ vi /etc/my.cnf
...
[mysqld]
...
#log setting
log-bin = mysql-bin		# 바이너리 로그 파일 이름
max_binlog_size = 100M		# 바이너리 로그 파일 최대 크기
expire_logs_days = 7		# 로그 보존 기간 (일 단위) 

#Replication for master server
server-id = 3			# 고유 서버 ID
binlog_do_db = racktables_db	# 복제할 DB (여러 개 지정 가능)
#binlog_do_db = zabbix_db	# 추가 복제 대상 DB
#binlog_ignore_db = test2	# 복제 제외할 DB 지정 가능

2. MySQL 재시작(MasterServer)

[root@svm101 ~]$ service mysqld restart

3. 복제 계정 생성 및 Master 상태 확인

[root@svm101 ~]$ mysql -u root -p
Enter password:

복제 전용 계정 생성

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl';
FLUSH PRIVILEGES;

데이터 일관성을 위한 잠금(DB Write 금지)

FLUSH TABLES WITH READ LOCK;

현재 마스터 상태 확인

SHOW MASTER STATUS;
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW MASTER STATUS;
+------------------+----------+----------------------+------------------+
| File             | Position | Binlog_Do_DB         | Binlog_Ignore_DB |
+------------------+----------+----------------------+------------------+
| mysql-bin.000001 |   873110 | zabbix,racktables_db |                  |
+------------------+----------+----------------------+------------------+
1 row in set (0.00 sec)

File과 Position 값은 Slave 설정 시 필요하니 기록해 둡니다.

4. 데이터 백업

[root@svm101 ~]$ mysqldump -u root -p zabbix_db > Backup_zabbix.sql
Enter password:
[root@svm101 ~]$ mysqldump -u root -p racktables_db > Backup_racktables_db.sql
Enter password:

파일 확인

[root@svm101 ~]$ ls -l | grep sql
-rw-r--r--  1 root root  262824 2014-04-09 00:43 Backup_racktables_db.sql
-rw-r--r--  1 root root 2027131 2014-04-09 00:43 Backup_zabbix.sql

5. 테이블 잠금 해제(DB Write 금지해제)

UNLOCK TABLES;
SHOW MASTER STATUS\G
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
 
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 895793
    Binlog_Do_DB: zabbix,racktables_db
Binlog_Ignore_DB:
1 row in set (0.00 sec)
728x90

Slave 서버 설정

1. 환경설정(/etc/my.cnf)

[root@cvm121 ~]$ vi /etc/my.cnf
...
[mysqld]
...
#Replication for master server
server-id = 4			# 고유 서버 ID
replicate-do-db = zabbix_db	# 복제할 DB (여러 개 지정 가능)
replicate-do-db = racktables_db	# 추가 복제 대상 DB

2. Slave에서 DB 생성 및 데이터 복원

[root@cvm121 ~]$ mysql -u root -p
Enter password:
create database zabbix;
create database racktables_db;
mysql> create database zabbix;
Query OK, 1 row affected (0.00 sec)

mysql> create database racktables_db;
Query OK, 1 row affected (0.00 sec)

백업 데이터 복원

[root@cvm121 ~]$ mysql -u root -p zabbix_db < Backup_zabbix_db.sql
Enter password:
[root@cvm121 ~]$ mysql -u root -p racktables_db < Backup_racktables_db.sql
Enter password:

3. Master와 연결 설정

Master 정보 입력

CHANGE MASTER TO 
  MASTER_HOST='192.168.0.100', 
  MASTER_USER='repl', 
  MASTER_PASSWORD='repl', 
  MASTER_PORT=3306, 
  MASTER_LOG_FILE='mysql-bin.000001', 
  MASTER_LOG_POS=846712;

Slave 시작

stop slave;
flush privileges;
start slave;

MASTER_LOG_FILE과 MASTER_LOG_POS는 Master에서 확인한 SHOW MASTER STATUS 값을 사용해야 합니다.

4. Slave 상태 확인

SHOW SLAVE STATUS\G

정상 상태

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.100', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=846712;
Query OK, 0 rows affected (0.01 sec)

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 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.0.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 871719
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 25258
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: zabbix,racktables_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: 871719
              Relay_Log_Space: 25414
              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)
mysql>

 

참고URL

- http://juhyunsik.blogspot.kr/2012/05/mysql-replication.html

- http://www.zosel.net/entry/MySQL-Replication-%EC%84%A4%EC%A0%95Master-Slave-Master-Master

 

반응형