Eugeny89
Eugeny89

Reputation: 3731

generating database with different name from mysqldump backup

The database "db" is backuped in backup.sql. Is there a way to restore database from script with different from "db" name?

thank you in advance!

Upvotes: 8

Views: 17198

Answers (4)

Ask and Learn
Ask and Learn

Reputation: 8959

This depends on how you created your MySQL dB dump file

for example, if you do

mysqldump -h localhost -u user mydb -pXXX > mydb.sql

There won't be any CREATE DATABASE statements in your sql dump file. But I think you can only backup one database.

If you create your mysql dump file with --database or --all-databases option for example

mysqldump -h localhost -u user --database mydb -pXXX > mydb.sql 
mysqldump -h localhost -u user --all-databases -pXXX > alldb.sql

then you will see CREATE DATABASE statement in your mysql dump file. If you want a different dB name, you will need to change it before DB restore.

Upvotes: 6

Gabriel
Gabriel

Reputation: 3857

If the name of the database is include the SQL file, I didn't find any other way than modify the SQL file.

My favorite command to do it :

    sed -i "s/\`old_db_name\`/\`new_db_name\`/g" my_sql_file.sql

Upvotes: 4

James
James

Reputation: 13501

Open up the .sql file and change the database name inside.

You can use a text editor, like Notepad or gedit.

Upvotes: 0

Henry
Henry

Reputation: 6620

Sure, when you import it you do this right:

mysql -uuser -ppassword databasename < mydump.sql

You can put anything you want where I wrote databasename - as long as that database actually exists :)

Upvotes: 12

Related Questions