반응형

출처:http://www.koreaidc.com/bbs/set_view.php?b_name=idcpds&w_no=104

https://www.digitalocean.com/community/tutorials/how-to-set-up-master-slave-replication-in-mysql

 

#MYsql 리플리케이션이란?
# Replication은 3.23.15부터 지원되기 시작한 기능으로 ‘복제’라는 사전적 의미에 맞게 마스터의 MySQL 서버의 데이터를 여러 대의 슬레이브 MySQL 서버의 데이터와 동기화 시켜주는 기능이다. 주로, MySQL의 데이터를 실시간으로 백업하거나, 데이터 서버의 부하분산을 하고자 할 때 많이 사용된다.




0. 서버 설정


1번서버 - master 서버  : 1차네임서버 : 192.168.1.111

2번서버 - slave 서버     : 2차네임서버 : 192.168.1.222
 
mysql 버전은 모두 mysql 5.X 이다.
두 서버 모두 mysql 데이터는 /free/mysql_data 에 위치해 있다고 가정한다.
 
 
 
1. Master 서버 설정
 
# vi /etc/my.cnf
 
(1) my.cnf 파일에서 #log-bin=mysql-bin 부분의 주석을 반드시 해제해준다
슬레이브 서버에서 저 바이너리 로그를 기준으로 데이터 리플리케이션을 실행 하기 때문에 저 로그파일이 꼭 필요하다!
 
(2) server-id = 1 로 설정한다.
마스터서버와 슬레이브서버의 번호가 서로 다르기만 하면 되기 때문에 원하는 번호를 지정한다.
 

# Replication Master Server (default)
# binary logging is required for replication

log-bin=mysql-bin

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id       = 1

 
 
(3) slave 서버 에서 접속 할 수 있는 Mysql 계정을 생성해준다
# mysql -u root -p 로 디비 접속
mysql > GRANT REPLICATION SLAVE ON *.*  TO 유저명@접속허용할IP IDENTIFIED BY '패스워드';
 
접속허용할 IP에 특정 IP만을 부여 할 수도 있고 '%' 를 적어주면 모든 외부의 접속을 허용한다는 의미이다.
mysql > use mysql
mysql > select * from user \G;
명령로 설정이 되어 있는지 확인 할 수 있다.
 
아래 셋팅은 실제 마스터 서버인 1차네임서버 서버의 설정 내용이다.
 
접속을 허용한 유저 이름은    : nayana
nayana의 접속을 허용한 IP : 192.168.1.222 (2차 네임서버)
 
슬레이브 서버(192.168.1.222) 에서 nayana 라는 계정의 Replication 접속을 허용한 것을 볼 수 있다.

*************************** 10. row ***************************
                 Host: 192.168.1.222
                 User: nayana
             Password: *7CC252065609E6F96DD42A08D09D6C0DDBFBB0B1
          Select_priv: N
          Insert_priv: N
        Update_priv: N
         Delete_priv: N
         Create_priv: N
            Drop_priv: N
         Reload_priv: N
    Shutdown_priv: N
        Process_priv: N
               File_priv: N
            Grant_priv: N
   References_priv: N
            Index_priv: N
              Alter_priv: N
      Show_db_priv: N
           Super_priv: N
Create_tmp_table_priv: N
  Lock_tables_priv: N
         Execute_priv: N
     Repl_slave_priv: Y      <- 요 부분이 Y로 되어있으면 된다!!
     Repl_client_priv: N
  Create_view_priv: N
    Show_view_priv: N
Create_routine_priv: N
  Alter_routine_priv: N
   Create_user_priv: N
             ssl_type:
           ssl_cipher:
          x509_issuer:
         x509_subject:
        max_questions: 0
          max_updates: 0
      max_connections: 0
 max_user_connections: 0
10 rows in set (0.00 sec)

 
 
(4) mysql을 재시작 하고 마스터 서버의 동작을 확인한다.
"mysql-bin.000022" 라는 바이너리 로그 파일을 생성한 것을 볼 수 있다.

mysql> show master status;

+------------------------+-----------+-------------------+------------------------+
| File                         | Position | Binlog_Do_DB  | Binlog_Ignore_DB  |
+------------------------+-----------+-------------------+------------------------+
| mysql-bin.000022   |     8385 |                         |                               |
+------------------------+-----------+-------------------+------------------------+
1 row in set (0.00 sec)

mysql>

 
 
 

2. Slave 서버 설정

(1) 마스터 서버의 DB를 슬레이브 서버의 DB에 복사한다!(= 최초1회는 직접 복사해서 동기화해준다!)
이유는?
 
mysql 리플리케이션은 rsync와 다르게 동기화로 엮여진 시점부터의 DB변화를 동기화 시킨다.
 
[ 동기화 전 DB 구조]
마스터               슬레이브        
1                        1
2                        2
3
 
########## 동기화 후 마스터 서버에 4 라는 DB를 추가하면? #########
 
[ 동기화 후 DB 구조]
마스터               슬레이브        
1                        1
2                        2
3                        4
4
 
이렇게 4 라는 DB만 추가되고 3이라는 DB는 추가되지 않는다. 애초에 처음부터 DB내용이 달랐기 때문!
rsync 였다면 양쪽에 모두 똑같아 졌을 것이다.
 
 
[ 마스터 서버 ]
# cd /free
# tar zcvf mysql.tar.gz mysql_data
# sz mysql.tar.gz
 
압축한 파일을 다운받아 슬레이브 서버의 /free/mysql_data 디렉토리에 플어준다. 이로서 최초 동기화를 완료했다.
 
(2) /etc/my.cnf 파일을 수정한다!
57 , 62 번 라인은 주석처리해주고 98번 라인부터는 아래와 같이 주석을 풀고 마스터 서버에 접속할 정보를 적는다.
 

       # Replication Master Server (default)
       # binary logging is required for replication
57   # log-bin=mysql-bin

       # required unique id between 1 and 2^32 - 1
       # defaults to 1 if master-host is not set
       # but will not function as a master if omitted
62   # server-id       = 1

98   # but will not function as a slave if omitted
       server-id       = 2
       # The replication master for this slave - required
       master-host     = 192.168.1.111

       # The username the slave will use for authentication when connecting
       # to the master - required
      
master-user     = nayana

       # The password the slave will authenticate with when connecting to
       # the master - required
       master-password = 패스워드
       
       # The port the master is listening on.
       # optional - defaults to 3306
       master-port     = 3306
       # binary logging - not required for slaves, but recommended
      
log-bin=mysql-bin

 
 
(3) mysql 을 재시작 한후 mysql에 접속하여 동기화가 되었는지 확인한다.
동기화가 정상적으로 되었다면 아래와 같이 "Waiting for master to send event" 라는 구문이 출력된다!
 

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 |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Waiting for master to send event | 192.168.1.111 | nayana      |        3306 |            60 | mysql-bin.000022 |                8385 | mysqld-relay-bin.000015 |           235 | mysql-bin.000022      | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                8385 |             235 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
1 row in set (0.00 sec)

mysql>

 
이제 마스터 서버에서 DB를 생성하거나 삭제하면 슬레이브 서버에서도 똑같이 동기화가 될 것이다!! 올레~!
 
반응형

+ Recent posts