ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MySQL] Ubuntu(์šฐ๋ถ„ํˆฌ), MySQL 8.0↑ Replication ์„ค์ •
    Database/MY-SQL 2023. 1. 17. 20:44

    ๐ŸŒˆ ๊ตฌ์„ฑ ํ™˜๊ฒฝ Master / Slave

    Ubuntu 22.04 LTS, MySQL 8.0.31 Community

     

    1. Master ์„ค์ •

    1-1. Replication์šฉ ๊ณ„์ • ์ƒ์„ฑ ๋ฐ replication ๊ถŒํ•œ ๋ถ€์—ฌ

    [mysql]

    ํŠน์ • ์•„์ดํ”ผ๋ฅผ ์„ค์ •ํ•  ๊ฒฝ์šฐ % ๋Œ€์‹  ip๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

    create user ์œ ์ €๋ช…@'%' identified by '๋น„๋ฐ€๋ฒˆํ˜ธ';
    grant replication slave on *.* to ์œ ์ €๋ช…@'%';

     

    1-2. Mysql Replication ์„ค์ • - mysqld.cnf

    [ํ„ฐ๋ฏธ๋„]

    vi ๋ช…๋ น์–ด๋กœ mysqld.cnf ์ˆ˜์ •ํ•œ๋‹ค.

    vi /etc/mysql/mysql.conf.d/mysqld.cnf


    [mysqld] ๋ฐ”๋กœ ์•„๋ž˜ ๋ถ€๋ถ„์— 2์ค„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

    server-id: ์„œ๋ฒ„ ์•„์ด๋””๋กœ ๊ณ ์œ  ๊ฐ’ ์„ค์ •

    log-bin: ๋ฐ”์ด๋„ˆ๋ฆฌ ๋กœ๊ทธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋  path/์ด๋ฆ„ ์„ค์ •, path๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ /var/lib/mysql์— ์ƒ์„ฑ๋œ๋‹ค.

    [mysqld]
    server-id = 1
    log-bin = mysql-bin


    1-3. MySQL ์žฌ์‹œ์ž‘

    [ํ„ฐ๋ฏธ๋„]

    sudo systemctl restart mysql

     

    1-4. Master ์ƒํƒœ ํ™•์ธ

    [mysql]

    show master status;

     

    *Slave ์„ค์ •์—์„œ ์‚ฌ์šฉ๋  File๋ช…๊ณผ Position ๋ฒˆํ˜ธ

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

     


    2. Slave ์„ค์ •

    2-1. Master์—์„œ ๋ณต์ œ ํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค Dumpํ•˜์—ฌ Slave์— ๋ณต์›ํ•œ๋‹ค.

    [MySQL] ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ” Dump ํ•˜๊ธฐ

    [Linux] SCP ๋ช…๋ น์–ด๋กœ ์„œ๋ฒ„ ๊ฐ„ ํŒŒ์ผ ๋ณต์‚ฌ, ๊ฐ€์ ธ์˜ค๊ธฐ

     

    2-2. Mysql Replication ์„ค์ • - mysqld.cnf

    [ํ„ฐ๋ฏธ๋„]

    vi /etc/mysql/mysql.conf.d/mysqld.cnf


    [mysqld] ์•„๋ž˜ ๋ถ€๋ถ„์— 2์ค„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

    server-id: ์„œ๋ฒ„ ์•„์ด๋””๋กœ ๊ณ ์œ  ๊ฐ’ ์„ค์ •

    log-bin: ๋ฐ”์ด๋„ˆ๋ฆฌ ๋กœ๊ทธ ํŒŒ์ผ์ด ์ƒ์„ฑ ๋  path/์ด๋ฆ„ ์„ค์ •, path๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ default path(/var/lib/mysql)์— ํ•ด๋‹น ์ด๋ฆ„์œผ๋กœ ๋กœ๊ทธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค.

    [mysqld]
    server-id = 2
    log-bin = mysql-bin
    read-only = 1


    2-3. MySQL ์žฌ์‹œ์ž‘

    [ํ„ฐ๋ฏธ๋„]

    sudo systemctl restart mysql

     

    2-4. Slave - Master ์—ฐ๊ฒฐ

    [mysql]

    reset slave;

    Master ์„œ๋ฒ„๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์„ ํ•œ๋‹ค.

    change master to
    master_host='Master DB IP',
    master_user='1-1์—์„œ ์ƒ์„ฑํ•œ replication์šฉ ๊ณ„์ •',
    master_password='๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ',
    master_log_file='1-4์˜ File๋ช…', 
    master_log_pos=1-4์˜ Position ๋ฒˆํ˜ธ;

     

    2-5. Replication ๋ช…๋ น์–ด

    ์‹œ์ž‘

    [mysql]

    start replica;

    ์ค‘์ง€

    [mysql]

    stop replica;

     

    2-6. Slave ์ƒํƒœ ํ™•์ธ

    [mysql]

    show slave status\G;


    โ— ์ƒํƒœ์—์„œ ํ•„์ˆ˜๋กœ ํ™•์ธ

    1. Master ๊ด€๋ จ ์„ค์ •๋“ค
    2. Slave_IO_Running: Connection or YES
    3. Slave_SQL_Running: YES

     

    [mysql] ํ”„๋กœ์„ธ์Šค ์ƒํƒœ ํ™•์ธ

    show processlist\G;

     

    no query specified

    Slave ์„œ๋ฒ„์— Error ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ฐœ์ƒํ•˜์˜€๋˜ ์‹œ์ ์œผ๋กœ๋ถ€ํ„ฐ Master ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๊ฐฑ์‹ ๋œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ Error๋ฅผ ๋„˜๊ฒจ์•ผ ๋‹ค์Œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Slave ์„œ๋ฒ„์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋„ฃ๊ณ  restart ํ•ด์ค€๋‹ค.

    vi /etc/mysql/mysql.conf.d/mysqld.cnf

    slave-skip-errors=all ์ถ”๊ฐ€

    [mysqld]
    ...
    slave-skip-errors=all

     

    Replication ํ…Œ์ŠคํŠธ

    ๋ฌธ์ œ ์—†์ด Replication ์„ค์ •์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด, ์‹ค์ œ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณธ๋‹ค.
    master DB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , slave DB์— ๋ณต์ œ๋˜์–ด ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

     

    ๋Œ“๊ธ€

Designed by Tistory.