본문으로 바로가기
728x90
반응형
SMALL

DB Replication 해보기 (master-slave) by 도커 컴포즈

docker-compose.yml

version: '3'

services:
  master_mysql:
    container_name: master-db
    image: mysql:8.0.34
    hostname: master-db
    volumes:
      - /apps/replica_db/master/data:/var/lib/mysql:rw
      - /apps/replica_db/master/cnf:/etc/mysql/conf.d
    environment:
      MYSQL_ROOT_PASSWORD: master
      MYSQL_DATABASE: uam
      MYSQL_USER: uam
      TZ: Etc/GMT
    ports:
      - 7306:3306
    command:
      - --default-authentication-plugin=mysql_native_password
      - --character-set-server=utf8mb3
      - --lower_case_table_names=1
      - --sql-mode=

  slave_mysql:
    container_name: slave-db
    image: mysql:8.0.34
    volumes:
      - /apps/replica_db/slave/data:/var/lib/mysql:rw
      - /apps/replica_db/slave/cnf:/etc/mysql/conf.d
    environment:
      MYSQL_ROOT_PASSWORD: slave
      MYSQL_DATABASE: uam
      MYSQL_USER: uam
      TZ: Etc/GMT
    ports:
      - 7307:3306
    links:
      - master_mysql
    command:
      - --default-authentication-plugin=mysql_native_password
      - --character-set-server=utf8mb3
      - --lower_case_table_names=1
      - --sql-mode=

각자 /apps/replica_db/master/cnf /apps/replica_db/slave/cnf 경로에

config_file.cnf 파일을 master, slave 전용 2개를 준비한다.

## master
[mysqld]
server-id=1
## slave
[mysqld]
server-id=2

master db의 bin 로그 확인

$ docker exec -it master-db /bin/sh

$ mysql -uroot -p master

mysql> SHOW MASTER STATUS\G

*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 157
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

master db의 server-id 확인

mysql> SHOW VARIABLES LIKE 'server_id';

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+

master db 에서replica용 user 생성

mysql> create user 'replica'@'%' IDENTIFIED BY 'replica';

mysql> grant replication slave on *.* to 'replica'@'%';

mysql> use mysql;

mysql> select user, host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| replica          | %         |
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+

master db 에서replica용 table 생성

mysql> create table test (no int(8), primary key(no));
mysql> desc test;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| no    | int  | NO   | PRI | NULL    |       |
+-------+------+------+-----+---------+-------+

master 의 db dump 하기

$ mysqldump -uroot -p uam > dump.sql

master db docker의 dump.sql 파일을 외부로 이동

$ docker cp master-db:dump.sql .

dump.sql 파일을 slave db docker 내부로 이동

$ docker cp dump.sql slave-db:.

slave db 에 dump.sql 적용하기

$ docker exec -it slave-db /bin/bash

$ mysql -u root -p uam < dump.sql

$ mysql -uroot -p slave

mysql> use uam

mysql> show tables;
+---------------+
| Tables_in_uam |
+---------------+
| test          |
+---------------+

slave db의 server-id 확인

mysql> SHOW VARIABLES LIKE 'server_id';

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+

master 서버에서 바이너리 로그파일 확인

$ docker exec -it master-db /bin/sh

$ mysql -uroot -p master

mysql> SHOW MASTER STATUS\G

*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 1094
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

이전 바이너리 로그와 달리 변경된 것을 확인할 수 있다.

File, Position 값은 replication 적용시 옵션 값으로 사용되니 기억하자.

slave 서버에서 master 서버와 연결하기

$ docker exec -it slave-db /bin/sh

$ mysql -uroot -p slave
mysql> change master to master_host='master-db', master_user='replica', master_password='replica', master_log_file='mysql-bin.000003', master_log_pos=1094
  • master-db: 컴포즈에 있는 master-db의 hostname을 기입한다.
  • master_user /master_password: master-db에서 replication 용으로 만든 정보를 기입한다.
  • master_log_file / master_log_pos: master-db의 바이너리 File 과 Position 값을 기입한다.
mysql> start slave;

mysql> SHOW SLAVE STATUS\G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: master-db
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1094
               Relay_Log_File: e309dfc2e8e5-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000003
             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: 1094
              Relay_Log_Space: 543
              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
                  Master_UUID: b11398d2-141c-11ef-acd0-0242ac170002
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:

아래 Error 값이 0이라면 정상적으로 반영이 된것이다.

                   ...
                   Last_Errno: 0
                   Last_Error:
                   ...
                   Last_IO_Errno: 0
                   Last_IO_Error:
                   ...

Replicaion 테스트

master db에 데이터 입력

$ docker exec -it master-db /bin/sh

$ mysql -uroot -p master

mysql> use uam;

mysql> insert into test values(1);

mysql> select * from test;
+----+
| no |
+----+
|  1 |
+----+

slave db에서 데이터가 동기화 됬는지 확인

$ docker exec -it slave-db /bin/sh

$ mysql -uroot -p slave

mysql> use uam;

mysql> insert into test values(1);

mysql> select * from test;
+----+
| no |
+----+
|  1 |
+----+
728x90
반응형
LIST