mysql-dump-in-docker

#docker

๋ฐฐ๊ฒฝ

  • ๋กœ์ปฌ์—์„œ ์‚ฌ์šฉ ์ค‘์ธ docker(mysql) ์˜ ๊ตฌ์กฐ๋ฅผ main-repl ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์ƒ๊น€.

  • ์ฒ˜์Œ ์ƒ๊ฐ์€ mysql dump, mysql main-repl on docker ๋“ฑ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ

mysql dump, restore data

  • ์ผ๋ฐ˜์ ์ธ dump ์ปค๋งจ๋“œ๋ฅผ ์ฐพ์•„๋ณด๊ธฐ๋ณด๋‹ค๋Š”, ๊ณต์‹๋ฌธ์„œ(docker)์—์„œ ์ฐพ์•„๋ณด๋Š” ๊ฒŒ, ๋œ ๊ณ ์ƒํ–ˆ์„๋“ฏ ์‹ถ์Œ.

  • ์•„๋ž˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด, ๋ฉ”์ธ DB์˜ data๋ฅผ dump ํ•˜๊ณ , repl ๊ตฌ์„ฑ ํ›„, ๋™๊ธฐํ™” ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์Œ.

Creating database dumps

docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql
  • --master-data ๊ฐ™์€ ์˜ต์…˜์„ ๋ถ™์–ด์ค˜๋„ ๊ดœ์ฐฎ์„๋“ฏ, ์ฐธ๊ณ 

  • all-database.sql ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ dump ๋œธ.

Restoring data from dump files

docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql

Setting Up Binary Log File Position Based Replication

  • replica ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋„์ปค์— ๋„์šด ์ƒํƒœ๋ผ ๊ฐ€์ •.

  • replica db์—๋Š” ์•„๋ฌด๋Ÿฐ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๊ฐ€ ์—†์œผ๋ฉฐ, configuration์€ ๋ฉ”์ธ๊ณผ ๋™์ผํ•œ ์ƒํƒœ

Setting the Replication Source Configuration

server-id=1
log-bin=mysql-bin
  • server-id, main-repl์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด

  • log-bin, Binary logging ์„ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด

    • ์ด ๊ธฐ๋Šฅ์„ ํ†ตํ•ด์„œ, ๋ฉ”์ธ๋””๋น„์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ๊ธฐ๋ก๋˜๊ณ , ์ด ๋กœ๊ทธ ๊ธฐ๋ก์„ ํ†ตํ•ด ๋ณต์ œ๋””๋น„์— ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ๋ณต์ œ๋œ๋‹ค๊ณ  ์ดํ•ด๋จ.

Creating a User for Replication

CREATE USER 'repl'@'%.example.com' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.example.com';
  • ๋ฉ”์ธ ๋””๋น„์—์„œ, replication์— ์‚ฌ์šฉํ•  ์œ ์ €๋ฅผ ์ƒ์„ฑ

Obtaining the Replication Source's Binary Log Coordinates

mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 73       | test         | manual,mysql     |
+------------------+----------+--------------+------------------+
  • repl ์—์„œ master ์„ค์ •ํ•  ๋•Œ ํ•„์š”ํ•œ ์ •๋ณด๋“ค

Choosing a Method for Data Snapshots

  • ์œ„ ์ฒซ๋ฒˆ์งธ ๋ฌธ๋‹จ ์ฐธ๊ณ ํ•ด์„œ, dump ์‹คํ–‰

Setting Up Replicas

server-id=2
  • configuration ํŒŒ์ผ์—, replica๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ๋‹ˆํฌํ•œ ์„œ๋ฒ„ ์•„์ด๋”” ์„ค์ •

Setting the Source Configuration on the Replica

CHANGE MASTER TO
MASTER_HOST='mysql-master',
MASTER_USER='replication',
MASTER_PASSWORD='Slaverepl123',
MASTER_LOG_FILE='87e8982d00d1-bin.000004',
MASTER_LOG_POS=349;
  • repl db์—์„œ

  • LOG_FILE ์€ Main ์—์„œ SHOW MASTER STATUS ๋ฅผ ํ†ตํ•ด ๋ณด์ด๋Š” File ๊ฐ’

    • log-bin ์— ์„ค์ •๋œ ๊ฐ’์ด prefix๋กœ ๋ถ™์–ด์žˆ๋Š” ๋“ฏ.

  • LOG_POS ๋Š” ์œ„ ๋ช…๋ น์˜ ๊ฒฐ๊ณผ ๊ฐ’ ์ค‘์— Position

Setting Up Replication between a New Source and Replicas

  • ์œ„ ์ฒซ๋ฒˆ์งธ ๋ฌธ๋‹จ ์ฐธ๊ณ ํ•ด์„œ, repl ์— ๋ฐ์ดํ„ฐ restore ์‹คํ–‰

  • ๋ฉ”์ธ, ๋ณต์ œ ๋””๋น„์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋˜‘๊ฐ™์€ ์ƒํƒœ์—์„œ, replication์ด ์‹œ์ž‘๋˜์–ด์•ผ ๋ฐ์ดํ„ฐ์˜ ์ •ํ•ฉ์„ฑ์ด ๋งž๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Œ.

Start the replication threads

START SLAVE;
  • repl์—์„œ, SLAVE ํ™œ๋™ ์‹œ์ž‘

  • ์ด์ œ ๋ฉ”์ธ ๋””๋น„์—์„œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ, ํ˜น์€ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ๋“ฑ์ด repl์— ์ž๋™์ ์œผ๋กœ ์ ์šฉ๋˜๋Š” ์ง€ ํ™•์ธํ•˜๋ฉด ๋.

Reference

  • https://hub.docker.com/_/mysql

  • https://dev.mysql.com/doc/mysql-replication-excerpt/5.7/en/replication-howto-slavebaseconfig.html

  • https://sesamedisk.com/docker-mysql-master-slave-replication-with-docker/

Last updated