Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error importing SQL dump into MySQL: Unknown database / Can't create database

I'm confused how to import a SQL dump file. I can't seem to import the database without creating the database first in MySQL.

This is the error displayed when database_name has not yet been created:

username = username of someone with access to the database on the original server.
database_name = name of database from the original server

$ mysql -u username -p -h localhost database_name < dumpfile.sql    Enter password:   ERROR 1049 (42000): Unknown database 'database_name'  

If I log into MySQL as root and create the database, database_name

mysql -u root   create database database_name;   create user username;# same username as the user from the database I got the dump from.   grant all privileges on database_name.* to username@"localhost" identified by 'password';   exit mysql 

then attempt to import the sql dump again:

$ mysql -u username -p database_name < dumpfile.sql   Enter password:   ERROR 1007 (HY000) at line 21: Can't create database 'database_name'; database exists 

How am I supposed to import the SQL dumpfile?

like image 601
BryanWheelock Avatar asked May 13 '09 17:05

BryanWheelock


People also ask

How do I fix an unknown database error in MySQL?

This type of error occurs if you select any database that does not exist in MySQL. Let us first display the error of unknown database in JDBC. To remove this type of error, just go to MySQL command line and show all database names and use the chosen one from there i.e. the database which really exist.

How do I create a database in MySQL workbench?

Open the MySQL Workbench as an administrator (Right-click, Run as Admin). Click on File>Create Schema to create the database schema. Enter a name for the schema and click Apply. In the Apply SQL Script to Database window, click Apply to run the SQL command that creates the schema.


2 Answers

This is a known bug at MySQL.

bug 42996
bug 40477

As you can see this has been a known issue since 2008 and they have not fixed it yet!!!

WORK AROUND
You first need to create the database to import. It doesn't need any tables. Then you can import your database.

  1. first start your MySQL command line (apply username and password if you need to)
    C:\>mysql -u user -p

  2. Create your database and exit

    mysql> DROP DATABASE database;   mysql> CREATE DATABASE database;   mysql> Exit   
  3. Import your selected database from the dump file
    C:\>mysql -u user -p -h localhost -D database -o < dumpfile.sql

You can replace localhost with an IP or domain for any MySQL server you want to import to. The reason for the DROP command in the mysql prompt is to be sure we start with an empty and clean database.

like image 98
Max Kielland Avatar answered Oct 10 '22 05:10

Max Kielland


Open the sql file and comment out the line that tries to create the existing database.

like image 20
Bjorn Avatar answered Oct 10 '22 05:10

Bjorn