53

I am trying to restore database from .sql file , i have created the database in phpmyadmin and also using the create if not exist command in the .sql file which i am restoring to the database and both names of database are same in phpmyadmin and .sql file which is"mydatabase".

Here is the command which i am using to restore database.

mysql -uroot -pmypassword mydatabase<mydatabase.sql;

When i execute the above command i am getting the following error, i have also given all the permission to the user upon this database.

ERROR 1049 (42000): Unknown database 'mydatabasename'
5

19 Answers 19

39

If dump file contains:

CREATE DATABASE mydatabasename;
USE mydatabasename; 

You may just use in CLI:

mysql -uroot –pmypassword < mydatabase.sql

It works.

2
  • it does not contain it, but your answer provided the final clue Apr 2, 2020 at 6:48
  • This was helpful, after this hint I went into the db_dump.sql file that was in the tmp folder to see that it only had one table load in it, the table that was in an error message before. The real dump file would lead to 13 databases (I checked this by going into the remote mysql server and then SHOW DATABASES, while with this cut dump file, I would get only the standard databases. I had to load the original db_dump.sql again to get all databases loaded. Jan 26, 2022 at 15:16
9

Whatever the name of your dump file, it's the content which does matter.

You need to check your mydatabase.sql and find this line :

USE mydatabasename;

This name does matter, and it's the one you must use in your command :

mysql -uroot -pmypassword mydatabasename<mydatabase.sql;

Two options for you :

  1. Remove USE mydatabasename; in your dump, and keep using :
    mysql -uroot -pmypassword mydatabase<mydatabase.sql;
  2. Change your local database name to fit the one in your SQL dump, and use :
    mysql -uroot -pmypassword mydatabasename<mydatabase.sql;
1
  • Thank you so much but this command worked for me "mysql -uroot -pmypassword -h localhost -D mydatabase < mydatabase.sql", without the USE mydatabase and CREATE DATABASE mydatabase IF NOT EXIST in the mysql dumpfile
    – La Chi
    Oct 30, 2013 at 11:58
4

Open the sql file and comment out the line that tries to create the existing database and remove USE mydatabasename and try again.

0
4

I solved because I have the same problem and I give you some clues:

1.- As @eggyal comments

mydatabase != mydatabasename

So, check your database name

2.- if in your file, you want create database, you can't set database that you not create yet:

mysql -uroot -pmypassword mydatabase<mydatabase.sql;

change it for:

mysql -uroot -pmypassword <mydatabase.sql;
3

You can also create a database named 'mydatabasename' and then try restoring it again.

Create a new database using MySQL CLI:

mysql -u[username] -p[password]
CREATE DATABASE mydatabasename;

Then try to restore your database:

mysql -u[username] -p[password] mydatabase<mydatabase.sql;
3

Create database which gave error as Unknown database, Login to mysql shell:

sudo mysql -u root -p
create database db_name;

Now try restoring database using .sql file, -p flag will ask for a sql user's password once command is executed.

sudo mysql -u root -p db_name < db_name.sql
1

You can also try

> mysql mysql

and you will connect to MySQL database from which you can create your own schema.

mysql> CREATE DATABASE mydb; USE mydb;
1
mysql -e "show databases;" 
mysql -e "create database mydatabasename;" 
mysql -e "use mydatabasename;" 
1
  • Can you edit your question to add an explanation and elaborate on your answer?
    – Michael M.
    Mar 3, 2023 at 5:13
0

La Chi's answer works for me.

You can view his/her answer in the comment of zessx answer in this page. But I initially have a problem with it if you also do just tweak his/her answer like this: mysql -h localhost -u root -p -D mydatabase < mydatabase.sql.

Also I would suggest that in the mydatabase.sql portion you include the direct location of it in your computer like "C:\Users\username\desktop".

Thank you.

0

If initially typed the name of the database incorrectly. Then did a Php artisan migrate .You will then receive an error message .Later even if fixed the name of the databese you need to turn off the server and restart server

0

I had the same issue, i run this command on command line and just like you i had added the ';' at the end. Removing it solved the issue. Instead of this

mysql -uroot -pmypassword mydatabase<mydatabase.sql;

try this

mysql -uroot -pmypassword mydatabase<mydatabase.sql
0

I found these lines in one of the .sql files

"To connect with a manager that does not use port 3306, you must specify the port number:

$mysqli = new mysqli('127.0.0.0.1','user','password','database','3307');

or, in procedural terms:

$mysqli = mysqli_connect('127.0.0.0.1','user','password','database','3307');"

It resolved the error for me . So i will suggest must use port number while making connection to server to resolve the error 1049(unknown database).

1
  • Thats fine @ROHAN Feb 24, 2020 at 7:26
0
mysql -uroot -psecret mysql < mydatabase.sql
0

I meet your issue. This is how to solve it

  1. Check your DB name correct and exist in MySQL
  2. Check if your IP and port is correct
0

It works by creating database and than typing command as : C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p -D cricket < C:\Users\habib_s9ayvfl\Desktop\sqlfile.sql

0

Create database:

CREATE DATABASE mydatabasename; USE mydatabasename;

use this one: mysql -u root -p 'mydatabasename'< '/tmp/db_dump.sql'

0

first, you need to check the folder /var/lib/mysql for mydatabasename (depend on how you installed mysql, but default folder is this one), please check the folder exists or not and its owner should be mysql:mysql, and of course the folder permission should be rw to mysql;

second, possibly because of you made changes to /etc/my.cnf, for example in my case, we created a database TEST_DB in uppercase, and then someone added lower_case_table_names=1 restriction in my.cnf, it caused the Unknown database error because mysql will transalte TEST_DB to lowercase test_db even when i key in select from TEST_DB, so it'll never find TEST_DB, simply comment out and restart mysql service solved my issue

-1

Its very simple: Use mysql -u root -p mysql

1
  • This does not even attempt to address OP's question.
    – roens
    Dec 4, 2023 at 21:23
-3

when u import database from workbench or other method ,should be give same name as your dump to avoid this kind of error

1
  • other answer explained how to import with different database name. Jul 24, 2020 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.