Topological Sort
Topological Sort

Reputation: 2787

Have MySQL Create Database when Restoring Backup

I want to provide users with a backup of an initial database, and give a one-line command to create the database on their systems. Here's how I'm backing up the database:

mysqldump -u root -p -B --add-drop-database "myDatabase" > backup.sql

Here's my command to restore it:

mysql -u root -p -D "myDatabase" < backup.sql

Problem is that it fails:

ERROR 1049 (42000): Unknown database 'myDatabase'

Here's what the relevant part of the backup file looks like:

--
-- Current Database: `myDatabase`
--

/*!40000 DROP DATABASE IF EXISTS `myDatabase`*/;

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `myDatabase` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `myDatabase`;

If I type this in inside MySQL, I get no problem.

(I would also like for mysqldump to take the comments off dropping the database if it exists, and creating it if it does not exist.)

If it matters, I'm running

  MySQL 5.5.49-0ubuntu0.12.04.1.

Upvotes: 0

Views: 96

Answers (1)

Bernd Buffen
Bernd Buffen

Reputation: 15057

ok, you give the SCHEMA NAME in the mysql command. So mysql want to change first the schema to myDatabase AND CANT THAT DO. there is no schema myDatabase. remove the -D option like:

mysql -u root -p  < backup.sql

Upvotes: 1

Related Questions