15

I exported 2 identical databases(identical in terms of names and structures of tables) into two .sql files using mysqldump. I want to merge them into one file. However, both the databases have a 'Drop table' line before every table. What that means is if I import db1 and then db2, db1 tables are dropped before db2 tables are imported.

The files are huge and I am not able to open them in the editor. Also, there are 50 tables in each databases.

How can I ignore the Drop table command during mysql import?

zenCoder
  • 740
  • 4
  • 13
  • 31

3 Answers3

44

All you need is to add --skip-add-drop-table option when using mysqldump.

$ mysqldump --databases --skip-add-drop-table -u root db1 > /tmp/qqq.2

So, there would not DROP TABLE IF EXISTS in sql files.

see docs of mysql on --skip-add-drop-table

GoingMyWay
  • 16,802
  • 32
  • 96
  • 149
18

If you do not want to make dump once again and you are using Linux you can go with:

awk '!/^DROP TABLE IF EXISTS/{print}' <dump.file> | mysql <db_name>

If you want to dump data once again you should pass --skip-add-drop-table to mysqldump utility.

ravnur
  • 2,772
  • 19
  • 28
  • 2
    No need to edit the file if you can pass a mysqldump argument doing what you want. See @GoingMyWay answer. – Yohan Obadia Jun 15 '18 at 15:16
  • No need to if you _know_ what you will want to do with the backup later. However, it's easier to filter it out, than add it in if it's missing and you want it. – Ian Jul 08 '21 at 11:36
1

I guess I don't see why a DROP TABLE statement should be problematic or why you need to merge dumps for two IDENTICAL databases.

That being said, you should probably just not add DROP TABLE in the initial dump. This would be controlled via flag use in your mysqldump command as noted in the documention at http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html

This probably means you need to use --skip-opt flag if you were using default options (default is to run as if --opt flag is passed). You will then need to specify all flags within --opt that you still want to use.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103