A query fails with No database selected
ERROR 1046 (3D000): No database selectedShort answer
Error 1046 means the connection has no default database and the statement used a bare table name. Choose one with USE db_name, start the client with a database name, or write db_name.table_name in the query.
Fix it fast
1Find out which cause you have
Ask the server which database the current session uses. NULL means none is selected.
SELECT DATABASE();2Match what you see, then jump to its fix
- 1
SELECT DATABASE() returns NULL right after connectingThe session never chose a databaseFix - 2
The error appears on the first CREATE TABLE or INSERT of a dump or migration fileAn SQL file is loaded without a databaseFix - 3
The same command works on one machine or user and fails on anotherThe default database is set in a config file that is not readFix
- 1
What does the error look like?
ERROR 1046 (3D000): No database selected
Cause 1 of 3
The session never chose a database
1Check
List what exists, then pick the one you need.
SHOW DATABASES;2Fix
Select it inside the client, or name it when you start the client.
USE shop;
# or at start-up
mysql -u appuser -p shop3Verify
DATABASE() now returns the name.
SELECT DATABASE();Confirmed by dev.mysql.com
Cause 2 of 3
An SQL file is loaded without a database
1Check
Check whether the file starts with USE or CREATE DATABASE, and how you run it.
head -n 20 schema.sql2Fix
Name the database on the command line; this is the same as putting USE at the top of the input.
mysql -u appuser -p shop < schema.sql3Verify
The import finishes with no 1046 error and the tables exist.
mysql -u appuser -p shop -e 'SHOW TABLES;'Confirmed by dev.mysql.com
Cause 3 of 3
The default database is set in a config file that is not read
1Check
The database option is meant for option files. Confirm the file and group the client reads.
mysql --print-defaults2Fix
Add a database line to the [client] or [mysql] group of the option file you actually use, or pass it on the command line.
[mysql]
database=shop3Verify
Start the client with no database argument and check DATABASE().
mysql -u appuser -p -e 'SELECT DATABASE();'Confirmed by dev.mysql.com
How do I stop it coming back?
- Start every script with an explicit USE or pass the database name to the client.
- Use the db_name.table_name form in one-off queries from tools that open a fresh connection.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
A query on Aurora MySQL dies with 'table /rdsdbdata/tmp/#sql... is full'
ERROR 1114 (HY000): The table '/rdsdbdata/tmp/#sqlfd_1586_2' is full
In Aurora MySQL 3, internal temporary tables share one pool, and when that pool and the memory-mapped overflow file are used up, a reader instance ends the query with error 1114.
3 causes, each with a fixDatabases
Azure Database for MySQL refuses a connection that does not use SSL
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
Azure Database for MySQL Flexible Server requires encrypted connections by default (require_secure_transport = ON, TLS 1.2).
4 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
DBeaver fails to resolve Maven dependencies when downloading a database driver
A driver library does not download automatically
DBeaver downloads driver jars from a Maven repository.
3 causes, each with a fixDatabases
DBeaver cannot verify the MySQL server certificate
unable to find valid certification path to requested target
DBeaver's MySQL driver is checking the server certificate and Java does not trust the CA that signed it.
2 causes, each with a fix