[MariaDB] Replication(복제)

devvace ㅣ 2017. 12. 5. 11:46

로그 서버를 나누면서 DB를 복제해야할 일이 생김


[테스트 환경]

OS          - CentOS Linux release 7.3.1611 (Core),  64bit

Master     - mariadb-server-5.5.56-2.el7.x86_64   

Slave       - mariadb-server-5.5.56-2.el7.x86_64 



1. Master 설정


우선 MariaDB 환경설정 파일을 수정한다.

# vim /etc/my.cnf


1
2
3
4
5
6
[mysqld]
...
server-id =1
log-bin=master-bin
binlog_format=mixed
...
cs

※ 주의, [mysqld] 밑 부분에 추가해야 함, 환경 설정파일 맨 밑부분에 추가해 보면 Master 기능이 제대로 작동 안함


DB에 접속해서 Replication 사용자를 추가한다.

# mysql -u root -p

...

MariaDB [(none)]> CREATE USER 'repl'@'%' IDENTIFIED BY 'password';

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO repl IDENTIFILED BY 'password';


MariaDB를 재시작 한 후 복제할 DB의 데이터를 덤프한다.

# systemctl restart mariadb

# mysqldump -u root -p testDB > testDB.sql

※ 이 과정은 Master에 있는 기존 데이터를 Slave에 옮겨야 하는 경우에만 적용한다.


Master의 상태를 확인한다.

# mysql -u root -p

...

MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;

MariaDB [(none)]> SHOW MASTER STATUS;

1
2
3
4
5
6
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |   123256 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
cs

※ Slave의 경우 Position 값과 앞에 데이터를 덤프한 파일이 필요하다. Position 값은 변화하기 때문에 Slave에서 설정할 때 한 번 더 확인하는 작업이 필요하다.

LOCK의 경우 Replication 종료 또는 서비스 중이 아니라면 'MariaDB [(none)]> UNLOCK TABLES;' 를 통해서 LOCK을 풀면된다.


마지막으로 Slave 의 접근 권한을 허용해야 한다.

# firewall-cmd --permanent --zone=public --add-port=3306/tcp

# firewall-cmd --permanent --zone=public --add-port=3306/udp

# systemctl restart firewalld



2. Slave 설정

Slave 설정이 들어가기 전에 해야 하는 작업이 있다. 복제할 서버(Slave)에 Master 서버의 동일한 DB 또는 Table 구조를 만들어야 한다. 물론 복제할 부분만 있으면 되고 데이터까지 똑같을 필요는 없다.


마찬가지로 MariaDB 환경설정 파일을 수정한다.

# vim /etc/my.cnf


1
2
3
4
5
6
7
[mysqld]
...
server-id=2
log-bin=slave-2-bin
relay-log=relay-bin
replicate-do-db=Syslog
...
cs

※ Master 와 마찬가지로 [mysqld] 아래 부분에 수정을 해야한다.

만약 하나 이상의 DB 를 복제하려면 'replicate-do-db={DBName}' 설정을 개수만큼 추가하면 된다.

server-id 의 경우 Master 와 겹쳐서는 안된다. 다수의 Slave 가 있다면 모두 각각의 server-id 를 가지고 있어야 한다.

테이블을 복사하려면 'replicate-do-table=db.table 을 하면 된다.


Master에서 덤프한 SQL 데이터를 복구한다.

# mysql -u root -p testDB < testDB.sql


Slave에서 Master로 접속하기 위한 정보를 추가한다.

# mysql -u root -p

MariaDB [(none)]> CHANGE MASTER TO

MASTER_HOST='115.xxx.xxx.xxx',

MASTER_USER='repl',

MASTER_PASSWORD='password',

MASTER_PORT=3306,

MASTER_LOG_FILE='master-bin.000001',

MASTER_LOG_POS=123256,

MASTER_CONNECT_RETRY=10;

MariaDB [(none)]> FLUSH PRIVILEGES;

MariaDB [(none)]> START SLAVE;


작동확인

MariaDB [(none)]> SHOW SLAVE STATUS\G;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 115.xxx.xxx.xxx
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 425494
               Relay_Log_File: relay-bin.000007
                Relay_Log_Pos: 366916
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: Syslog
          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: 425494
              Relay_Log_Space: 367204
              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
1 row in set (0.00 sec)
cs


에러가 없으면, 정상적으로 INSERT 되고 있는지 SELECT 문을 통해서 확인해보면 된다.


ref) https://ncube.net/10825

tutorialbook