Forum Moderators: open

Message Too Old, No Replies

Howto Override Create Database During Import

         

HoboTraveler

6:18 am on Aug 30, 2008 (gmt 0)

10+ Year Member



Hello,

I have this 100MB MySQL dump file. I am trying to import this dump into a database of a different name.

However, the dump file tries to create a database. I want to override the create database and import it to a different database of a different name.

Is it possible to use a different database name during import?

It is not easy to edit this file through SSH. Plus the dump contains UTF-8 characters so editing seems impossible.

The MySQL dump contains these lines:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database_name` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `database_name`;

Can I override this somehow through the command line import tool?

TIA

coopster

5:02 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Rather than that, skip the create database statements [dev.mysql.com] in your original dump statement. Much easier.

HoboTraveler

7:31 pm on Aug 30, 2008 (gmt 0)

10+ Year Member



That is a problem. I cannot re-generate the dump.

All I have is a dump file. Is it possible to skip the create database statement during import?

TIA

coopster

9:28 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could always use command line tools to find/replace the text in the dump file itself with your new database name.

Or you could import the file into your own development version/installation of MySQL and then dump it back out in the format you desire in order to move it on over and up to your new server.

Just some thoughts.

phranque

11:41 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



assuming a *nix command line you could do something like:
egrep -v '^CREATE DATABASE ¦^USE ' dump.sql ¦ mysql database_name

(replacing the broken pipe symbol with an actual pipe, of course)

HoboTraveler

8:11 am on Aug 31, 2008 (gmt 0)

10+ Year Member



WoW! That worked!

I'm confused though. What does egrep do? Replace the CREATE DATABASE with nothing? or does it comment out the line?

This is really great phranque.

Thanks!

phranque

8:26 am on Aug 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



egrep is a version of grep that lets you OR the regular expressions.
some versions of grep also work that way.
the regular expressions i used mean lines starting with "CREATE DATABASE " or "USE ".
-v is an option that inverts the match.
it therefore selects non-matching lines and passes them to grep's standard output which then gets piped into mysql's standard input.

HoboTraveler

10:33 am on Aug 31, 2008 (gmt 0)

10+ Year Member



So what your saying is egrep finds the pattern, skips that line and passes the rest to mysql's standard input, Correct?

TIA

phranque

10:53 am on Aug 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



yes, so keep in mind if the file is something like this it might not work correctly:
CREATE DATABASE /*!32312 IF NOT EXISTS*/
`database_name` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE
`database_name`;

HoboTraveler

12:29 pm on Aug 31, 2008 (gmt 0)

10+ Year Member



hmm.. I see the problem. I guess that would create an issue.

Thanks for that.