Menu

MySQL Error 1049: Unknown Database (42000)

Fix MySQL Error 1049 (42000) by checking the database name, server, letter case, and account visibility before creating or selecting a database.

Posted on By
On this page

MySQL Error 1049 (42000, ER_BAD_DB_ERROR) means that the statement named a database that MySQL cannot find on the server your connection reached. A common message is:

ERROR 1049 (42000): Unknown database 'app_db'

The MySQL 8.4 error reference lists this code and message. Check the database name and connection target before creating anything; a typo in a host, port, or environment setting can send a valid command to the wrong server.

Check the connection and database name

In the same connection that produced the error, check the server and current database:

SELECT @@hostname AS mysql_host,
       @@port AS mysql_port,
       DATABASE() AS selected_database;

Then check for the exact database name:

SELECT SCHEMA_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'app_db';

If this returns no row, verify that the connection is reaching the intended MySQL server and that app_db is spelled exactly as created. On Unix-like systems, database names can be case-sensitive; use the same letter case throughout. See MySQL’s notes on creating and selecting a database.

An empty result does not always prove the database is absent. MySQL shows only databases for which your account has privileges unless it has the global SHOW DATABASES privilege. Ask an administrator to confirm the name and grant access if needed; SHOW DATABASES documents this visibility rule.

Fix the cause that applies

Correct the name or connect to the right server

Compare the database name in USE, a qualified table name such as app_db.orders, or the client’s default-database setting with the name on the intended server. Check the host, port, container or service name, and environment configuration. Development, staging, and production servers can contain different databases.

Create the database only if it should be new

If the database has not been created and this connection is definitely the intended server, create it with an account that has the CREATE privilege:

CREATE DATABASE `app_db`;

Do not create a new database just to silence the error when the application is supposed to use an existing one. If it should already exist, check whether its schema setup or restore step ran on this server. See the MySQL CREATE DATABASE tutorial.

Request access if the database exists but your account cannot use it

An existing database that your account cannot access is a privileges problem, usually reported as Error 1044, not Error 1049. Ask the database administrator to grant the required privileges instead of creating a second database.

Select the database after connecting

If the database exists and your account can use it, select it before running statements with unqualified table names:

USE `app_db`;
SELECT DATABASE();

USE selects an existing database; it does not create one. Error 1049 means the named database was not found. Error 1046 (3D000, No database selected) means a statement needed a default database but none was selected. See the MySQL USE tutorial.

Error 1007 instead means a CREATE DATABASE statement found a database with that name already present. Read Error 1007: database already exists and Error 1050: table already exists for those cases.

Browse more fixes in MySQL error troubleshooting.