Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

ERROR 1049 (42000): Unknown database

ErrorERROR 1049 (42000): Unknown database 'mydatabasename'

Short answer

Error 1049 means the server has no database with that exact name. List the databases first. Then either create it, correct the name (names are case-sensitive on Linux), or create the database before you load a dump that has no CREATE DATABASE statement.

  • Applies to MySQL 8.0, MySQL 8.4 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Connect without naming a database and list what the server really has. Compare the names letter by letter.

    mysql -u root -p -e "SHOW DATABASES;"
  2. 2Match what you see, then jump to its fix

    1. 1SHOW DATABASES does not list it at allThe database was never created on this serverFix
    2. 2SHOW DATABASES lists MyDatabase, and you typed mydatabaseThe name differs in letter caseFix
    3. 3The error appears when you run mysql dbname < dump.sqlA dump was loaded into a database that does not exist yetFix

What does the error look like?

  • ERROR 1049 (42000): Unknown database 'mydatabasename'
  • Unknown database 'mydatabasename'

Cause 1 of 3

The database was never created on this server

1Check

You may be on a fresh server, a new container, or a different server from the one you created it on.

mysql -u root -p -e "SELECT @@hostname, @@port; SHOW DATABASES;"

2Fix

Create it. Choose the character set on purpose.

CREATE DATABASE IF NOT EXISTS mydatabasename
  CHARACTER SET utf8mb4;

3Verify

Connect to it by name.

mysql -u root -p mydatabasename -e "SELECT DATABASE();"

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

The name differs in letter case

1Check

Databases are folders in the data directory, so case matters on Linux and most Unix systems but not on Windows. lower_case_table_names is 0 on Unix, 1 on Windows and 2 on macOS by default. A project that worked on Windows can fail on Linux.

mysql -u root -p -e "SHOW VARIABLES LIKE 'lower_case_table_names'; SHOW DATABASES;"

2Fix

Use the exact name in the connection string and in statements. Pick one convention, preferably all lowercase.

mysql -u root -p MyDatabase

3Verify

The client connects without error.

mysql -u root -p MyDatabase -e "SELECT DATABASE();"

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

A dump was loaded into a database that does not exist yet

1Check

mysqldump writes CREATE DATABASE only when --databases or --all-databases is used. A dump of a single database does not contain it.

grep -c -i "CREATE DATABASE" dump.sql

2Fix

Create the database first, then load the dump into it.

mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS mydatabasename"
mysql -u root -p mydatabasename < dump.sql

3Verify

Count the tables that arrived.

mysql -u root -p mydatabasename -e "SHOW TABLES;"

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Use lowercase database names so one config works on Windows, macOS and Linux.
  • Take dumps with --databases when the restore target may be empty.
  • Keep the database name in one environment variable instead of retyping it.

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.