æ ªå¼ä¼ç¤¾ç¾ éç¤ã®æç°ã§ãã
ä»åã¯ãç§ãmysqldumpã¨ããã³ãã³ããè¦ãã¦ããåæ°å¹´ä½¿ãç¶ãã¦ãããç§ä¼ã®å¥¥ç¾©ãç´¹ä»ãã¾ãã
ã·ã¹ãã éçºã§éä¸åå ãããå¼ãç¶ãã å ´åãã¾ããªãã¼ã¹ã¨ã³ã¸ãã¢ãªã³ã°ãã¦æ¢åã®ã·ã¹ãã ãèªã¿è§£ãã¨ããããå§ã¾ãã¨æãã¾ãã
ããã®æä½ãããããDBã¯ã©ãå¤ããã®ã ããï¼ã
ã¨ããçåã«æã¿ç¶ãã¦ããæ¦ããå§ã¾ãã¾ãã
ã³ã¼ããèªã¿è§£ãã«ãã¦ããé ãããã³ã¼ãããªããä¸å®ã«ãªãã¾ãã
ã¡ã¿ããã°ã©ãã³ã°ããä½ããã§é»éè¡ã使ããã¦ããæã«ã¯çºçãããã«ãªãã¾ãã
ãããªæãã¨ããæä½ã®åå¾ã®DBã®ç¶æ ãæã£ã¦ããã¦æ¯è¼ããã°å ¨ã¦ã解決ãã¾ãã
å·®åãåºããã¨ã§ãå¤æ´ããããã¼ãã«ã¯ããã ãããã以ä¸ã§ããã以ä¸ã§ããªãã¨ããäºå®ãå¾ããã¾ãã
ã¾ããèªåã追å ããå®è£ ãæ¬å½ã«æå³ããéãã®DBå¤æ´ããã¦ããã確èªãããæã«ã使ãã¾ãã
奥義ã®èª¬æ
1. ãªã¹ãã¢ç¨ã®dumpãã¡ã¤ã«ä½æ
ããã§ã¯ "ç¾å¨ã®DBã®ç¶æ " ãdumpãã¦ãã¡ã¤ã«ã«ä¿åãã¾ãã
ã¨ããæä½ãä½åãç¹°ãè¿ãããæããã¡ãã£ã¨æä½æé ãå¤ãã¦è©¦ãããæã«ããã¤ã§ããªã¹ãã¢ï¼æä½åã«ã¿ã¤ã ã¹ãªããï¼ã§ããããã«ãã¦ããã¾ãã
2. ä½ãããã®æä½ãè¡ãåã®ç¶æ ã®ã¹ãããã·ã§ãããä½æ
ããã§ã "ç¾å¨ã®DBã®ç¶æ " ãdumpãã¦ãã¡ã¤ã«ã«ä¿åãã¾ãã
--skip-extended-insert
ã¨ãããªãã·ã§ã³ãã¤ãã¦ãINSERTæã1è¡ãã¤åºåãããããã«ãã¦dumpãä½æãã¾ãã
ã1ã®dumpã使ãã°ãããããªãã®ï¼ãã¨æãã¾ãããskip-extended-insert
ãã¤ãã¦ããªã1ã®ãªã¹ãã¢ç¨dumpãã¡ã¤ã«ã¯
insert into hoge values (ã¬ã³ã¼ã1), (ã¬ã³ã¼ã2), (ã¬ã³ã¼ã3), ...
ã¨ããæãã§ãè¤æ°ã¬ã³ã¼ããã¾ã¨ãããã¦ãã¾ãå¾è¿°ããå·®åæ¯è¼ãã§ããªããªãããã§ãã
ãããã2ã®dumpãã¡ã¤ã«ã ãã§è¯ãããããªãã®ï¼ãã¨ãæãã¾ããã1è¡ãã¤ã®dumpãã¡ã¤ã«ã¯ãªã¹ãã¢ã«æéããããã¾ãã
éã«ãããã¾ãããä½ååãã®æéå·®ãåºã¦ãã¾ãã å°éã®ãã¼ã¿ãã¼ã¹ã§ããã°ããã§ãå·®ãæ¯ãããã¾ããã
3. ä½ãããã®æä½
ããã§ã·ã¹ãã ãåããã¦ã¿ã¾ãã 試ãã¦ã¿ããæä½ããã¦ã¿ã¾ãããã
4. ä½ãããã®æä½ãè¡ã£ãå¾ã®ç¶æ ã®ã¹ãããã·ã§ãããä½æ
2ã¨åããã¹ãããã·ã§ãããä½æãã¾ãã
5. å·®åãè¦ã
æä½ã®åå¾ã®ãã¡ã¤ã«å士ãæ¯è¼ããä½ã®ãã¼ãã«ãæ´æ°ãããã®ã確èªãã¾ã
6. ãªã¹ãã¢
ãã°ãä»è¾¼ãã§å度試ãããã¨ãããã©ã¡ã¼ã¿ãå¤ãã¦æä½ãå度試ãããæã«ã¯1ã®ãã¡ã¤ã«ã使ã£ã¦ãªã¹ãã¢ããã¾ãã ä½ãããã«ãèªç±ã§ãã
ã ã£ã¦ãã¤ã§ããã®é ã«æ»ããã®ã ããã
奥義ã®å®è·µ
ãµã³ãã«DBã»ãã¼ãã«ä½æ
create database example_db; create table reservations ( id bigint auto_increment primary key, date datetime );
1. ãªã¹ãã¢ç¨ã®dumpãã¡ã¤ã«ä½æ
mysqldump -h 127.0.0.1 -u xxx -pxxx example_db > example_db.2024-12-03.before-extended-insert.dump
2. ä½ãããã®æä½ãè¡ãåã®ç¶æ ã®ã¹ãããã·ã§ãããä½æ
mysqldump -h 127.0.0.1 -u xxx -pxxx example_db --skip-extended-insert --skip-dump-date > example_db.2024-12-03.1.dump
3. ä½ãããã®æä½
insert into reservations values(null, now());
ï¼ä½ãããã®æä½ããçµæãreservationsãã¼ãã«ã«ã¬ã³ã¼ãã1ã¤è¿½å ãããã¨ããç¶æ³ï¼
4. ä½ãããã®æä½ãè¡ã£ãå¾ã®ç¶æ ã®ã¹ãããã·ã§ãããä½æ
mysqldump -h 127.0.0.1 -u xxx -pxxx example_db --skip-extended-insert --skip-dump-date > example_db.2024-12-03.2.dump
5. å·®åãè¦ã
diff example_db.2024-12-03.1.dump example_db.2024-12-03.2.dump 23c23 < ) ENGINE=InnoDB DEFAULT CHARSET=latin1; --- > ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; 29a30 > INSERT INTO `reservations` VALUES (1,'2024-12-03 03:09:38');
AUTO_INCREMENTã®åæå¤ã®å·®åããã¤ãºã¨ãªã£ã¦ãã¾ãããã¾ãç¡è¦ãã¾ããã
6. ãªã¹ãã¢
mysql -h 127.0.0.1 -u xxx -pxxx example_db < example_db.2024-12-03.before-extended-insert.dump