Skip to content
MySQL

MySQL Error 1049

Unknown database

Applies to: All MySQL versions (checked on MySQL 8.0.46)
Last technically reviewed 24 October 2026 on MySQL 8.0.46
The full message
ERROR 1049 (42000): Unknown database 'sales_db'
Other forms of this message
mysql client started with a database that does not exist (mysql -D sales_db)
ERROR 1049 (42000): Unknown database 'sales_db'
PHP PDO, database in the DSN (raised while connecting)
SQLSTATE[HY000] [1049] Unknown database 'sales_db'
PHP PDO, USE statement on an open connection
SQLSTATE[42000]: Syntax error or access violation: 1049 Unknown database 'sales_db'
PHP mysqli exception while connecting (code and message)
1049 Unknown database 'sales_db'
Error number
1049
SQLSTATE
42000
Symbol
ER_BAD_DB_ERROR
Product
MySQL Server
Versions
All MySQL versions (checked on MySQL 8.0.46)
Last reviewed
24 October 2026

What it means

Error 1049 means the database name you gave, in the connection settings, a USE statement or a db.table reference, does not exist on the server you are connected to.

MySQL checks database names before it looks at tables. If sales_db is not a database on this server, any statement that refers to it fails with 1049, whether the name came from USE sales_db, from sales_db.orders in a query, or from the database part of the connection string. In the connection case the error happens during login, so the application never gets a working connection; PDO then reports SQLSTATE HY000 with [1049] rather than 42000.

The name in quotes is exactly what MySQL searched for, so look at it closely: a typo, a missing environment prefix (shop versus shop_dev), a trailing character, or a database that exists on a different server or port than the one you reached. Database names map to directories, so on servers where lower_case_table_names is 0 (the default on Unix) Shop and shop are different databases.

Common causes

  • A typo or the wrong environment name in the database setting, for example shop_prod on a development server that only has shop_dev.
  • The database was never created: a fresh install, a new container or a CI job that runs migrations before CREATE DATABASE.
  • The application reached a different MySQL server than expected, for example another instance on a different port, a local server instead of the remote one, or a replica that has not received the database.
  • Letter case: the code says Shop but the database is shop, which matters on a server with lower_case_table_names = 0.
  • A name with special characters such as a hyphen. Unquoted, USE my-shop is a syntax error (1064); quoted with backticks it is looked up literally and fails with 1049 if that exact name does not exist.

Diagnose it

Read-only These statements only read metadata (information_schema, performance_schema and SHOW statements) or your own data. They do not change data, privileges or configuration, so they are safe to run first.

Which databases exist on this server?

SHOW DATABASES lists only the databases your account has some privilege on, so a database you cannot see may still exist. Compare the exact spelling and letter case with the name in the error.

SHOW DATABASES;

SELECT schema_name
FROM information_schema.schemata
WHERE schema_name LIKE '%sales%';
What to look for

No row for the name in the error confirms it does not exist (or is not visible to you). A near match, such as a different suffix or case, is usually the real database you meant.

Am I connected to the server I think I am?

SELECT @@hostname AS server_host, @@port AS server_port, VERSION() AS server_version, CURRENT_USER() AS account;
What to look for

The host name, port and version of the server that answered. If they are not the server where the database lives, fix the host or port in the connection settings rather than the database name.

Does the server compare database names case-sensitively?

SELECT @@lower_case_table_names AS lctn, @@version_compile_os AS os;
What to look for

0 means names are case-sensitive (the Unix default), so the case must match exactly. 1 (the Windows default) stores and compares names in lowercase, and 2 (the macOS default) compares them in lowercase.

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Correct the database name in the connection settings

Client or network side

Copy the exact name from SHOW DATABASES into the DSN, the environment variable or the config file, and check the host and port at the same time. Nothing changes on the server.

-- PHP PDO DSN
mysql:host=127.0.0.1;port=3306;dbname=shop;charset=utf8mb4

-- mysql client
mysql -u app -p -D shop

Create the database when it really should exist

Changes database state

On a new environment, create the database before running migrations, with an explicit character set so it does not depend on server defaults. IF NOT EXISTS makes the step safe to re-run. Creating a database needs the CREATE privilege; ask the server owner if your account does not have it.

Caution: Make sure you are on the intended server first (see the diagnostic above). Creating an empty database with the expected name on the wrong server hides the real problem: the application then connects but fails with 1146 because the tables are missing.

CREATE DATABASE IF NOT EXISTS shop_dev
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_0900_ai_ci;

Quote names that contain special characters

Client or network side

A database name with a hyphen or other special character must be written in backticks in SQL. In a DSN or on the command line the name is passed as is, without backticks.

USE `my-shop`;
SELECT COUNT(*) FROM `my-shop`.orders;

Fix the qualifier in a db.table reference

Client or network side

When the error comes from a query such as SELECT ... FROM shop_archive.orders, the database part is wrong or missing on this server. Correct the qualifier, or drop it to use the connection's default database.

SELECT id, total
FROM shop.orders
WHERE id = 10;

Examples

USE with a database name that does not exist

USE sales_db;
Result
ERROR 1049 (42000): Unknown database 'sales_db'

Reproduced on MySQL 8.0.46, 24 October 2026.

A table qualified with the wrong database

CREATE TABLE orders (id INT PRIMARY KEY, total DECIMAL(10,2));

SELECT id, total
FROM shop_archive.orders;  -- this server has no shop_archive database
Result
ERROR 1049 (42000): Unknown database 'shop_archive'

Reproduced on MySQL 8.0.46, 24 October 2026.

A hyphenated name quoted correctly but spelled wrongly

Without the backticks, USE my-shop; fails earlier with ERROR 1064 near '-shop', because the hyphen is read as a minus sign.

USE `my-shop`;
Result
ERROR 1049 (42000): Unknown database 'my-shop'

Reproduced on MySQL 8.0.46, 24 October 2026.

How to prevent it

Keep the database name, host and port together in one environment-specific setting, and fail fast at start-up with a clear log line that prints the host, port and database (never the password) when the connection is refused.

Make environment set-up scripts create the database explicitly (CREATE DATABASE IF NOT EXISTS with a character set) before migrations run, and use lowercase database names without special characters so they behave the same on Windows, macOS and Linux.

Common questions

How do I fix MySQL error 1049 Unknown database?

Run SHOW DATABASES on the same server and compare the exact name. If it is spelt differently, fix the name in your connection settings or query. If it is missing and should exist, check you are connected to the right host and port, then create it with CREATE DATABASE.

The database exists in my GUI tool, so why does the application get 1049?

The two are almost certainly talking to different servers, ports or instances. Run SELECT @@hostname, @@port; in both and compare. A local development machine can easily have more than one MySQL or MariaDB server running.

Are MySQL database names case-sensitive?

It depends on lower_case_table_names. With 0, the default on Unix, names are case-sensitive, so Shop and shop are different databases. Windows (default 1) and macOS (default 2) compare them in lowercase. The setting can only be chosen when the server is initialised.

Why does SELECT * FROM missing_db.orders give 1049 and not 1146?

MySQL resolves the database before the table. When the database itself is unknown you get 1049; when the database exists but the table does not, you get 1146 Table doesn't exist.

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 24 October 2026; examples and diagnostics run on MySQL 8.0.46. Message templates checked against the server's error catalogue (perror) for that version.

Still stuck on a MySQL error?

Browse the full MySQL error library, or get hands-on help from an experienced SQL professional.