Skip to content
Fix MySQL Error 1046: No Database Selected

Click to use (opens in a new tab)

Fix MySQL Error 1046: No Database Selected

October 22, 2026 by Chat2DBChat2DB Team
ERROR 1046 (3D000): No database selected

MySQL raises error 1046 when a statement refers to a table, view or routine without naming its database, and the session has no default database to fall back on. The SQLSTATE is 3D000 (invalid catalog name). The server is not missing anything; it simply does not know where to look.

The fix is always one of two things: select a default database for the session, or qualify the name as database.table. What takes time is figuring out why the session had no default, because that depends on how you connected. This guide covers the mysql client, application connection strings, dump imports and stored routines. All examples were run on MySQL 8.4.

Reproducing it

Connect without choosing a database and run any statement that uses a table:

SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL       |
+------------+
SELECT * FROM customers;
-- ERROR 1046 (3D000): No database selected
 
CREATE TABLE notes (id int);
-- ERROR 1046 (3D000): No database selected
 
SHOW TABLES;
-- ERROR 1046 (3D000): No database selected

DATABASE() returning NULL is the definitive check. If you are unsure whether a session has a default database, run it first.

Statements that do not touch a database-level object work fine without a default: SELECT 1, SELECT NOW(), SHOW DATABASES, CREATE DATABASE and user management all run normally.

Fix 1: USE a database

In an interactive session, pick the default database with USE:

USE shop;
SELECT DATABASE();      -- shop
SELECT COUNT(*) FROM customers;

The mysql client also accepts the short command \u shop. The setting lasts until the connection closes or you run another USE.

If USE itself fails, the database name is wrong, and you get a different error:

USE shopp;
-- ERROR 1049 (42000): Unknown database 'shopp'
 
USE Shop;
-- ERROR 1049 (42000): Unknown database 'Shop'

The second one surprises people coming from Windows or macOS. On Linux, MySQL's default lower_case_table_names = 0 makes database and table names case-sensitive, because they map to directories and files on a case-sensitive file system. Run SHOW DATABASES; to see the exact spelling.

Fix 2: choose the database when you connect

Every client lets you pass the default database at connection time, which is better than relying on a later USE:

mysql -u app -p -D shop
mysql -u app -p shop          # the last positional argument is the database

In application connection strings, the database is part of the URL or a separate option. Error 1046 in an application almost always means it is missing:

# JDBC: the database goes after the port
jdbc:mysql://db.internal:3306/shop          # correct
jdbc:mysql://db.internal:3306/              # no database: queries fail with 1046

# Python (mysql-connector, PyMySQL), Node.js (mysql2): a database option
connect(host="db.internal", user="app", password="...", database="shop")

Check where the value comes from in each environment. A missing DB_NAME environment variable that silently becomes an empty string is a classic cause: the app connects successfully, then every query fails with 1046.

Frameworks behave the same way. If a Spring, Django, Laravel or Rails application reports 1046, the database name in its datasource or DATABASE_URL setting is empty or not being read.

If you always work in the same database from the command line, put it in your option file so you do not have to type it. In ~/.my.cnf:

[mysql]
database = shop

The [mysql] group applies only to the mysql client, so mysqldump and other tools are not affected. Remove or override it (mysql -D other_db) when you need a different default; a forgotten option file entry is a common reason why a script runs against the wrong database without any error at all.

For migration and seed scripts, a simple safety habit is to start each file with an explicit USE shop; or, better, to have the migration tool pass the database on the connection. Either way, the script then fails loudly with Unknown database if the name is wrong, instead of creating tables wherever the session happened to point.

Fix 3: qualify table names

You can always bypass the default database by naming it in the statement:

SELECT * FROM shop.customers LIMIT 1;

That works even when DATABASE() is NULL. It is the right choice for scripts that deliberately work across several databases, and for administrative queries.

Qualification has to be complete, though. If one table in a join is qualified and another is not, the unqualified one still needs a default database:

SELECT *
FROM shop.orders o
JOIN customers c ON c.id = o.customer_id;
-- ERROR 1046 (3D000): No database selected

Importing a dump: the most common cause

This is where most people meet error 1046. mysqldump only writes CREATE DATABASE and USE statements when you ask for them. Dump a single database or table the usual way and look at the file:

mysqldump -u root -p shop customers > customers.sql
grep -E 'CREATE DATABASE|^USE ' customers.sql     # nothing

There is no USE line, so importing it into a session without a default database fails at the first CREATE TABLE or DROP TABLE:

mysql -u root -p < customers.sql
# ERROR 1046 (3D000) at line 22: No database selected

The fix is to tell the client which database to import into. Create it first if it does not exist:

mysql -u root -p -e "CREATE DATABASE shop_restore"
mysql -u root -p shop_restore < customers.sql

This is actually a useful property: a dump without USE can be restored into any database name, for example a copy for testing.

If you want a dump that recreates the database by itself, use --databases (or -B):

mysqldump -u root -p --databases shop > shop.sql

The file then starts with:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `shop` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `shop`;

and can be imported with a plain mysql -u root -p < shop.sql. Keep in mind that it always restores into shop, overwriting tables there if they exist.

GUI tools have the same issue. When you run an imported SQL file in MySQL Workbench without selecting a schema, you get 1046; select the target schema in the navigator first (Workbench calls it "Set as Default Schema"), or add a USE line at the top of the file.

Stored procedures, functions, triggers and events

Routines belong to a database too, so creating one requires a default database or a qualified name:

CREATE PROCEDURE p() SELECT 1;
-- ERROR 1046 (3D000): No database selected
 
CREATE PROCEDURE shop.p() SELECT 1;   -- works

Inside a routine, unqualified table names resolve against the database the routine belongs to, not the caller's current database. That is usually what you want, and it means a routine created in shop keeps working when called from a session that has no default database: CALL shop.p();.

Connection pools and USE

Some applications run USE other_db on a pooled connection to switch databases temporarily. The default database is a session setting, so it stays changed when the connection goes back to the pool, and the next request that borrows that connection runs against the wrong database, or against none if the code resets it incorrectly. If an application must work with several databases, qualify the names (other_db.table) or use a separate pool per database instead of switching with USE.

A quick diagnosis

Where you see 1046Likely causeFix
mysql client, interactiveConnected without a databaseUSE shop; or mysql -D shop
mysql < dump.sqlDump has no CREATE DATABASE / USEmysql shop < dump.sql, or dump with --databases
Application, every queryConnection string or env var lacks the databaseAdd the database to the URL or database option
Application, only some requestsUSE on a pooled connectionQualify names or use separate pools
CREATE PROCEDURE / CREATE TRIGGERNo default databaseUSE shop; first, or CREATE PROCEDURE shop.p()
Join with some tables qualifiedUnqualified tables still need a defaultQualify every table or set a default

When you work in a GUI client, the selected database is shown next to the editor, which makes this error rare. Chat2DB (opens in a new tab) shows the active database for each query tab and lets you switch it from a dropdown, and you can also open the web version (opens in a new tab) to run a dump file against a chosen database without the command line.

Summary

MySQL error 1046 means the statement used an unqualified object name while the session's default database was NULL. Check with SELECT DATABASE(), then either set the default (USE, mysql -D, or the database part of your connection string) or qualify names as database.table. For dumps, import into a named database or create the dump with --databases, and avoid switching databases with USE on pooled connections.