Ошибка 1049 mysql

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'

BenMorel's user avatar

BenMorel

34.5k50 gold badges183 silver badges322 bronze badges

asked Oct 30, 2013 at 9:44

La Chi's user avatar

5

If dump file contains:

CREATE DATABASE mydatabasename;
USE mydatabasename; 

You may just use in CLI:

mysql -uroot –pmypassword < mydatabase.sql

It works.

answered Mar 24, 2016 at 17:02

Dobpbiu's user avatar

DobpbiuDobpbiu

4814 silver badges4 bronze badges

2

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;

answered Oct 30, 2013 at 10:02

zessx's user avatar

zessxzessx

68.1k28 gold badges135 silver badges159 bronze badges

1

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

answered Oct 30, 2013 at 10:05

Sathish D's user avatar

Sathish DSathish D

4,85431 silver badges44 bronze badges

0

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;

answered Jan 31, 2017 at 2:55

molavec's user avatar

molavecmolavec

8,8481 gold badge27 silver badges22 bronze badges

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;

answered Oct 11, 2016 at 8:20

Iulian Cucoanis's user avatar

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

answered Oct 19, 2020 at 8:05

Nabeel Shaikh's user avatar

Nabeel ShaikhNabeel Shaikh

1,2081 gold badge14 silver badges27 bronze badges

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;

answered Jun 20, 2022 at 12:05

alexlz's user avatar

alexlzalexlz

6181 gold badge10 silver badges24 bronze badges

mysql -e "show databases;" 
mysql -e "create database mydatabasename;" 
mysql -e "use mydatabasename;" 

answered Mar 2 at 13:45

Anshuman Bisoyi's user avatar

1

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.

joanolo's user avatar

joanolo

6,0581 gold badge29 silver badges37 bronze badges

answered Aug 1, 2017 at 19:08

jhamezzz1315's user avatar

jhamezzz1315jhamezzz1315

331 gold badge4 silver badges12 bronze badges

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

answered Dec 9, 2019 at 17:19

Adil Roubleh's user avatar

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

answered Jan 23, 2020 at 15:42

Sely Lychee's user avatar

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).

dxjuv's user avatar

dxjuv

8791 gold badge8 silver badges28 bronze badges

answered Feb 20, 2020 at 11:30

Aqsa Zahoor's user avatar

1

mysql -uroot -psecret mysql < mydatabase.sql

answered May 29, 2020 at 14:40

Juan Castellon's user avatar

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

answered May 26, 2021 at 10:18

Vanavy's user avatar

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

answered Aug 12, 2021 at 10:14

habib ur rehman's user avatar

Create database:

CREATE DATABASE mydatabasename;
USE mydatabasename;

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

answered Feb 11, 2022 at 10:33

user18179848's user avatar

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

answered Mar 25, 2022 at 9:58

sree's user avatar

sreesree

511 silver badge3 bronze badges

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

answered Apr 30, 2022 at 8:12

LIU YUE's user avatar

LIU YUELIU YUE

1,61312 silver badges19 bronze badges

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

answered Jul 24, 2020 at 11:56

VIJAY PRATAP SINGH's user avatar

1

Often while performing WHM backups, users notice an error “mysqldump: Got error: 1049: Unknown database”. This can happen when the database does not exist in MySQL.

As a part of our Server Management Services, we help our customers with similar requests related to WHM/ cPanel.

Let us today, discuss the possible reasons and fixes for this error.

What causes “MySQLdump: got error :1049 :unknown database ” error

MySQLdump helps to perform the logical backups, generating a set of SQL statements like DDL, DML for reproduced backup Schema. It dumps one or more MySQL databases for backup or transfers to another SQL server.

We can also generate output in CSV, other delimited text or XML format. The main drawback of MySQLdump is that the restoration procedure can take a long time to execute if the database is very large.

While performing WHM backups, at times, we can see the following error in the backup log:

The backup process encountered the following error: The backup process on “hostname.example.server” encountered an error.
[2021-05-10 02:25:26 -0600] mysqldump: Got error: 1049: Unknown database ‘example_database’ when selecting the database

Generally, This error indicates that the related database exists in a cPanel user’s database map, but the database does not exist in MySQL.
 

How to fix “MySQLdump: got error :1049 :unknown database ” error

The first thing that our Support Engineers perform on seeing this error is to check whether the database exists within MySQL. They does this by running the following command as the root user via SSH:

mysql -e "show databases;" | grep example_database

Replace example_database with the database found within the backup error in the backup logs. We can find the backup logs within /usr/local/cpanel/logs/cpbackup.

If the above command does not display any results, it indicates that the database does not exist in MySQL.

Thus, In order to correct the backup errors, we have to remove the databases that do not actually exist in MySQL from cPanel.

For this, we initially log in to the cPanel account for the particular database user. Then, we navigate to the Databases section and then click on the MySQL Databases option.

Here, we just need to delete the corresponding database from the current database section.

mysqldump got error 1049 unknown database

 
[Need any further assistance to fix cPanel errors? – We’re available 24*7]

Conclusion

The “MySQLdump: got error :1049 :unknown database ” triggers while performing cPanel backups. This can happen when the database does not exist in MySQL. Today, we saw how our Support Engineers fix this error.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

The error «ERROR 1049 (42000): Unknown database» occurs when a user tries to access a database that does not exist on the MySQL server. This error can be frustrating for users because it can prevent them from accessing their data and can make it difficult to troubleshoot the problem. However, there are several methods that can be used to resolve this issue.

Method 1: Verify Database Existence

To fix the ERROR 1049 (42000): Unknown database error in MySQL, you can verify the existence of the database before attempting to connect to it. Here’s how you can do it in PHP:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check if database exists
$result = $conn->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$dbname'");

if ($result->num_rows == 0) {
    // Database does not exist, handle error
    die("Database does not exist");
}

// Database exists, continue with connection
// ...

In this example, we first create a connection to the MySQL server using the mysqli class. We then execute a query to check if the database exists using the INFORMATION_SCHEMA.SCHEMATA table. If the query returns zero rows, we know that the database does not exist and we can handle the error accordingly. If the query returns one or more rows, we know that the database exists and we can continue with the connection.

Note that this is just one way to fix the ERROR 1049 (42000): Unknown database error in MySQL. There are other methods such as creating the database manually or using the CREATE DATABASE statement in SQL.

Method 2: Create the Database

To fix the «ERROR 1049 (42000): Unknown database» in MySQL, you can create the database using the following steps:

  1. Open the MySQL command-line interface or any MySQL client tool.

  2. Connect to the MySQL server using a valid username and password.

  3. Use the following command to create a new database:

CREATE DATABASE database_name;

Replace «database_name» with the name of the database you want to create.

  1. Once the database is created, you can switch to it using the following command:
  1. Now you can create tables, insert data, and perform other operations on the database.

Here is an example of how to create a new database named «mydatabase» using the MySQL command-line interface:

mysql -u root -p

Enter password: ********

mysql> CREATE DATABASE mydatabase;

mysql> USE mydatabase;

This will create a new database named «mydatabase» and switch to it.

You can also create a new database using any MySQL client tool that supports SQL commands. For example, in PHP, you can use the following code to create a new database:

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE mydatabase";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . $conn->error;
}

$conn->close();
?>

This code will create a new database named «mydatabase» using the mysqli extension in PHP.

In conclusion, to fix the «ERROR 1049 (42000): Unknown database» in MySQL, you can create the database using the «CREATE DATABASE» command and then switch to it using the «USE» command. You can also create a new database using any MySQL client tool that supports SQL commands.

Method 3: Check User Privileges

To fix the «ERROR 1049 (42000): Unknown database» in MySQL using «Check User Privileges», follow these steps:

  1. Connect to the MySQL server using the mysql command-line client:
  1. Enter your password when prompted.

  2. Check if the database exists by running the following command:

  1. If the database exists, make sure that the user has the necessary privileges to access it. You can do this by running the following command:
SHOW GRANTS FOR username;
  1. If the user does not have the necessary privileges, you can grant them by running the following command:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
  1. Replace database_name with the name of the database and username with the name of the user.

  2. If the database does not exist, you can create it by running the following command:

CREATE DATABASE database_name;
  1. Replace database_name with the name of the database.

  2. Once you have created the database and granted the necessary privileges to the user, you should be able to access the database without encountering the «ERROR 1049 (42000): Unknown database» error.

Example code:

mysql -u root -p
SHOW DATABASES;
SHOW GRANTS FOR 'myuser'@'localhost';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
CREATE DATABASE mydatabase;

Note: Replace root with your MySQL username, myuser with your desired username, and mydatabase with your desired database name.

Method 4: Check Connection Parameters

To fix the «Mysql: how to fix ERROR 1049 (42000): Unknown database?» error using «Check Connection Parameters», follow these steps:

  1. Open your MySQL client and check the connection parameters that you are using to connect to the database. The connection parameters include the hostname, username, password, and database name.

  2. Ensure that the database name specified in the connection parameters exists in your MySQL server. You can check this by running the following command in your MySQL client:

    This will display a list of all the databases in your MySQL server.

  3. If the database name specified in the connection parameters does not exist, you need to create it. You can create a new database by running the following command in your MySQL client:

    CREATE DATABASE database_name;

    Replace database_name with the name of the database you want to create.

  4. Once you have created the database, update the connection parameters in your MySQL client to use the correct database name.

  5. Test the connection by running a query in your MySQL client. For example, you can run the following command to select all the records from a table:

    SELECT * FROM table_name;

    Replace table_name with the name of the table you want to select records from.

  6. If the query runs successfully, you have fixed the «Mysql: how to fix ERROR 1049 (42000): Unknown database?» error using «Check Connection Parameters».

Here is an example of how to update the connection parameters in PHP:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Run a query
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
  }
} else {
  echo "0 results";
}

$conn->close();
?>

This document (7009762) is provided subject to the disclaimer at the end of this document.

Environment

SUSE Linux Enterprise Server 10 Service Pack 1

SUSE Linux Enterprise Server 10 Service Pack 2

SUSE Linux Enterprise Server 10 Service Pack 3

SUSE Linux Enterprise Server 10 Service Pack 4

SUSE Linux Enterprise Server 11 Service Pack 1

SUSE Linux Enterprise Server 11 Service Pack 2

Situation

ERROR 1049 (42000): Unknown database ‘mysql’ when trying to access mysql via this command:

this command will access the default ‘mysql’ database, and needs to be done prior to setting or resetting the root users’ MySQL password.

Resolution

Depending on how MySQL was installed, it is possible that the default MySQL database was NOT created. 

This may be checked by looking in /var/lib/mysql for a mysql subfolder (i.e. /var/lib/mysql/mysql ). If the path does NOT contain a mysql subfolder, it needs to be created by completing the following steps:
 

rcmysql stop
pkill mysql   (NOTE: wait until notification is given that mysqld ended, then hit <ENTER>)
/usr/bin/mysql_install_db

Now that the above steps have been run, check and make sure that the database was created:
 

cd /var/lib/mysql
ls -al | grep mysql

In the listing output there should now be a folder called mysql.  Finally, the correct owner and group need to be set on the mysql folder.

Restart the mysql service in safe mode again and attempt the initial command again:

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented «AS IS» WITHOUT WARRANTY OF ANY KIND.

  • Document ID:7009762
  • Creation Date:
    17-Nov-2011
  • Modified Date:12-Aug-2022
    • SUSE Linux Enterprise Server

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com

In fact, I made a very serious mistake, and this mistake can only be reported for two reasons.
Reason 1: There is an extra space after your account password, which means you have the wrong database.
Fix: If it is a login password, report this error, as shown in the figure below:

It would be possible to have an extra space between p and 123, and then you would remove the space in between. You put p and 123 together and you type in p123
Reason 2: Your syntax is wrong, because use can only be followed by the database name, never the table name. An incorrect or incorrect table name will report the following error.

Solution: Double check your database to see if it has TB_EMP6.

Check your table again to see if it is your table name.

The name of the table is confused with the database name.
Summary is to confirm the database name is not exist, there are no spelling mistakes, if not again to see your grammar is not wrong.
This is better to locate your problem, in fact, to put it bluntly this is I made a stupid mistake, but also eat a catty gain a wisdom.
Hope to help you, welcome to leave a message to exchange ~

Read More:

Понравилась статья? Поделить с друзьями:
  • Ошибка 105 pascal
  • Ошибка 105 http
  • Ошибка 105 784 invalid floating point operation
  • Ошибка 1049 ниссан х трейл
  • Ошибка 104006 эскейп фром тарков