简体   繁体   English

恢复mysql数据库时如何跳过表?

[英]How to skip a table when restoring mysql database?

I have a big mySQL database dump named forum.sql. 我有一个名为forum.sql的大型mySQL数据库转储。 I want to restore only one table, but when I restore the full database, it takes a long time to import the "post" table. 我想只恢复一个表,但是当我恢复完整数据库时,导入“post”表需要很长时间。

Is there any option to restore this database skipping the "post" table? 是否有任何选项可以恢复此数据库跳过“post”表?

If you are restoring from a dump file, you can easily build a new dumpfile without this table, just by writting down the line numbers. 如果要从转储文件进行恢复,只需写下行号,就可以轻松地在没有此表的情况下构建新的转储文件。

Initial line 初始线

> grep dumpfile.sql -ne "Dumping data for table \`avoid_tablename\`" -m 1
43:-- Dumping data for table `avoid_tablename`

Total lines 总行数

> wc -l dumpfile.sql
63 dumpfile.sql

Make a new file 制作一个新文件

> head -n 43 dumpfile.sql > dumpfile-lite.sql
> tail -n 20 dumpfile.sql >> dumpfile-lile.sql

20 comes from substracting 63 - 43 20来自减去63 - 43

not clean, but usefull 不干净,但有用

I don't think you can do it. 我不认为你能做到。 But you can dump tables separately when necessary, using --tables myqsldump option. 但是,您可以在必要时使用--tables myqsldump选项单独转储表。 So, you can generate a dump for post table and another dump for the remaining tables. 因此,您可以为post表生成转储,为剩余的表生成另一个转储。

Example: 例:

mysqldump -u USERNAME -pPASSWORD --tables TABLE_NAME database_name > TABLE_NAME.sql

Restore a single table 恢复单个表

First, do the restore with zero inserts: 首先,使用零插入执行恢复:

cat dump.sql | grep -v '^INSERT INTO' | mysql -u <user> -p<pw> <dbname>

Using grep -v here will exclude any statements matching the pattern. 在这里使用grep -v将排除与模式匹配的任何语句。 The pattern in this case uses ^ to match at the beginning of a line. 在这种情况下,模式使用^来匹配行的开头。 The result should be a restored schema with zero data. 结果应该是具有零数据的恢复模式。

Next, restore only your desired INSERT statements: 接下来,仅还原所需的INSERT语句:

cat dump.sql | grep '^INSERT INTO \\\`<table>\\\`' | mysql -u <user> -p<pw> <dbname>

That will restore data only for the table named <table> . 这将仅恢复名为<table>的表的数据。 Note the triple backslashes. 注意三重反斜杠。 You need a backslash to escape a backtick and then you need to escape the backslash with 2 more backslashes. 你需要一个反斜杠来逃避反击,然后你需要用另外两个反斜杠来逃避反斜杠。

Restore everything except one table 恢复除一个表外的所有内容

Another technique I use all the time when I want to restore an entire database but exclude the data from a table or two is this... You can filter out any unwanted INSERT statements by passing your dump through a filter before pushing into the db. 我想要恢复整个数据库但是从一个或两个表中排除数据时我一直使用的另一种技术是......你可以通过在推入数据库之前将转储通过过滤器来过滤掉任何不需要的INSERT语句。 Here's an example using grep as the filter: 以下是使用grep作为过滤器的示例:

nohup sh -c "cat dump.sql | grep -v 'INSERT INTO \\\`<table>\\\`' | mysql -u <user> -p<pw> <dbname>" &

The nohup command will keep the sh command running even if you log out of your shell. 即使您退出shell, nohup命令也会使sh命令保持运行。 That can be pretty handy if you have a large dump file that will take quite some time to restore. 如果你有一个需要很长时间才能恢复的大型转储文件,这可能非常方便。

The -v flag for grep will exclude anything matching the pattern. grep-v标志将排除与模式匹配的任何内容。

The & at the end will send the command to the background. &最后将命令发送到后台。

As far as I know, no. 据我所知,没有。

You would have to manually edit the CREATE and INSERT statements of the undesired table out of the dump file. 您必须从转储文件中手动编辑不需要的表的CREATEINSERT语句。

You could alter your dump statement so that it uses ignore table? 您可以更改转储语句,以便它使用忽略表吗? http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_ignore-table http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_ignore-table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM