Fix new database name during import using mysql and php

I have a large database dump.sql file that I import from the command line in linux. The .sql dump creates a database called database_name. I want to import a database from this .sql file, but I want to force it to use a database with a different name, since the script is currently overwriting "database_name" and "database_name" already exists and has data that I cannot overwrite.

Is the best option to search and replace in a .sql file? What is the best way to do this since the file is 50 MB. Can't I just file_get_contents () on this shiz? Can I?

Below are the lines that I need to replace in the .sql file:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database_name` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `database_name`;
+5
source share
4 answers

You must replace the database_name in the .sql file.

You can do this from the shell using sed 's/database_name/new_database_name/' dumpFile.sql > newDumpFile.sql

this is the correct answer to my question, but it was provided by "fab" in the comments of another answer.

NOTE . As @Diego noted in the comments, if the string 'database_name' appears elsewhere in this .sql dump, it will also be replaced. You might want to look at the contents of the .sql file with less file_name.sql, and then find / replace it more explicitly.

+7
source

mysqldump --no-create-db. CREATE DATABASE .
mysql -h <host> -u <user> -p <databaseName> < dump.sql
, ( !)

+9

- , ( 10 15 ):

CREATE DATABASE/! 32312 / old_database_name...

old_database_name;

old_database_name . 5,9 vim. 15 . , 3 . 30 . . , !

0

, . :

pv 20171212.dump.bz2 | bunzip2 | sed -e '/^\(CREATE DATABASE\|USE\|.*DROP DATABASE\).*`<old db name>`/d' | mysql <new db name>

pv just shows a progress bar. The line could be written as

bunzip2 20171212.dump.bz2 | sed -e '/^\(CREATE DATABASE\|USE\|.*DROP DATABASE\).*`<old db name>`/d' | mysql <new db name>

sed deletes lines that prohibit changing the database name without changing any text in the dump file.

0
source

All Articles