Menu

MySQL Error 1046: No Database Selected (3D000)

Fix MySQL Error 1046 (3D000) by selecting a database with USE, qualifying table names, or setting the database in your client connection.

Posted on By
On this page

MySQL Error 1046 (3D000, ER_NO_DB_ERROR) means a statement needs a default database, but the current connection has not selected one. The message is:

ERROR 1046 (3D000): No database selected

This often happens in a new client session when you run a statement with an unqualified table name, such as SELECT * FROM orders; or SHOW TABLES;.

Check the current database

Run this in the same connection that returned the error:

SELECT DATABASE();

If there is no default database, DATABASE() returns NULL. See the MySQL DATABASE() documentation.

Select a database with USE

If you know which existing database contains the table, select it before running the query:

USE `app_db`;
SELECT DATABASE();
SELECT * FROM `orders`;

USE sets the default database for the current connection. It does not create a database. If it returns Error 1049, the server cannot find the name you specified; see MySQL Error 1049 troubleshooting.

Qualify a table for a one-off statement

You can specify the database as part of the table name instead of changing the connection’s default:

SELECT * FROM `app_db`.`orders`;

For commands that support a database argument, pass it directly. For example, to list tables without selecting a default database:

SHOW TABLES FROM `app_db`;

Your account still needs privileges for the database and table.

Set the database when connecting

When using the MySQL command-line client, specify the database while connecting:

mysql -h db.example.com -u app_user -p -D app_db

For an application or GUI client, set the database/schema in its connection configuration. Confirm that the setting applies to the connection that runs the failing statement; pooled connections and newly opened sessions can have different defaults.

Distinguish Error 1046 from nearby errors

  • Error 1046 (3D000): no default database is selected for a statement that needs one.
  • Error 1049 (42000): a database name was supplied, but MySQL could not find it on that server.
  • Error 1044 (42000): the database exists, but the account is not allowed to access it. See Error 1044 troubleshooting.

The MySQL server error reference lists these codes. For syntax and examples, see the MySQL USE tutorial. Browse more fixes in MySQL error troubleshooting.