views:

45

answers:

2

I made a backup of my_database like this:

mysqldump --add-drop-database --databases 
          --user=my_username --password=my_password my_database > backup.sql

Then I deleted the database completely, and tried to restore it like this:

mysql --user=my_username --password=my_password my_database < backup.sql

I got the following error:

ERROR 1049 (42000): Unknown database 'my_database'

What am I doing wrong ?

I need to be able to restore the database if it was changed somehow or removed completely.

Bonus question: is that possible to read the password from a file rather than providing it in the command line ?

+1  A: 

If I understand correctly, what you are doing, then you do not need a 'database' argument for the 'mysql' invocation, since the dump should contain the create database statements.

As for the bonus: you want a --defaults-extra-file option

shylent
Thanks a lot !!
Misha Moroshko
A: 

The mysqldump command does not include the create database statement in its output. You need to create the my_database database before running the second command.

Ash White
Au contraire, it most definitely does include the `create database` statement. Did you try it?
shylent
@Ash White: Please read carefully through the mysqldump invocation in the question and the excerpt from mysqldump documentation in your comment. As a matter of fact, I've just run mysqldump in the very same way, as done in the question. `create database` statement is there. Am I dreaming?
shylent
@skylent, You're right, I didn't see that the OP had included the --databases flag, which causes the `create database` statement to be included. It is worth noting that the `create database` statement is NOT included unless --databases or --all-databases is used.
Ash White