Skip to content
MySQL

MySQL Error 1046

No database selected

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 1046 (3D000): No database selected
Other forms of this message
mysql client, statement given with -e or read from a file
ERROR 1046 (3D000) at line 2: No database selected
PHP PDO
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
Error number
1046
SQLSTATE
3D000
Symbol
ER_NO_DB_ERROR
Product
MySQL Server
Versions
All MySQL versions (checked on MySQL 8.0.46)
Last reviewed
24 October 2026

What it means

Error 1046 means the statement names a table without saying which database it is in, and the connection has no default database to fall back on.

Every MySQL connection has an optional default database. An unqualified name such as orders is looked up in that database. When you connect without choosing one (no database in the connection string, no -D option, no USE statement), SELECT DATABASE() returns NULL, and any statement that needs to find or create a table fails with 1046 before MySQL even checks whether the table exists.

The fix is to tell MySQL where to look: choose a default database for the session, or write the database name in front of the table (shop.orders). SQLSTATE 3D000 is the standard class for "invalid catalog name", which is why PDO labels it that way. If you did select a database but its name is wrong, you get 1049 Unknown database instead, and a wrong table name in the right database gives 1146.

Common causes

  • The connection string or DSN has no database name, for example a PDO DSN without dbname= or a mysqli connection with an empty database argument.
  • The mysql client was started without a database (mysql -u app -p) and the first statement is a query rather than USE.
  • A dump of a single database is restored without naming a target database. mysqldump only writes CREATE DATABASE and USE when it was run with --databases or --all-databases.
  • A GUI tool opened a new query tab that is not attached to a schema, so the tab has no default database even though another tab does.
  • A connection pool or a new connection after a reconnect does not repeat the USE statement that an earlier connection ran.

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 database is this session using?

SELECT DATABASE() AS current_db, CURRENT_USER() AS account;
What to look for

current_db is NULL when no default database is selected, which is the condition for 1046. Run it from the same connection (or query tab) that raised the error, not from another one.

List the databases you can choose from

Use the exact name from this list in your connection settings or USE statement. SHOW DATABASES only lists databases your account has some privilege on.

SHOW DATABASES;

SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
ORDER BY schema_name;
What to look for

The application databases on this server. If the one you expect is missing, you are connected to a different server or account, or the database was never created (see 1049).

Find which database holds the table

SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name = 'orders';
What to look for

The database (schema) that contains the table. Select that database, or qualify the table with it.

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.

Put the database in the connection settings

Client or network side

This is the usual fix for applications: name the database when you connect, so every connection (including new ones from a pool or after a reconnect) starts with the right default.

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

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

Select a default database for the session

Client or network side

In an interactive session or at the top of a script, choose the database with USE. It stays the default until the session ends or another USE runs. It only affects the current connection.

USE shop;
SELECT * FROM orders;

Qualify table names with the database

Client or network side

Writing database.table works whatever the default database is, which suits scripts that touch more than one database. It is also clearer in migrations that must run against a specific schema.

SELECT o.id, o.total
FROM shop.orders AS o
WHERE o.customer_id = 1;

Restore a single-database dump into a named database

Changes data

A dump made with mysqldump shop contains the tables but no USE statement. Name the target database on the command line when you load it, or dump with --databases shop next time so the file carries its own CREATE DATABASE and USE.

Caution: A dump normally contains DROP TABLE IF EXISTS for every table it creates. Check that the target database is the one you mean to overwrite before loading it.

mysql -u app -p shop < shop_dump.sql

Examples

A query on a connection with no default database

-- connected with: mysql -u app -p   (no database named)
SELECT * FROM orders;
Result
ERROR 1046 (3D000): No database selected

Reproduced on MySQL 8.0.46, 24 October 2026.

Running a table script before choosing a database

-- the top of a schema script that never says USE
CREATE TABLE customers (
    id   INT PRIMARY KEY,
    name VARCHAR(100)
);
Result
ERROR 1046 (3D000): No database selected

Reproduced on MySQL 8.0.46, 24 October 2026.

SHOW TABLES also needs a database

SHOW TABLES FROM shop; works without a default database because it names one.

SHOW TABLES;
Result
ERROR 1046 (3D000): No database selected

Reproduced on MySQL 8.0.46, 24 October 2026.

How to prevent it

Always put the database name in the application's connection settings rather than relying on a USE statement that runs once. Connection pools, reconnects and new GUI tabs all start fresh, and each of them needs its own default database.

Make deployment and migration scripts explicit: start them with USE shop; or qualify table names, and dump databases with --databases when the file should restore into a database of the same name.

Common questions

How do I fix "No database selected" in MySQL?

Choose a database for the connection. In the mysql client or a script run USE your_database; first. In application code add the database to the connection string (for PDO, dbname=your_database in the DSN). Alternatively write the database in front of each table, as in your_database.orders.

Why do I get 1046 when importing a .sql file?

The file was probably made with mysqldump for a single database, which writes no USE statement. Load it with mysql -u user -p target_db < file.sql, or pick the target database in your GUI tool before running the import.

My first query worked, so why do later ones fail with 1046?

The default database belongs to one connection. If the application opens a new connection (a pool, a reconnect after a timeout, or a new query tab in a GUI), that connection starts with no default unless the database is in its connection settings.

What is the difference between 1046 and 1049?

1046 means no database was chosen at all. 1049 means a database name was given, in USE, in the connection settings or as db.table, but no database with that name exists on the server you are connected to.

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.