7

I want to backup two tables: table1 and table2.

table1 is from database database1.

table2 is from database database2.

Is there a way to dump them with a single mysqldump call?

I know I can do:

mysqldump -S unixSocket --skip-comments --default-character-set=utf8 --databases database1 --tables table1 > /tmp/file.sql

But how to dump two tables from different databases?

5
  • don't have a elegant solution, why restricted to single mysqldump call only?
    – ajreal
    Commented Nov 22, 2011 at 14:39
  • Do it twice, but second time with >> /tmp/file.sql :-). Commented Nov 22, 2011 at 14:47
  • @Michał Powaga Will that append to file instead of rewriting? Commented Nov 22, 2011 at 14:58
  • @RichardKnop, yes it will append to file if exists or create new one if not exists. Redirection (computing) on Wikipedia Commented Nov 22, 2011 at 15:11
  • @Michał Powaga Ok, post it as an answer and I will accept it. Commented Nov 22, 2011 at 15:48

5 Answers 5

11

Use mysqldump twice but second time with redirect to file as append >> /tmp/file.sql.

1
  • You can't use --databases with multiple databases in combination with --tables.
    – mmalone
    Commented Jun 23, 2020 at 18:02
3

There are three general ways to invoke mysqldump:

shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases

Only the first one lets you select the database and table name, but doesn't allow multiple databases. If you use the second or third option you'll dump the selected databases (second) or all databases (third).

So, you can do it but you'll need to dump to entire databases.

As Michał Powaga stated in the comments, you might also do it twice.

first time with "> /tmp/file.sql"

second time with ">> /tmp/file.sql to append"

2

The syntax is:

mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql

Check for reference: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

Hope it helps

1
  • this dumps entire database, while the question was to dump only certain tables
    – Tomas M
    Commented Jan 16, 2018 at 8:57
2

This might be a workaround but you could ignore the other tables you DON'T want to backup.

Such as in your case:

mysqldump --databases database1 database2 --ignore-table=database1.table2 --ignore-table=database2.table1

You need to define each table you DON'T want to dump with each --ignore-table statement.

Good luck!

1

For linux/bash, oneliner:

(mysqldump dbname1 --tables table1; mysqldump dbname2 --tables table2) | gzip > dump.sql.gz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.