简体   繁体   中英

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!

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 :)

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

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.

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

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

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