简体   繁体   中英

Restoring selective tables from an entire database dump?

I have a mysql dump created with mysqldump that holds all the tables in my database and all their data. However I only want to restore two tables. (lets call them kittens and kittens_votes )

How would I restore those two tables without restoring the entire database?

Well, you have three main options.

  1. You can manually find the SQL statements in the file relating to the backed up tables and copy them manually. This has the advantage of being simple, but for large backups it's impractical.

  2. Restore the database to a temporary database. Basically, create a new db, restore it to that db, and then copy the data from there to the old one. This will work well only if you're doing single database backups (If there's no CREATE DATABASE command(s) in the backup file).

  3. Restore the database to a new database server, and copy from there. This works well if you take full server backups as opposed to single database backups.

Which one you choose will depend upon the exact situation (including how much data you have)...

You can parse out CREATE TABLE kittens|kitten_votes AND INSERT INTO ... using regexp, for example, and only execute these statements. As far as I know, there's no other way to "partially restore" from dump.

打开.sql文件,然后复制所需表的插入语句。

What you want is a "Single Table Restore"

http://hashmysql.org/wiki/Single_table_restore

A few options are outlined above ... However the one which worked for me was:

  • Create a new DB

$ mysql -u root -p CREATE DATABASE temp_db

  • Insert the .sql file ( the one with the desired table ) into the new DB

$ mysql -u root -p temp_db < ~/full/path/to/your_database_file.sql

  • dump the desired table

$ mysqldump -u root -p temp_db awesome_single_table > ~/awesome_single_table.sql

  • import desired table

$ mysql -u root -p original_database < ~/awesome_single_table.sql

Then delete the temp_db and you're all golden!

create a new user with access to only those 2 tables. Now restore the DB with -f (force) option that will ignore the failed statements and execute only those statements it has permission to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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