Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

How can we restore mysql database with different name from mysqldump file. I dont't want to open dump file and edit it. Any other better methods?

share|improve this question

6 Answers 6

up vote 15 down vote accepted

You can let mysqldump create the dump in such a way that it does not create or select the database.

EXAMPLE : You are dumping the database db1 and loading it into database db2

This will put in the CREATE DATABASE and the USE commands in the dump

mysqldump -u... -p... --routines --triggers --databases db1 > /root/db1.sql

This will not put in the CREATE DATABASE and the USE commands in the dump (this is what you want)

mysqldump -u... -p... --routines --triggers db1 > /root/db1.sql

You can load it into another database (such as db2) in one of four(4) ways:

OPTION 1

$ mysqldump -u... -p... --routines --triggers db1 | mysql -u... -p... -A -Ddb2

OPTION 2

$ mysqldump -u... -p... --routines --triggers db1 > /root/db1.sql
$ mysql -u... -p... -A -Ddb2 < /root/db1.sql

OPTION 3

$ mysqldump -u... -p... --routines --triggers db1 > /root/db1.sql
$ mysql -u... -p... -A -Ddb2
mysql> source /root/db1.sql

OPTION 4

$ mysqldump -u... -p... --routines --triggers db1 > /root/db1.sql
$ mysql -u... -p... -A
mysql> use db2
mysql> source /root/db1.sql

Give it a Try !!!

share|improve this answer
    
Thanks RolandoMySQLDBA,,, It worked..:) –  Praveen Prasannan Dec 8 '11 at 13:16

I have done this once, long time ago.

When you export all your data, there is an option to set the database name in the begining of the file, something as: "use database x"

So, you can change this declaration.

share|improve this answer

Here is the shell script that will allow you to add suffix / affix to all the schema names on the fly.

http://snippets.dzone.com/posts/show/13749

share|improve this answer

I am a big fan of dump, edit and insert. but you do not have to open the text file (dump file) to change it (this is especially usefull when it is several milion lines long). if you want to dump the database MYDATABASE.

mysqldump MYDATABASE > mydump.sql

then use sed to replace the old database name with new one like this

sed -i 's/MYDATABASE/MYNEWDATABASE/g' mydump.sql

then you can simply create the new database and import it again, and it will create all tables with in the new databae MYNEWDATABASE'

mysqladmin create MYNEWDATABASE

mysql MYNEWDATABASE < mydump.sql
share|improve this answer
    
I think the question was about restoring an enitre database but with a different name, not just copying one table within an existing DB. –  Michael Green Jul 3 '14 at 10:54
    
oh.. good point, have fixed. (not sure if the sed part if much help now, but I leave it in there, just in case someone need to change something in the dumpfile) –  user1201232 Jul 4 '14 at 9:56
1  
the sed part is needed –  Steve Buzonas Jan 22 at 0:22
1  
This command is a bad idea and I would down vote if I could. If you are going for string replacements, at least match the line CREATE TABLE and USE so you don't replace any other data. –  bksunday Jul 20 at 22:41
    
ok, good feedback, if you have spesific I can edit my answer –  user1201232 Jul 22 at 2:04

If you do a dump using the following two rules:

  1. Do not use options such as --databases, --database, and simply just put the database name at the end of the command without these options.
  2. Include the option --no-create-db

If you do the following then mysqldump will create the SQL without referencing the database, then you can use the new database name at the end of your mysql command during import!

mysqldump  --no-create-db old_db_name --single-transaction --compress --order-by-primary --host old_db_host -u old_db_user -pOld_db_password | mysql --host new_host -u new_user -pnewpassword new_db_name
share|improve this answer

If the file is what you have in hand and you are manipulating it from a shell / console, I would use sed to do string replacements on lines starting with CREATE DABATASE, USE and optionnally -- (mysqldump comments)

Replacing the db name on lines that matches Create Database, Use and mysqldump comments

dbfile="yoursqldumpfile.sql";
dbname="current_db_name";
dbnewname="new_db_name";
dbnewfile="/tmp/$dbnewname.sql";
cat $dbfile | sed "/^CREATE DATABASE/ s=$dbname=$dbnewname=" | sed "/^USE / s=$dbname=$dbnewname=" | sed "/^-- / s=$dbname=$dbnewname=" > $dbnewfile


Of course like many of the answers here mentions, it's easier if the mysqldump backup did not contain a CREATE DATABASE and a USE line. Meaning the options --all-databases, --databases or their short versions -A or -B were not used.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.