ãµã¤ããã§ããã ãæ¢ããã«MySQLãã¼ã¿ãã¼ã¹ã®åæ§ç¯ãè¡ãæ¹æ³
my.cnfã§innodb_file_per_tableãæå®ããibdata1ãã¡ã¤ã«ãè¨ãä¸ãã£ãæãªã©ãå¾ããã¬ã³ã¼ããåé¤ãã¦ããã¡ã¤ã«ã縮å°ãããã¨ãã§ããªãçºããã£ã¹ã¯ä½¿ç¨éãæ¸ããããã«ã¯ãã¼ã¿ãã¼ã¹ã®åæ§ç¯ãè¡ãå¿ è¦ãããã¾ãã
åæ§ç¯ã¯mysqldumpã§ããã¯ã¢ãããã¦ããã¦ãmysql_install_db ã§åæåããæ°ãããã¼ã¿ãã¼ã¹ã«æµãè¾¼ãã ãã§ãã
å ¨ä½ã®ãµã¤ãºãæ¯è¼çå°ããå ´åã¯çæéã§æ¸ãã®ã§ã¡ã³ããã³ã¹æéãçãã¦æ¸ã¿ã¾ããããµã¤ãºã大ãããªãã¨åæ§ç¯ã«æéããããããã«ãªãå¤éã¡ã³ãã®æéã ãã§ã¯çµãããªããªã£ã¦ãã¾ãã
ãµã¤ããæ¢ãã¦ããæéãæå°ã«ãã¦ããµã¤ãã«ã§ããã ãè² è·ãæããã«åæ§ç¯ããæ¹æ³ãè¨è¼ãã¦ããã¾ãã
ã¨ã¯è¨ããã³ãããã¨ãã¨åãæ¿ããæã§æä½2åæ¢ããå¿ è¦ãããã¾ãã®ã§ãå¤ä¸ãªã©ã¢ã¯ã»ã¹ã®å°ãªãã¿ã¤ãã³ã°ã§ä½æ¥ãã¾ãããã
ä»åã®å¯¾è±¡ã¯ä¸è¨ã®å ´åã対象ã¨ãã¦ãã¾ãã
- ãµã¼ãã¼1å°ã§éç¨ãã¬ããªã±ã¼ã·ã§ã³ãåã£ã¦ããªã
- åæ§ç¯ã§ããç¨åº¦ã®ç©ºã容éã¯ãã
- LVMãªã©ã§ãã¹ãããã·ã§ãããåããç°å¢ããªã
æ³å®ã¨ãã¦ã¯å¤§ä½ä¼å¡æ°ãæ°ä¸äººè¦æ¨¡ã®æ¯è¼çå°ããªãµã¤ãã対象ã§ãã
æé
æé ãç°¡åã«èª¬æããã¨ä¸è¨ã®æµãã«ãªãã¾ãã
- ãã¤ããªãã°ãæ®ã
- mysqldumpã§ãã«ãã³ããã
- æ°ãã¼ã¿ãã¼ã¹ã®ä½æ
- åä¸ãµã¼ãã¼å ã«å¥ããã»ã¹ã§mysqldãæ°ããç«ã¡ä¸ãããã³ããããã¼ã¿ãæ°ãã¼ã¿ãã¼ã¹ã«æµãããã
- ã¹ã¬ã¼ãè¨å®
- æ°ãã¼ã¿ãã¼ã¹ãæ§ãã¼ã¿ãã¼ã¹ã®ã¹ã¬ã¼ãã«è¨å®ãã¦åæãã¨ãã
- æ°ãã¼ã¿ãã¼ã¹ã¸ã®åãæ¿ã
- åæãå®äºããæç¹ã§ã¿ã¤ãã³ã°ãè¦ã¦ãµã¤ããåæ¢ããæ°ãã¼ã¿ãã¼ã¹ã«åãæ¿ããã
ç°¡åã«è¨ãã¨ã¹ã¬ã¼ããµã¼ãã¼ãç«ã¦ã¦ãåæãã¨ããæç¹ã§ä¸åº¦åæ¢ãã¦åãæ¿ããã¨ãããã¨ã§ãã
Step.1 ãã¤ããªãã°ãæ®ã
ã¾ããã¬ããªã±ã¼ã·ã§ã³ã®ããã«ãã¤ããªã¼ãã°ãæ®ãã¾ãã
/etc/my.cnfãç·¨éãè¨å®ãè¡ãã¾ãã
[mysqld] server-id=1 log-bin=/var/lib/mysql/binary.log expire_logs_days=2
mysqldãåèµ·åãã¾ãã
service restart mysql
Step.2 mysqldumpã§ãã«ãã³ãããã
次ã«ãã«ãã³ããè¡ãã¾ãã
ãã¼ã¿ãã¼ã¹ãããã¯ãããã®ã§ãããã ãã¯ç¢ºå®ã«åæ¢ãã¾ãã
mysqldump --all-databases --flush-logs --master-data --add-drop-table --extended-insert --add-locks -uUSERNAME -pPASSWORD -h127.0.0.1 -x > mysqldump.sql
ãã³ããçµãã£ããã¨ã¯åãæ¿ããã¾ã§éç¨å¯è½ã§ãã
Step.3 æ°ãã¼ã¿ãã¼ã¹ã®ä½æ
次ã«ä»ç¨¼åãã¦ãããã¼ã¿ãã¼ã¹ã¨ã¯å¥ã«æ°ãã¼ã¿ãã¼ã¹ç¨ã«ãã¼ã¿ ãã£ã¬ã¯ããªãåæåãã¾ãã
ä»åã®ä¾ã§ã¯ç¨¼åä¸ã®ãã¼ã¿ã/var/lib/mysqlã«ãããã®ã¨ãã¦ãåä¸ãã¼ãã£ã·ã§ã³ã®/var/lib/mysql_newã«ä½æãã¾ãã
mysql_install_db --datadir=/var/lib/mysql_new --user=mysql
æ¢åã®my.cnfãã³ãã¼ãæ°ãã¼ã¿ãã¼ã¹ç¨ã®è¨å®ãã¡ã¤ã«ãä½æãã¾ãã
cp -p /etc/my.cnf /etc/my_new.cnf
/etc/my_new.cnfã®ä¸èº«ãç·¨éãã¾ãã
以ä¸ã«ç·¨éç®æãæç²ãã¾ãã
[client] port=3307 socket=/var/lib/mysql_new/mysql.sock [mysqld] port=3307 datadir=/var/lib/mysql_new socket=/var/lib/mysql_new/mysql.sock server-id=2 pid-file=/var/run/mysqld/mysqld_new.pid [mysqld_safe] log-error=/var/log/mysqld_new.log pid-file=/var/run/mysqld/mysqld_new.pid
ãã¼ãã¯ããã©ã«ãã§ã¯3306ã§ãããéè¤ããªãããã«3307ã使ç¨ãã¦ãã¾ãã
datadirã¯æ°ãã¼ã¿ãã¼ã¹ç¨ã®/var/lib/mysql_newã«ãserver-idã¯å¥ã®idã«å¤ãã¦ããã¾ãã
socketãpid-fileãããããå¤æ´ãã¾ãã
å¥ããã»ã¹ã§mysqlãç«ã¡ä¸ãã¾ãã
ioniceã¨niceã使ç¨ãã¦ãã©ã¤ãªãªãã£ãä¸ãã¦ããã¾ãã
nohupã¨niceã¯mysqld_safeã§è¡ãã¾ãã
ionice -c3 mysqld_safe --defaults-file=/etc/my_new.cnf --nice=19 >/dev/null 2>&1 &
ãã³ãããSQLãæµããã¿ã¾ãã
ãã®æãioniceã¨niceã§ãã©ã¤ãªãªãã£ãä¸ãã¦ããã¾ãã
cd /var/tmp nohup ionice -c3 nice -n 19 mysql -uroot -h 127.0.0.1 -P 3307 < mysqldump.sql >/dev/null 2>&1 &
SQLã®å®è¡ãå®äºããã¾ã§æ«ãå¾
ã¡ã¾ãã
Step.4 ã¹ã¬ã¼ãè¨å®
ãã³ãããSQLãæµãè¾¼ãã ãã¨ãæ§ãã¼ã¿ãã¼ã¹ããæ°ãã¼ã¿ãã¼ã¹ã¸ã®ã¹ã¬ã¼ãè¨å®ãè¡ãåæãã¨ãã¾ãã
ã¾ãã¯ããã¸ã·ã§ã³ã調ã¹ã¦ããã¾ãã
ã³ãã³ã
head -n 100 mysqldump.sql |grep 'CHANGE MASTER TO'
ã¬ã¹ãã³ã¹
CHANGE MASTER TO MASTER_LOG_FILE='binary.000175', MASTER_LOG_POS=106;
ãã£ããåæããããã®ãããã¹ã¯ãªãã slow_repl.shãä½æãã¾ãã
#!/bin/bash MASTER_HOST=127.0.0.1 MASTER_PORT=3306 MASTER_USER=XXXXXXXX MASTER_PASSWORD=XXXXXXXX MASTER_LOG_FILE=binary.000175 MASTER_LOG_POS=106 SLAVE_HOST=127.0.0.1 SLAVE_PORT=3307 SLAVE_USER=XXXXXXXX SLAVE_PASSWORD=XXXXXXXX OPTIONS="-u ${SLAVE_USER} -p${SLAVE_PASSWORD} -h ${SLAVE_HOST} -P ${SLAVE_PORT}" mysql $OPTIONS -e " CHANGE MASTER TO MASTER_HOST='${MASTER_HOST}', MASTER_PORT=${MASTER_PORT}, MASTER_USER='${MASTER_USER}', MASTER_PASSWORD='${MASTER_PASSWORD}', MASTER_LOG_FILE='${MASTER_LOG_FILE}', MASTER_LOG_POS=${MASTER_LOG_POS}" echo "`date` START SLAVE"; mysql $OPTIONS -e "START SLAVE"; sleep 1; BEHIND=`mysql $OPTIONS -e "show slave status\G" |grep Seconds_Behind_Master|cut -f 2 -d ':'` while [ ${BEHIND} -gt 0 ] do echo "`date` STOP SLAVE"; mysql $OPTIONS -e "STOP SLAVE IO_THREAD"; sleep 5; echo "`date` START SLAVE"; mysql $OPTIONS -e "START SLAVE IO_THREAD"; sleep 1; BEHIND=`mysql $OPTIONS -e "show slave status\G" |grep Seconds_Behind_Master|cut -f 2 -d ':'` echo "`date` Seconds_Behind_Master:${BEHIND}"; done
slow_repl.shãå®è¡ãåæãã¾ãã
chmod +x slow_repl.sh ./slow_repl.sh
åæã追ãã¤ãã¾ã§æ«ãå¾
ã¡ã¾ãã
Step.5 æ°ãã¼ã¿ãã¼ã¹ã¸ã®åãæ¿ã
mysqldãåæ¢ããæ°ãããã¼ã¿ ãã£ã¬ã¯ããªã«åãæ¿ããmysqldãéå§ãã¾ãã
以ä¸ã®ã¹ã¯ãªããã«é©å½ãªåå(change_new.shãªã©)ãä»ãã¦ä¿åãã¾ãã
#!/bin/bash service stop mysql sleep 3 mysqladmin --defaults-file=/etc/my_new.cnf shutdown mv /var/lib/mysql /var/lib/mysql_old mv /var/lib/mysql_new /var/lib/mysql service start mysql
ã¹ã¯ãªãããå®è¡ãã¾ãã
chmod +x change_new.sh ./change_new.sh
以ä¸ããããªæãã§ã
P.S.
Percona Xtra Backupã¨ã使ãã¨ãæ¢ããã«ä½æ¥ãå¯è½ããããã¾ããã
試ãããã¨ãããæ¹ãããããã°æ¯éãç´¹ä»ããã ããã°ã¨æãã¾ãã