How to Fix MySQL Error 1046: No Database Selected

ERROR 1046 (3D000): No database selected is raised when a statement references a table but the connection has no active default database and no explicit database qualifier is provided. The error symbol is ER_NO_DB_ERROR.

Impact

Every MySQL connection has an optional "current database" context, set by the USE statement or the database parameter in the connection string. When a query references a bare table name — SELECT * FROM users rather than SELECT * FROM myapp.users — MySQL resolves it against the current database. If no database has been selected for the session, MySQL cannot resolve the table reference and immediately raises error 1046, aborting the statement.

This error surfaces frequently during manual mysql CLI sessions (where USE is easy to forget), in migration scripts that assume a database is already selected, and after connection pool reconnects where the initial USE statement was not re-issued. ORMs typically select a database at connect time via the connection URL, so applications rarely encounter it at runtime — but developers hit it often when running ad-hoc SQL against a freshly opened shell or a shared admin connection.

Common Causes

  1. No USE statement issued in the session. The MySQL CLI opens a connection without a default database unless one is passed on the command line (mysql -u root -p mydb).
  2. Missing database in the connection string. A connection URL like mysql://user:pass@host/ (without a database component) leaves the session without a default database.
  3. Importing a dump file without a USE statement. SQL dump files that contain only CREATE TABLE / INSERT statements — without a leading USE dbname; or CREATE DATABASE block — will fail when piped into a session that has no active database.
  4. Running DDL scripts against a root or admin connection. Scripts written for a specific schema but executed via a generic DBA connection that never selects a database.
  5. Stored procedures or events referencing unqualified tables. A procedure defined in one schema calling a table that exists only in another schema, combined with a session that does not default to the correct schema at invocation time.

Troubleshooting and Resolution Steps

  1. Check the current database for the session:

    SELECT DATABASE();
    

    Returns NULL if no database is selected.

  2. Select a database with USE:

    USE myapp;
    SELECT * FROM users;
    
  3. Qualify the table with the schema name to avoid relying on session context entirely:

    SELECT * FROM myapp.users;
    INSERT INTO myapp.orders (user_id, total) VALUES (1, 99.99);
    
  4. Pass the database on the mysql CLI command line so the session starts with a default database:

    mysql -u root -p myapp < migration.sql
    # or interactively:
    mysql -u root -p myapp
    
  5. Add a USE statement to dump or migration files so they are self-contained:

    USE myapp;
    
    CREATE TABLE IF NOT EXISTS users (
      id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
      email VARCHAR(255) NOT NULL
    );
    
  6. Verify your application connection URL includes the database name. For common drivers:

    # JDBC
    jdbc:mysql://localhost:3306/myapp
    
    # Python (mysql-connector / PyMySQL)
    mysql+pymysql://user:pass@localhost/myapp
    
    # Node.js (mysql2)
    mysql://user:pass@localhost:3306/myapp
    
  7. Check stored procedure definitions for unqualified table references if the error appears inside a procedure call:

    SHOW CREATE PROCEDURE my_procedure\G
    

    Rewrite any bare table references to use fully qualified names (schema.table).

Additional Information

  • The SQLSTATE code 3D000 is the standard SQL state for "invalid catalog name," which maps directly to a missing or inaccessible default schema.
  • This error is purely a session-context issue and is unrelated to user privileges — even a superuser will get error 1046 if no database is selected.
  • INFORMATION_SCHEMA tables can be queried without a default database because they are not tied to a single schema: SELECT * FROM information_schema.tables LIMIT 10; works from a session with no active database.
  • MySQL Workbench and other GUI clients default to the schema selected in the sidebar; switching the active schema there changes DATABASE() for subsequent queries.
  • Connection poolers (ProxySQL, PgBouncer-equivalent tools) may reuse connections across different logical users or schemas — ensure the pool's init_connect or equivalent sets the expected default database, or use fully qualified names throughout.

Frequently Asked Questions

Why does my ORM never throw this error but my migration scripts do? ORMs construct the connection URL from configuration that includes the database name, so they issue an implicit USE at connect time. Raw migration scripts run via the CLI or a script runner often connect without specifying a database and rely on a USE statement in the script itself — which is easy to omit.

Can I set a default database at the server level so I never need USE? Not per-server-wide, but you can set a default database per MySQL user with ALTER USER 'app'@'%' DEFAULT ROLE ... — however there is no built-in DEFAULT DATABASE per user. The reliable approaches are to embed the database in the connection string or always qualify table names.

Does fully qualifying every table name (schema.table) eliminate the need for USE? Yes. Fully qualified names are resolved independently of the session's current database. This is the preferred style for stored procedures, views, and any SQL that may run across different connection contexts.

What is the difference between error 1046 and error 1049? Error 1046 (ER_NO_DB_ERROR) means no database has been selected for the session. Error 1049 (ER_BAD_DB_ERROR, "Unknown database") means a database name was specified — either via USE or as a qualifier — but that database does not exist on the server.

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.

We use cookies to provide an optimized user experience and understand our traffic. To learn more, read our use of cookies; otherwise, please choose 'Accept Cookies' to continue using our website.