웹사이트 검색

Oracle Linux에서 MySQL 다중 마스터 복제를 구성하는 방법


이 페이지에서

  1. 1. 서문
  2. 2. 설치 단계
  3. 3. 구성 단계
  4. 4. 테스트 단계

이 자습서에서는 Oracle Linux에서 MySQL 다중 마스터 복제를 설정하고 구성하는 방법을 설명합니다. 이미 알고 계시겠지만 MySQL은 기업용으로 입증된 최고 수준의 데이터베이스 제품으로 잘 알려져 있습니다. 데이터는 모든 조직에 중요하므로 대부분의 데이터베이스 관리자는 사용자가 연중무휴 데이터에 액세스할 수 있도록 고가용성을 설정하는 데 적합한 솔루션을 찾고 있습니다. MySQL 복제는 고가용성 정책을 보장할 수 있는 솔루션입니다. 또한 MySQL 복제는 데이터베이스 관리자가 READ 및 WRITE 요청의 부하를 분산하여 여러 데이터베이스 서버에 부하를 분산하는 데 도움이 될 수 있습니다. 안타깝게도 기본 복제는 READ 요청에 대해서만 이점을 제공할 수 있습니다. 이로 인해 WRITE 요청에 대한 복제도 제공하기 위해 MySQL 다중 마스터 복제가 도입되었습니다.

1. 서문

이 튜토리얼에서는 32비트 버전의 Oracle Linux 6.8을 사용하고 있습니다. 구성은 Oracle Linux에서 이루어지지만 단계 및 구성은 주로 CentOS 및 Red Hat Linux와 동일합니다. 이 자습서에서는 2개의 서버를 사용합니다. 각각에 대해 MySQL 데이터베이스를 설정하고 다중 마스터 복제를 위해 구성합니다. 이 자습서의 끝에서 DDL(데이터 정의 언어) 및 DML(데이터 조작 언어) 요청을 포함한 모든 READ 또는 WRITE 요청이 두 서버에서 실행되는 것을 볼 수 있습니다.

2. 설치 단계

설치 단계에서는 구성 단계를 위한 MySQL 서버 패키지와 데이터베이스 환경에 액세스하기 위한 MySQL 클라이언트만 필요합니다. 두 패키지 모두 일부 종속성을 설치해야 합니다. 먼저 운영 체제 버전을 확인하고 사전 구성을 위해 IP 주소를 적어둡니다.


[ ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:42:C0:4C
inet addr:192.168.43.11 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe42:c04c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:544 errors:0 dropped:0 overruns:0 frame:0
TX packets:79 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:51274 (50.0 KiB) TX bytes:9474 (9.2 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)

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


[ ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 09:00:30:42:C1:5D
inet addr:192.168.43.12 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe42:c04c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:544 errors:0 dropped:0 overruns:0 frame:0
TX packets:79 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:51274 (50.0 KiB) TX bytes:9474 (9.2 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)

다음으로 서버 호스트 파일에 IP 주소를 추가합니다. 아래와 같이 두 서버에서 동일하게 수행하십시오.


[ ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.43.11 DB1
192.168.43.12 DB2

다음으로, yum 유틸리티를 통해 MySQL 서버 및 MySQL 클라이언트 패키지를 설치하도록 새 리포지토리를 구성합니다. 두 서버 모두에서 이 작업을 수행하십시오.


[ yum.repos.d]# ls
CentOS.repo mysql-community.repo
mysql-community-release-el6-5.noarch.rpm mysql-community-source.repo

최신 MySQL 버전의 새 리포지토리가 설치되었습니다. 활성화합시다.



[ yum.repos.d]# vi mysql-community.repo
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

완료되면 MySQL 패키지를 사용할 수 있는지 확인합니다.



[ yum.repos.d]# rpm -qa|grep -i mysql
mysql-community-release-el6-5.noarch

좋습니다. 이제 절반이 끝났습니다. 현재 서버에 MySQL 패키지가 설치되어 있지 않으므로 패키지 설치를 시작하겠습니다. 다음은 단계입니다.

[> 
Package: mysql-community-release-el6-5.noarch (installed)
From : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : mysql-community-common-5.6.36-2.el6.i686 1/5
Installing : mysql-community-libs-5.6.36-2.el6.i686 2/5
Installing : mysql-community-client-5.6.36-2.el6.i686 3/5
Installing : perl-DBI-1.609-4.el6.i686 4/5
Installing : mysql-community-server-5.6.36-2.el6.i686 5/5
Verifying : perl-DBI-1.609-4.el6.i686 1/5
Verifying : mysql-community-server-5.6.36-2.el6.i686 2/5
Verifying : mysql-community-libs-5.6.36-2.el6.i686 3/5
Verifying : mysql-community-common-5.6.36-2.el6.i686 4/5
Verifying : mysql-community-client-5.6.36-2.el6.i686 5/5

Installed:
mysql-community-server.i686 0:5.6.36-2.el6

Dependency Installed:
mysql-community-client.i686 0:5.6.36-2.el6 mysql-community-common.i686 0:5.6.36-2.el6
mysql-community-libs.i686 0:5.6.36-2.el6 perl-DBI.i686 0:1.609-4.el6

Complete!

좋습니다. 이제 설치가 완료되었습니다. 처음으로 MySQL 데몬을 시작합니다.


[ yum.repos.d]# service mysqld restart
Stopping mysqld: [ OK ]
Initializing MySQL database: 2017-05-22 09:55:53 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-05-22 09:55:53 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-05-22 09:55:53 0 [Note] /usr/sbin/mysqld (mysqld 5.6.36) starting as process 18645 ...
2017-05-22 09:55:53 18645 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-05-22 09:55:53 18645 [Note] InnoDB: The InnoDB memory heap is disabled
2017-05-22 09:55:53 18645 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-05-22 09:55:53 18645 [Note] InnoDB: Memory barrier is not used
2017-05-22 09:55:53 18645 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-05-22 09:55:53 18645 [Note] InnoDB: Using Linux native AIO
2017-05-22 09:55:53 18645 [Note] InnoDB: Using CPU crc32 instructions
2017-05-22 09:55:53 18645 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-05-22 09:55:53 18645 [Note] InnoDB: Completed initialization of buffer pool
2017-05-22 09:55:53 18645 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-05-22 09:55:53 18645 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-05-22 09:55:53 18645 [Note] InnoDB: Database physically writes the file full: wait...
2017-05-22 09:55:53 18645 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2017-05-22 09:55:53 18645 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2017-05-22 09:55:53 18645 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2017-05-22 09:55:53 18645 [Warning] InnoDB: New log files created, LSN=45781
2017-05-22 09:55:53 18645 [Note] InnoDB: Doublewrite buffer not found: creating new
2017-05-22 09:55:53 18645 [Note] InnoDB: Doublewrite buffer created
2017-05-22 09:55:53 18645 [Note] InnoDB: 128 rollback segment(s) are active.
2017-05-22 09:55:53 18645 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-05-22 09:55:53 18645 [Note] InnoDB: Foreign key constraint system tables created
2017-05-22 09:55:53 18645 [Note] InnoDB: Creating tablespace and datafile system tables.
2017-05-22 09:55:53 18645 [Note] InnoDB: Tablespace and datafile system tables created.
2017-05-22 09:55:53 18645 [Note] InnoDB: Waiting for purge to start
2017-05-22 09:55:53 18645 [Note] InnoDB: 5.6.36 started; log sequence number 0
2017-05-22 09:55:54 18645 [Note] Binlog end
2017-05-22 09:55:54 18645 [Note] InnoDB: FTS optimize thread exiting.
2017-05-22 09:55:54 18645 [Note] InnoDB: Starting shutdown...
2017-05-22 09:55:55 18645 [Note] InnoDB: Shutdown completed; log sequence number 1625977


2017-05-22 09:55:55 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-05-22 09:55:55 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-05-22 09:55:55 0 [Note] /usr/sbin/mysqld (mysqld 5.6.36) starting as process 18667 ...
2017-05-22 09:55:55 18667 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-05-22 09:55:55 18667 [Note] InnoDB: The InnoDB memory heap is disabled
2017-05-22 09:55:55 18667 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-05-22 09:55:55 18667 [Note] InnoDB: Memory barrier is not used
2017-05-22 09:55:55 18667 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-05-22 09:55:55 18667 [Note] InnoDB: Using Linux native AIO
2017-05-22 09:55:55 18667 [Note] InnoDB: Using CPU crc32 instructions
2017-05-22 09:55:55 18667 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-05-22 09:55:55 18667 [Note] InnoDB: Completed initialization of buffer pool
2017-05-22 09:55:55 18667 [Note] InnoDB: Highest supported file format is Barracuda.
2017-05-22 09:55:55 18667 [Note] InnoDB: 128 rollback segment(s) are active.
2017-05-22 09:55:55 18667 [Note] InnoDB: Waiting for purge to start
2017-05-22 09:55:55 18667 [Note] InnoDB: 5.6.36 started; log sequence number 1625977
2017-05-22 09:55:55 18667 [Note] Binlog end
2017-05-22 09:55:55 18667 [Note] InnoDB: FTS optimize thread exiting.
2017-05-22 09:55:55 18667 [Note] InnoDB: Starting shutdown...
2017-05-22 09:55:57 18667 [Note] InnoDB: Shutdown completed; log sequence number 1625987




PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h vdevknime1 password 'new-password'

Alternatively you can run:

/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

Note: new default config file not created.
Please make sure your config file is current

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
#NAME?

[ OK ]
Starting mysqld: [ OK ]

좋습니다. 이제 MySQL 서버 서비스가 시작되었습니다. MySQL 서비스에서 사용하는 포트를 나열하여 확인하겠습니다. 기본적으로 MySQL은 서비스를 시작할 때 포트 3306을 사용합니다. 다음은 명령입니다.


[ yum.repos.d]# netstat -apn|grep -i mysql
tcp 0 0 :::3306 :::* LISTEN 2139/mysqld
unix 2 [ ACC ] STREAM LISTENING 16018 2139/mysqld /var/lib/mysql/mysql.sock 

이제 MySQL 서버의 기본 보안을 놓치지 않도록 MySQL 루트 사용자의 초기 비밀번호를 설정하겠습니다.


[ |
+----------------+
1 row in set (0.00 sec)

설치 단계를 완료했습니다. 다중 마스터 복제 설정의 구성으로 이동하겠습니다.

3. 구성 단계

MySQL my.cnf 구성 파일 내부로 이동하여 서버 DB1에서 아래와 같이 변경합니다.


[ ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
port=3306
innodb_file_per_table=ON
pid-file=/var/lib/mysql/mysqld.pid

server-id = 11
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = test_rep

다음은 구성에 대한 설명입니다.

  • server-id ==> 복제 ID
  • log_bin ==> 복제 작업에 사용할 로그 파일\n
  • binlog_do_db ==> 복제 프로세스와 관련된 데이터베이스\n

완료되면 MySQL 서버 환경으로 이동하여 관련 데이터베이스를 생성하고 복제 프로세스에 대한 사용자를 할당합니다.

[ ~]# mysql -u root -p 
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql> create database test_rep;
Query OK, 1 row affected (0.01 sec)

mysql> create user 'replicator'@'DB2' identified by 'Rep1234';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'replicator'@'DB2';
Query OK, 0 rows affected (0.00 sec) 

완료, 이제 MySQL 서버를 다시 시작하고 구성이 활성화되었는지 확인하십시오. 다음은 단계입니다.

[ ~]# mysql -u root -p 
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 854 | test_rep | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) 

훌륭합니다. 이제 서버 DB2를 DB1 복제 마스터의 슬레이브 서버로 설정하고 그 위에 서버 DB2를 DB1 서버의 마스터로 설정할 수 있습니다. 다음은 단계입니다.

[ ~]# vi /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
port=3306
innodb_file_per_table=ON
pid-file=/var/lib/mysql/mysqld.pid

server-id = 12
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = test_rep 

DB1의 구성과 동일하게 MySQL 서버 환경 내부로 들어가 관련 데이터베이스를 생성하고 복제 프로세스를 위한 사용자를 할당합니다.

[ ~]# mysql -u root -p 
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql> create database test_rep;
Query OK, 1 row affected (0.01 sec)

mysql> create user 'replicator'@'DB1' identified by 'Rep1234';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'replicator'@'DB1';
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status;
Empty set (0.01 sec)

완료, 이제 DB2 MySQL 서버를 다시 시작하고 구성이 활성화되었는지 여부를 확인합니다. 그렇다면 계속해서 DB1 서버용 슬레이브 생성을 진행합니다.


[ ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 553 | test_rep | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

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

mysql> CHANGE MASTER TO MASTER_HOST = 'DB1', MASTER_PORT = 3306, MASTER_USER = 'replicator', MASTER_PASSWORD = 'Rep1234', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 854;
Query OK, 0 rows affected, 2 warnings (0.07 sec)

mysql> start slave;
Query OK, 0 rows affected (0.04 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: DB1
Master_User: replicator
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 854
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: mysql-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: 854
Relay_Log_Space: 459
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: 11
Master_UUID: 6e143d91-3635-11e7-b9ad-08002742c04c
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)

ERROR:
No query specified


mysql> show slave status;
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------
| Waiting for master to send event | DB1 | replicator | 3306 | 60 | mysql-bin.000001 | 854 | mysqld-relay-bin.000002 | 284 | mysql-bin.000001 | Yes | Yes | | | | | | | 0 | | 0 | 854 | 459 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 11 | 6e143d91-3635-11e7-b9ad-08002742c04c | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------
1 row in set (0.00 sec) 

엄청난! DB2 서버에 대한 모든 설정이 완료되었으므로 DB1 서버로 돌아가서 DB2 서버에 대한 슬레이브 구성을 만듭니다.

[ ~]# mysql -u root -p 
Enter password:
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 854 | test_rep | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

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

mysql> CHANGE MASTER TO MASTER_HOST = 'DB2', MASTER_PORT = 3306, MASTER_USER = 'replicator', MASTER_PASSWORD = 'Rep1234', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 553;
Query OK, 0 rows affected, 2 warnings (0.25 sec)

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

mysql> show slave status;
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------
| Waiting for master to send event | DB2 | replicator | 3306 | 60 | mysql-bin.000001 | 553 | mysqld-relay-bin.000002 | 284 | mysql-bin.000001 | Yes | Yes | | | | | | | 0 | | 0 | 553 | 459 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 12 | 14f5ab41-3c7b-11e7-a293-08002742c04c | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 |
+----------------------------------+---------------+-------------+-------------+---------------+-------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------
1 row in set (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: DB2
Master_User: replicator
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 553
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: mysql-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: 553
Relay_Log_Space: 459
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: 12
Master_UUID: 14f5ab41-3c7b-11e7-a293-08002742c04c
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)

ERROR:
No query specified 

훌륭하게 완료되었습니다. 이제 모든 것이 준비되었으므로 테스트 단계를 진행하여 모든 구성이 올바르게 수행되었는지 결론을 내립니다.

4. 테스트 단계

테스트를 시작하기 전에 최종 결과 기대치를 가정해 보겠습니다. 이 테스트를 위해 DB1 MySQL 서버에 테이블을 생성한 다음 DB2에서 테이블이 자동으로 존재하는지 여부를 확인합니다. 그렇다면 새 데이터 행을 여기에 추가하고 두 서버에서 새 데이터를 사용할 수 있는지 DB1 서버에서 다시 확인합니다.


[ ~]# mysql -u root -p test_rep
Enter password:
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select database();
+------------+
| database() |
+------------+
| test_rep |
+------------+
1 row in set (0.00 sec)

mysql> create table tbl1( id int(11) primary key auto_increment, fullname varchar(30));
Query OK, 0 rows affected (0.22 sec)

mysql> show tables in test_rep;
+--------------------+
| Tables_in_test_rep |
+--------------------+
| tbl1 |
+--------------------+
1 row in set (0.01 sec) 

완료, 테이블 생성은 DDL(데이터 정의 언어) 문이므로 커밋 명령을 입력할 필요가 없습니다. 이제 DB2 MySQL 서버 내부로 들어가 새로 생성된 테이블이 존재하는지 확인해보자.

[ ~]# mysql -u root -p test_rep 
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables in test_rep;
+--------------------+
| Tables_in_test_rep |
+--------------------+
| tbl1 |
+--------------------+
1 row in set (0.00 sec)

mysql> insert into tbl1 ( fullname ) values ('Shahril'), ('mark'), ('Allen'), ('Suzy'), ('Adam') ;
Query OK, 5 rows affected (0.05 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from tbl1;
+----+----------+
| id | fullname |
+----+----------+
| 1 | Shahril |
| 2 | mark |
| 3 | Allen |
| 4 | Suzy |
| 5 | Adam |
+----+----------+
5 rows in set (0.00 sec)

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

훌륭합니다. DB1 서버에서 새로 생성된 테이블이 이제 자동으로 DB2 MySQL 서버에 존재합니다. 그런 다음 5행의 데이터를 테이블에 삽입했습니다. 최종 확인을 위해 테이블 TBL1의 업데이트된 행이 DB1 서버에서도 볼 수 있는지 확인합니다.


[ ~]# mysql -u root -p test_rep
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from tbl1;
+----+----------+
| id | fullname |
+----+----------+
| 1 | Shahril |
| 2 | mark |
| 3 | Allen |
| 4 | Suzy |
| 5 | Adam |
+----+----------+
5 rows in set (0.01 sec)

mysql> delete from tbl1 where fullname like 'A%';
Query OK, 2 rows affected (0.07 sec)

mysql> select * from tbl1;
+----+----------+
| id | fullname |
+----+----------+
| 1 | Shahril |
| 2 | mark |
| 4 | Suzy |
+----+----------+
3 rows in set (0.00 sec) 

엄지척! 2개의 서버 간에 MySQL 다중 마스터 복제를 성공적으로 생성했습니다.