mysqldude
Error codes › 1046

MySQL & MariaDB error 1046: No database selected

Error messageNo database selected
Example from our test runs: No database selected (5.6.51)

The statement uses a table name without a database, and the connection has no default database. Name one when connecting, run USE, or write db.table.

MySQLMariaDB
SymbolER_NO_DB_ERRORER_NO_DB_ERROR
SQLSTATE3D0003D000
TypeServer errorServer error
Versionsevery release since 5.0 activeevery release since 5.5 active

What it means

An unqualified name like orders is looked up in the connection’s default database. A connection opened without one (no database in the connection string, no USE) has nowhere to look, so every unqualified table reference fails with 1046.

Why it happens

  • mysql -u app -p with no database argument, followed by SELECT * FROM orders.
  • A connection string or DSN without the database part (mysql://app@db/ instead of mysql://app@db/shop).
  • Importing a dump made with --all-databases or --databases works because it contains USE statements. A dump of a single database made without --databases doesn’t, so importing it without naming a target database gives 1046.
  • SELECT DATABASE(); returns NULL when this is the problem.

How to fix it

  • Name the database when connecting: mysql -u app -p shop < dump.sql.
  • Or start the script with USE shop;.
  • Or qualify the names: SELECT * FROM shop.orders. This also avoids surprises when a pooled connection’s default database was changed by earlier code.

Reproduce it yourself

These steps are the exact test our harness runs. Use a scratch server, never production.

  1. Connect with mysql -u root -p and create a scratch database:

    CREATE DATABASE demo;
    USE demo;
  2. In a shell on the database host:

    mysql -uroot -p -e 'SELECT * FROM t'

You get: ERROR 1046 (3D000) at line 1: No database selected(11.8.9)

Verified fixes

  1. Name the database on the command line (or USE it, or qualify the table)

    CREATE TABLE t (a INT);
    INSERT INTO t VALUES (1);
    mysql -uroot -p -N demo -e 'SELECT a FROM t'

Tested on

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Connect without naming a database, then use an unqualified table name
repro
✓ 1046✓ 1046✓ 1046✓ 1046✓ 1046✓ 1046✓ 1046✓ 1046✓ 1046
Name the database on the command line (or USE it, or qualify the table)
fix
✓✓✓✓✓✓✓✓✓
Connect without naming a database, then use an unqualified table name repro
MySQL 5.6✓ 10465.7✓ 10468.0✓ 10468.4✓ 10469✓ 1046
MariaDB 10.6✓ 104610.11✓ 104611.4✓ 104611.8✓ 1046
Name the database on the command line (or USE it, or qualify the table) fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

Related errors

  • 1049 ER_BAD_DB_ERRORexplained
  • 1146 ER_NO_SUCH_TABLEexplained