Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysqldump tables from different databases?

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?

like image 357
Richard Knop Avatar asked Nov 22 '11 14:11

Richard Knop


4 Answers

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

like image 94
Michał Powaga Avatar answered Sep 30 '22 20:09

Michał Powaga


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

like image 28
Sudhir Bastakoti Avatar answered Sep 30 '22 20:09

Sudhir Bastakoti


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"

like image 43
aF. Avatar answered Sep 30 '22 21:09

aF.


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!

like image 32
user3912822 Avatar answered Sep 30 '22 19:09

user3912822