๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ชฉ๋ก์ด ์—†์Šต๋‹ˆ๋‹ค.

[MariaDB] Master-Slave replication ์„œ๋ฒ„ ๊ตฌ์ถ•

๐Ÿ›ข DB

     


     

    Database๋ฅผ ๋งŒ๋“ค๊ณ  ๋‚˜์„œ ํ˜น์‹œ ๋ชจ๋ฅผ DB์„œ๋ฒ„์žฅ์• ์— ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•œ ์˜ˆ๋ฐฉ์ฑ…์œผ๋กœ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘์— ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ด๊ณ  ํšจ๊ณผ์ ์ธ ๋Œ€๋น„์ฑ…์€ ๋ฐ”๋กœ ๋ฐฑ์—…(backup)์ธ๋ฐ์š”. ๋ฐฑ์—… ๋ฐฉ์‹์—๋„ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค. linux ๊ธฐ๋ฐ˜ DB ์„œ๋ฒ„๋ผ๋ฉด ์Šค์ผ€์ค„๋Ÿฌ์ธ cron์„ ์ด์šฉํ•ด์„œ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋คํ”„ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๊ณ  ์•„๋‹ˆ๋ฉด ์˜ค๋Š˜ ์ •๋ฆฌํ•  master-slave ์„ค์ •์„ ํ†ตํ•ด์„œ ๋ณต์ œ(replication)ํ•˜์—ฌ ์žฅ์• ์— ๋Œ€๋น„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

     


     

    ์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋‹ค์‹œํ”ผ ์˜ค๋Š˜์€ master-slave๋ฅผ ์„ค์ •ํ•ด์„œ DB๋ฅผ ๋ฐฑ์—…ํ•˜๊ณ  master๋Š” write๋งŒ, slave๋Š” read๋งŒ ํ•˜๋Š” ๋ถ„์‚ฐ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ์•ฝ๊ฐ„์˜ ์„ฑ๋Šฅํ–ฅ์ƒ์„ ๋…ธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

    ์šฐ์„  master์™€ slave๊ฐ€ ๊ฐ™์€ ์„œ๋ฒ„ ์ฆ‰, ํ•˜๋‚˜์˜ ์„œ๋ฒ„์—์„œ ๋™์ž‘ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ๋˜๋ฉด ์„œ๋ฒ„์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ œ๋Œ€๋กœ๋œ ์—ญํ• ์„ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„๋ฅผ ๋ณ„๋„๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ์ค€๋น„๋ฌผ

    1. master ์„œ๋ฒ„
    2. slave ์„œ๋ฒ„
    3. ๊ฐ๊ฐ ๋™์ผํ•œ DB ๋ฐ์ดํ„ฐ
    **master ์„œ๋ฒ„๋Š” slave ์„œ๋ฒ„์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์•ผํ•จ.

     

     

    1. Master ์„ค์ •

    master์ชฝ์— ์„ค์ •์„ ๋จผ์ € ํ•ด์ค์‹œ๋‹ค.

     

    1-1. my.cnf (mariadb ์„ค์ •ํŒŒ์ผ) ์ˆ˜์ •

    my.cnfํŒŒ์ผ์„ ์—ด์–ด์„œ [mysqld] ์•„๋ž˜์— server_id๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.

    ...
    
    [mysqld]
    
    #
    # * Master Settings
    #
    server_id = 1
    
    ...

     

    server_id๋ฅผ my.cnf์— ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  ์‹ค์ œ๋กœ ์ž˜ ์ ์šฉ์ด ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

     

    # service mysql restart
    # mysql -uroot -p
    
    > show variables like 'server_id';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 1     |
    +---------------+-------+
    1 row in set (0.001 sec)

    ๊ฒฐ๊ณผ๋กœ ์œ„์—์„œ ์„ค์ •ํ•œ server_id๊ฐ€ 1๋กœ ๋‚˜์˜ค๋ฉด ์ •์ƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ์— ๊ฐ’์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜๋™์œผ๋กœ server_id๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    > SET GLOBAL server_id = 1;

     

    1-2. Replication ์‚ฌ์šฉ์ž ๋“ฑ๋ก

    replication์„ ํ•  slave ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

     

    > grant replication slave on *.* to ์•„์ด๋””@'%' identified by '๋น„๋ฐ€๋ฒˆํ˜ธ';
    > flush privileges;

    ์—ฌ๊ธฐ์„œ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์—๋Š” ์‚ฌ์šฉํ•˜์‹ค ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

     

    1-3. Master ์ •๋ณด ํ™•์ธ

    ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ Slave์—์„œ ์ง€์ •ํ•  Master์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

    > show master status;
    +--------------------+----------+--------------+------------------+
    | File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +--------------------+----------+--------------+------------------+
    | mariadb-bin.000001 |      344 |              |                  |
    +--------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)
    
    ์œ„์˜ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ค๋Š” File๊ณผ Position์„ ๊ธฐ์–ตํ•˜์‹œ๊ฑฐ๋‚˜ ๋ฉ”๋ชจํ•ด์ฃผ์„ธ์š”. Slave์„ค์ • ์‹œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

     

    1-4. Master ์ •๋ณด ํ™•์ธ ๋ถˆ๊ฐ€ (์ •๋ณด๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๋Š” ๊ฒฝ์šฐ. Empty set)

     

    Empty set (0.00 sec)

    ๋งŒ์•ฝ์— ์œ„์™€๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ log_bin์ด ๊บผ์ ธ์žˆ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” log_bin์ด ์ƒ๋žต๋˜๋„๋ก my.cnf์— ์„ค์ •๋˜์–ด์žˆ์ง€๋Š” ์•Š์€์ง€ ์•„๋‹ˆ๋ฉด ์•„์˜ˆ ์„ค์ •์ด ์•ˆ ๋˜์–ด์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜์‹œ๊ณ  ์„ค์ •ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    MariaDB [(none)]> show variables like '%log_bin%';
    +---------------------------------+----------------------------------+
    | Variable_name                   | Value                            |
    +---------------------------------+----------------------------------+
    | log_bin                         | OFF                              |
    | log_bin_basename                | /var/log/mysql/mariadb-bin       |
    | log_bin_compress                | OFF                              |
    | log_bin_compress_min_len        | 256                              |
    | log_bin_index                   | /var/log/mysql/mariadb-bin.index |
    | log_bin_trust_function_creators | OFF                              |
    | sql_log_bin                     | ON                               |
    +---------------------------------+----------------------------------+
    7 rows in set (0.00 sec)
    
    log_bin์ด OFF๋กœ ๊บผ์ ธ์žˆ๋Š”๊ฒŒ ํ™•์ธ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. my.cnf ํŒŒ์ผ์„ ๋‹ค์‹œ ์—ด์–ด์„œ [mysqld] ์˜์—ญ์— log_bin ๊ฒฝ๋กœ๋ฅผ ์…‹ํŒ…ํ•ด์ฃผ๊ฑฐ๋‚˜ ๋ฐ‘์— skip_log_bin์ด ์„ค์ •๋˜์–ด์žˆ์ง€ ์•Š์€์ง€ ํ™•์ธํ•˜์‹œ๊ณ  ์ˆ˜์ •ํ•œ ํ›„์— mysql restart ํ•ด์ฃผ์‹œ๋ฉด ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค.

     

    my.cnf ์ˆ˜์ •

    [mysqld]
    ...
    # log_bin = mysql-bin -> ์ฃผ์„์ฒ˜๋ฆฌ๋˜์–ด์žˆ๊ฑฐ๋‚˜ ์•„์˜ˆ ์—†๋Š”๊ฒฝ์šฐ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผํ•จ
    skip_log_bin -> ์ œ๊ฑฐํ•ด์ฃผ์–ด์•ผํ•จ
    ...

     


     

    2. Slave ์„ค์ •

    Master ์„ค์ •์„ ๋งˆ์ณค๋‹ค๋ฉด ์ ˆ๋ฐ˜์€ ์„ฑ๊ณตํ•˜์‹  ๊ฒ๋‹ˆ๋‹ค. ์ด์–ด์„œ Slave๋„ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.

     

    2-1. my.cnf ์ˆ˜์ •

    Master์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๊ณ  my.cnfํŒŒ์ผ์„ ์—ด์–ด์„œ [mysqld] ์•„๋ž˜์— server_id ๋ฐ slave ์ •๋ณด๋ฅผ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.

    ...
    
    [mysqld]
    
    #
    # * Slave Settings
    #
    server_id = 2
    relay_log = mysql-relay-bin
    log_slave_updates = 1
    read_only = 1
    
    
    ...

     

    my.cnf ์ˆ˜์ •ํ•œ ๋‚ด์šฉ์„ ์ ์šฉ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ mysql๋ฅผ restartํ•ด์ฃผ๊ณ  server_id๋ฅผ ํ™•์ธํ•ด์ค˜์š”.

     

    # service mysql restart
    # mysql -uroot -p
    
    > show variables like 'server_id';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 2     |
    +---------------+-------+
    1 row in set (0.001 sec)

     

    Master์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ œ๋Œ€๋กœ ์„ค์ •๋˜์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ˆ˜๋™์œผ๋กœ ์„ค์ •ํ•ด์ค˜์š”.

     

    > SET GLOBAL server_id = 2;

     

     

    2-2. Master ์ ‘์† ์ •๋ณด ์„ค์ •

    Slave์—์„œ Master๋กœ ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ ์„ค์ •ํ•ด์ค˜์š”.

    ** ์—ฌ๊ธฐ์„œ ์œ„์—์„œ Master ์„ค์ • ์‹œ ๊ธฐ์–ตํ•˜๊ฑฐ๋‚˜ ๋ฉ”๋ชจํ–ˆ๋˜ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

     

    > CHANGE MASTER TO MASTER_HOST='Master์„œ๋ฒ„ IP',
    MASTER_USER='์•„์ด๋””',
    MASTER_PASSWORD='๋น„๋ฐ€๋ฒˆํ˜ธ',
    MASTER_PORT=Master DB Port,
    MASTER_LOG_FILE='์œ„์—์„œ ํ™•์ธํ•œ File',
    MASTER_LOG_POS=์œ„์—์„œ ํ™•์ธํ•œ Position,
    MASTER_CONNECT_RETRY=10;
    ๋ณด๊ธฐ ํŽธํ•˜๊ฒŒ ๊ฐœํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅํ•˜์‹ค๋•Œ์—๋Š” ํ•œ์ค„์— ์ž…๋ ฅํ•˜์…”๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค.

     

    Master-slave ์„ค์ • ๋ถ€๋ถ„์ค‘์— ๊ฐ€์žฅ ๋ณต์žกํ•œ ๋ถ€๋ถ„์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ์ฟผ๋ฆฌ์—์„œ MASTER_HOST๋Š” Master ์„œ๋ฒ„์˜ IP๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์‹œ๋ฉด ๋˜๊ณ 

    MASTER_USER์—๋Š” Master์„ค์ • ์‹œ replication salve ๊ณ„์ •์˜ ์•„์ด๋””๋ฅผ MASTER_PASSWORD์—๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    MASTER_LOG_FILE๊ณผ MASTER_LOG_POS์€ Master ์„ค์ • ์‹œ show master status; ์ฟผ๋ฆฌ๋กœ ํ™•์ธํ–ˆ๋˜ ์ •๋ณด๋ฅผ ๊ทธ๋Œ€๋กœ ์ž…๋ ฅํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    ๋งˆ์ง€๋ง‰์œผ๋กœ MASTER_CONNECT_RETRY๋Š” ์—ฐ๊ฒฐ ์‹œ๋„ ํšŸ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 10๋ฒˆ์œผ๋กœ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ์˜ˆ๋ฅผ๋“ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     

    MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='45.119.144.73',
     -> MASTER_USER='repl',
     -> MASTER_PASSWORD='repl_pw',
     -> MASTER_PORT=3306,
     -> MASTER_LOG_FILE='mariadb-bin.000001',
     -> MASTER_LOG_POS=344,
     -> MASTER_CONNECT_RETRY=10;
    Query OK, 0 rows affected (0.01 sec)

     

    ์„ค์ •์„ ๋งˆ์ณค์œผ๋ฉด slave๋ฅผ ๊นจ์›Œ๋ด…์‹œ๋‹ค.

     

    2-3. Slave start (์‹œ์ž‘)

    MariaDB [(none)]> start slave;
    Query OK, 0 rows affected (0.00 sec)

     

    2-4. Slave ๋™์ž‘ ํ™•์ธ

    ์ž˜ ๋œ๊ฑด์ง€ ํ™•์ธํ•ด๋ณด๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ์ฟผ๋ฆฌ๋ฅผ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

     

    > show slave status\G;
    *************************** 1. row ***************************
                    Slave_IO_State: Queueing master event to the relay log
                       Master_Host: <host>
                       Master_User: <user>
                       Master_Port: <port>
                     Connect_Retry: 10
                   Master_Log_File: mariadb-bin.000004
               Read_Master_Log_Pos: 105184981
                    Relay_Log_File: mysql-relay-bin.000002
                     Relay_Log_Pos: 53169888
             Relay_Master_Log_File: mariadb-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: 53169975
                   Relay_Log_Space: 210825132
                   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: 3453
     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_SSL_Crl:
                Master_SSL_Crlpath:
                        Using_Gtid: No
                       Gtid_IO_Pos:
           Replicate_Do_Domain_Ids:
       Replicate_Ignore_Domain_Ids:
                     Parallel_Mode: optimistic
                         SQL_Delay: 0
               SQL_Remaining_Delay: NULL
           Slave_SQL_Running_State: Update
                  Slave_DDL_Groups: 297
    Slave_Non_Transactional_Groups: 3
        Slave_Transactional_Groups: 31
    1 row in set (0.425 sec)

     

    Slave_I/O_Running: Yes
    Slave_SQL_Running: Yes
    ์œ„์˜ 2๊ฐœ ํ•ญ๋ชฉ์ด ๋ชจ๋‘ Yes์ด๊ณ  Slave_I/O_State: Waiting for master to send event ์ด๋ฉด ์ •์ƒ์ž‘๋™ํ•˜๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.

     

    ์ด์ œ master์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด slave์˜ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ๊ฐ™์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ํ…Œ์ŠคํŠธ ํ•ด๋ณด์‹œ๋ฉด ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๋งŒ์•ฝ์— slave๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ show slave status\G; ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    Last_IO_Errno
    Last_IO_Error
    Last_SQL_Errno
    Last_SQL_Error

    ์ด 4๊ฐœ์˜ ํ•„๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

     

    ์ฐธ๊ณ 

    ๋ธ”๋กœ๊ทธ