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`;
You have to replace the database_name in the .sql file.
You can do that from the shell with
sed 's/database_name/new_database_name/' dumpFile.sql > newDumpFile.sqlthis is the correct answer to my question but was provided by ‘fab’ in the comments of another answer.
NOTE: As @Diego pointed out in the comments, if the string ‘database_name’ shows up anywhere else in this .sql dump, it will be replaced there as well. You may want to view the contents of the .sql file using
less file_name.sqland then be more explicit with your find/replace.