简体   繁体   中英

What does mysqldump do when trying to restore a database?|

I've been trying to find a good way to back up and restore a database and came upon the mysqldump . I've been using the command prompt to access the executable and I'm able to create a back up of the database in an sql file using the command:

mysqldump.exe -u [username] -p [databasename] -h localhost > [path]\backup.sql  

then I enter my password for it to execute.
Incase it wasn't understood:

[username]=my username for accessing the database  
[databasename]=my name for the database that I'm trying to back up  
[path]=the folder path to where I want to save my back up.  

With this command I'm able to create a back up and I've looked and found that the way to restore it would just be to flip the > to < so you would get this:

mysqldump.exe -u [username] -p [databasename] -h localhost > [path]\backup.sql  

then I enter my password for it to execute.
When it executes it prints out what it's dumping (doesn't create new tables if I deleted some that were supposed to be restored). I think it only adds values not already in the table since I added a row and it wasn't deleted when I tried restoring a previous version of the database.
I can execute the backup.sql file normally using a client or command prompt commands, but I just want to understand why the mysqldump isn't doing what I expect.

mysqldump is for backup only, you can't use it to load the data from dump. mysqldump produces a dump file, which just contains SQL commands necessary to recreate database structure and repopulate it with data.

To restore feed the dump into mysql.exe .

When you do

mysql -u[username] -p[root_password] [database_name] < dumpfile.sql

you basically login into MySQL, then you ask it to perform all the SQL operations saved in the dumpfile.sql It's that simple.

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