mysqldump with multiple tables with or without where clause

喜夏-厌秋 提交于 2019-12-28 13:38:34

问题


I have a set of tables in my database that I have to take a dump ( :D ) of. My problem is I want to take some data from some tables that only date back certain days and would like to keep the remaining tables in tact.

The query I came up with was something like:

mysqldump -h<hostname> -u<username> -p <databasename> 
<table1> <table2> <table3> 
<table4> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)',
<table5> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)
--single-transaction --no-create-info | gzip
> $(date +%Y-%m-%d-%H)-dump.sql.gz

The trouble with the above code is that table1, table2 and table3 will try to take the where clause of table4. I don't want that cause that would spit out an error that created field does not exist in these tables.

I tried putting comma (,) after table names as I did after where clause but it doesn't work.

At this point I'm pretty much stuck and have no more alternative expect create two different sql dump files, which I wouldn't want to do.


回答1:


make two dumps or if you dont want to make two dumps then try two command

a.

mysqldump -h<hostname> -u<username> -p 
<databasename>  <table1> <table2> <table3>
--single-transaction --no-create-info > dumpfile.sql

b.

mysqldump -h<hostname> -u<username> -p <databasename> 
<table4> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)',
<table5> --where 'created > DATE_SUB(now(), INTERVAL 7 DAY)
--single-transaction --no-create-info >> dumpfile.sql

c.

gzip dumpfile.sql



回答2:


So the solution above won't work unless the tables have a common foreign key field.

If you look at my example below, the user_addresses, user_groups, and user_payment_methods all have the user_id field i common. When mysqldump executes the where clause it will filter those tables.

mysqldump -u <username> -p <password> 
user_addresses user_groups user_payment_methods 
-w "user_id 
in (select id from users where email like '%@domain.com')"
--single-transaction| gzip > sqldump.sql.gz


来源:https://stackoverflow.com/questions/32368101/mysqldump-with-multiple-tables-with-or-without-where-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!