1. 시스템 환경

▲구현하고자 하는 Master/Slave Replication과 시스템 환경

2. Replication 설정
Master Server (Linux)
① Replication을 위한 계정을 추가한다. 다만 4.0.2 이전 버젼의 MySQL 사용자일 경우는 REPLICATION SLAVE의 권한설정 부분이 없으므로, 대신에 FILE 권한설정 사용하여야 한다. (user id와 user password는 설정하고자 하는 것으로 하면 된다. 여기서는 둘다 repli로 설정)
mysql> GRANT REPLICATION SLAVE ON *.*
   -> TO ‘user id’@’%’ IDENTIFIED BY ‘user password’;

*4.0.2 이전 버젼은 다음과 같이 한다.
mysql> GRANT FILE ON *.*
   -> TO ‘user id’@’%’ IDENTIFIED BY ‘user password’;

② Replication을 적용할 테이블을 생성한다.(여기서는 Test DB에 repli_test 테이블을 생성하기로 한다.)
mysql> use test;
mysql> create table repli_test (name varchar(255));

③ MySQL 서비스를 중지 시킨다.

④ /etc/my.cnf 파일을 생성하고, 아래의 내용 중 노란색으로 마킹된 내용을 추가하거나 수정한다. (my.cnf 파일은 설치시 생성되는 파일이 아니므로, 파일을 직접 작성하거나, Source를 이용해서 설치했을 경우, [mysql 설치 디렉토리]/share/mysql 디렉토리에 my-xxx.cnf 파일들이 있으므로 적당히 골라서 수정을 하여 사용한다.) server-id는 서버끼리 중복되지 않는 값을 지정하여 줄 수가 있으며, 지정범위는 1부터 2^32-1(=4294967295)까지 이다.
# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is /home/mysql/data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
#password     = your_password
port        = 3306
socket       = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port        = 3306
socket       = /tmp/mysql.sock
skip-locking
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
net_buffer_length = 2K
thread_stack = 64K

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id     = 1

# Uncomment the following if you want to log updates
log-bin

# Uncomment the following if you are NOT using BDB tables
#skip-bdb

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /home/mysql/data/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /home/mysql/data/
#innodb_log_arch_dir = /home/mysql/data/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer = 8M
sort_buffer_size = 8M

[myisamchk]
key_buffer = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout

⑤ [mysql 설치 디렉토리]/data/test 디렉토리로 이동하여 Replication을 적용할 Table을 백업해놓는다.(백업한 Table 파일은 Slave DB Server에 복사해서 사용할 것이다. 사실 이렇게 파일 복사 말고 Table export를 하여 Slave DB에 import 시켜도 된다.)
# ls
repli_test.MYD repli_test.MYI repli_test.frm
# tar cvzf repli_test.tar.gz *

Slave Server (Windows 2000 Pro)
⑥ MySQL 서비스를 중지 시킨다.

⑦ C:\WINNT\my.ini 파일을 아래와 같이 노란색으로 마킹된 내용을 추가하거나 수정한다.
[WinMySQLAdmin]
Server=C:/mysql/bin/mysqld-nt.exe
[mysqld]
master_host=192.168.0.1
master-user=repli
master-password=repli
master-port=3306
server-id=2

⑧ Master Server에서 백업한 Replication을 적용할 Table 파일을 C:\mysql\data\test 디렉토리에 압축을 풀어놓거나 복사해 놓는다. (⑤과정참조)


Master & Slave Server
⑨ Master Server의 MySQL 서비스를 시작한 후, Slave Server의 MySQL 서비스를 시작한다.

3. Test 및 Monitoring 하기
Test는 간단하게 Master Server에서 Data값을 Insert, Update, Delete를 해보고, 결과가 Slave Server에도 반영이 되는지 살펴보면 된다.(Master/Slave Replication 구성으로는 Slaver Server에서 Data값을 Insert, Update, Delete를 해도 Master Server에는 반영이 되지 않는다.)
*Master Server
mysql> insert into repli_test value ('test1');
Query OK, 1 row affected (0.00 sec)

*Slave Server
mysql> select * from repli_test;
+-------+
| name  |
+-------+
| test1 |
+-------+
4 rows in set (0.01 sec)

Master와 Slave Server의 상태를 확인하고 싶을 때에는 다음과 같은 명령어를 실행한다.
*Master Server
mysql> show master status \G
*************************** 1. row ***************************
       File: XXX-bin.002
    Position: 300
  Binlog_do_db:
Binlog_ignore_db:
1 row in set (0.00 sec)

*Slave Server
mysql> show slave status \G
*************************** 1. row ***************************
      Master_Host: XXX.XXX.XXX
      Master_User: repli
      Master_Port: 3306
    Connect_retry: 60
   Master_Log_File: XXX-bin.002
Read_Master_Log_Pos: 300
    Relay_Log_File: XXX-relay-bin.002
    Relay_Log_Pos: 456
Relay_Master_Log_File: XXX-bin.002
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
   Replicate_do_db:
Replicate_ignore_db:
      Last_errno: 0
      Last_error:
     Skip_counter: 0
Exec_master_log_pos: 300
   Relay_log_space: 452
1 row in set (0.00 sec)

Slave Server에서 Replication을 중단(slave stop)하거나, 다시 시작(slave start)할 때에는 다음과 같은 명령어 실행한다.
*Slave Server
mysql> slave stop;
Query OK, 0 rows affected (20.69 sec)

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

4. Replication을 응용한 구성의 예

▲Dual-Master Replication

▲Replication Ring

▲Relay

▲Load-Balancing Reads

5. 참고자료

+ Recent posts