Tag Archives: Replication

MySQL Replication

MASTER CONFIGURATION (127.0.0.1)

my.cnf 파일에 설정을 추가합니다.
[code]# inodb configurations
log-bin                         = mysql-bin
server-id                       = 1
innodb_data_home_dir            = /theeye/db
innodb_data_file_path           = ibdata1:1000M;ibdata2:1000M:autoextend
innodb_buffer_pool_size         = 1G
innodb_additional_mem_pool_size = 40M
innodb_log_file_size            = 250M
innodb_log_buffer_size          = 8M
innodb_flush_log_at_trx_commit  = 0
innodb_lock_wait_timeout        = 5
relay-log                       = /theeye/db/master-relay-bin


# for master
innodb_flush_log_at_trx_commit  = 1
sync_binlog                     = 1
binlog_ignore_db                = mysql
binlog_ignore_db                = test
binlog_do_db                    = theeye[/code]

슬레이브가 리플리케이션을 위해 접근할 계정을 추가합니다. 원격지에서 접근 가능하도록 합니다.
[code]mysql> GRANT REPLICATION SLAVE ON *.* TO replicator@’%’ IDENTIFIED BY ‘password’;[/code]

마스터의 데이터를 백업하기 위해 쓰기를 금지합니다., innodb의 경우 COMMIT까지 방지됩니다.
[code]mysql> FLUSH TABLES WITH READ LOCK;[/code]

현재 쓰고 있는 마스터 log-bin 파일을 확인합니다.
[code]
mysql > SHOW MASTER STATUS;
+—————+———-+————–+——————+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+—————+———-+————–+——————+
| mysql-bin.003 | 73       | theeye       | test,mysql       |
+—————+———-+————–+——————+[/code]

마스터 디비를 백업합니다.
[code]# mysqldump –add-drop-table –routines  –triggers –default-character-set=utf8 –master-data theeye > theeye.sql[/code]

디비 락 걸었던것을 해제 합니다. 실제로는 슬레이브로의 이관작업이 끝날때 합니다.
[code]mysql> UNLOCK TABLES;[/code]

SLAVE CONFIGURATION (127.0.0.2)

my.cnf 파일에 설정을 추가합니다.
[code]# inodb configurations
server-id                       = 2
master-host                     = 127.0.0.1
master-user                     = replicator
master-password                 = ‘password’
master-port                     = 3306
innodb_data_file_path           = ibdata1:1000M;ibdata2:1000M:autoextend
innodb_buffer_pool_size         = 1G
innodb_additional_mem_pool_size = 40M
innodb_log_file_size            = 250M
innodb_log_buffer_size          = 8M
innodb_flush_log_at_trx_commit  = 0
innodb_lock_wait_timeout        = 5
relay-log                       = /theeye/db/slave-relay-bin


# for slave
read-only
master-connect-retry            = 60
replicate-do-db                 = theeye[/code]

마스터에서 백업해온 파일로 복원합니다.
[code]shell> mysql theeye < theeye.sql[/code]

연결할 마스터를 정의합니다. 아까 봐둔 마스터 log-bin파일명을 넣어줍니다.
[code]mysql> slave stop;
mysql> CHANGE MASTER TO MASTER_HOST=’127.0.0.1′, MASTER_USER=’replicator’, MASTER_LOG_FILE=’mysql-bin.003′, MASTER_PASSWORD=’password’;
mysql> slave start;[/code]

Slave_IO_Running, Slave_SQL_Running가 둘다 Yes이고 Read_Master_Log_Pos가 마스터의 변화에 따라 올라간다면 정상적으로 작동중인것입니다.
[code]mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 127.0.0.1
                Master_User: replicator
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000004
        Read_Master_Log_Pos: 2288979
             Relay_Log_File: slave-relay-bin.000002
              Relay_Log_Pos: 1267487
      Relay_Master_Log_File: mysql-bin.000004
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB: theeye
        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: 2288979
            Relay_Log_Space: 1267487
            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
1 row in set (0.00 sec)[/code]

기타 슬레이브가 다수가 될 경우에는 위와 같은 방법으로 my.cnf의 server-id값만 다르게 하면 됩니다.
LOAD DATA FROM MASTER등의 명령은 deprecated되었으므로 사용을 자제 합시다.