generating database with different name from mysqldump backup

半腔热情 提交于 2020-01-01 09:42:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!