问题
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!
回答1:
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 :)
回答2:
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
回答3:
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.
回答4:
Open up the .sql file and change the database name inside.
You can use a text editor, like Notepad or gedit.
来源:https://stackoverflow.com/questions/5993617/generating-database-with-different-name-from-mysqldump-backup