- Error number
- 1049
- SQLSTATE
42000- Symbol
ER_BAD_DB_ERROR- Product
- MySQL Server
- Versions
- All MySQL versions (checked on MySQL 8.0.46)
- Category
- Syntax & Names
- Last reviewed
- 24 October 2026
On this page
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_prodon a development server that only hasshop_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
Shopbut the database isshop, which matters on a server with lower_case_table_names = 0. - A name with special characters such as a hyphen. Unquoted,
USE my-shopis 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%'; 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; 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; 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 sideCopy 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 stateOn 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 sideA 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 sideWhen 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;
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
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`;
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
- CREATE DATABASE statement
- SHOW DATABASES statement
- Identifier case sensitivity (lower_case_table_names)
- MySQL 8.0 server error message reference
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.