Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a new database name on import with mysql and php

I have a large database dump.sql file I am importing from the command line in linux. The .sql dump creates a database named "database_name". I want to import the database from this .sql file but I want to force it to a database with a different name, as the script currently overwrites "database_name" and "database_name" already exists and has data I can't overwrite.

Is the best option to find and replace within the .sql file? What is the best method for that since the file is 50mb. I can't simply file_get_contents() on that shiz? Can I?

Below are the lines I would have to replace in the .sql file:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database_name` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `database_name`;
like image 635
T. Brian Jones Avatar asked Apr 28 '11 07:04

T. Brian Jones


1 Answers

When dumping the database with mysqldump, use the option --no-create-db. This will suspress the CREATE DATABASE statement in your dump file.
Then restore the database with
mysql -h <host> -u <user> -p <databaseName> < dump.sql
In this way you can restore your data in whatever database you like (But that database has to exist!)

like image 62
fab Avatar answered Sep 24 '22 22:09

fab