PHP error guide

SQLSTATE HY000 unknown database: meaning and fix

Error summary

Create the intended schema through migrations or correct the DSN database name. The server is reachable but the schema named by the DSN does not exist.

What it means

Create the intended schema through migrations or correct the DSN database name. The server is reachable but the schema named by the DSN does not exist.

What the error means

This message means that the server is reachable but the schema named by the DSN does not exist. The exact signature distinguishes sqlstate hy000 unknown database from a generic application failure.

Why PHP produces it

The engine or service reports “SQLSTATE HY000 unknown database” because its required precondition was not met. Create the intended schema through migrations or correct the DSN database name.

PHP version notes

The sqlstate hy000 unknown database wording here is based on PDO with MySQL/MariaDB; exact server wording varies; punctuation and exception class names can differ on older branches or vendor builds.

Most common causes

  • The immediate input or configuration reaches the specific condition: the server is reachable but the schema named by the DSN does not exist
  • The code path assumes the prerequisite for sqlstate hy000 unknown database has already been satisfied.
  • For sqlstate hy000 unknown database, development and production differ in version, extension, permissions, paths, or service configuration.
  • An earlier operation returned an unchecked value that is consumed by the line reporting sqlstate hy000 unknown database.

Minimal examples

BAD — reproduces the problem

php
$dsn = "mysql:dbname=inventroy;host=db";

FIXED — safer pattern

php
$dsn = "mysql:dbname=inventory;host=db;charset=utf8mb4";

Step-by-step diagnosis

  1. Copy the complete “SQLSTATE[HY000] [1049] Unknown database 'inventory'” text and retain the first application stack frame.
  2. Reproduce sqlstate hy000 unknown database in the same SAPI and environment listed for this page.
  3. Before changing sqlstate hy000 unknown database, inspect the preceding value or directive and verify its type, path, version, and permissions.
  4. Apply the narrow correction—create the intended schema through migrations or correct the DSN database name—then repeat the original request once.

Fixes

Correct the failing prerequisite

Create the intended schema through migrations or correct the DSN database name

php
$dsn = "mysql:dbname=inventory;host=db;charset=utf8mb4";

Fail explicitly at the boundary

Validate the condition before the operation that emits sqlstate hy000 unknown database, and log a safe diagnostic without credentials or full production paths.

Common mistakes when fixing it

  • Suppressing sqlstate hy000 unknown database instead of correcting its upstream condition.
  • Testing sqlstate hy000 unknown database only with the CLI binary when the failing request runs under FPM or Apache.
  • Changing a global setting for sqlstate hy000 unknown database before confirming the site-specific effective configuration.

How to prevent it

  • Add a focused test that exercises the boundary responsible for sqlstate hy000 unknown database.
  • Keep runtime versions, extensions, configuration, and deploy artifacts affecting sqlstate hy000 unknown database reproducible.
  • Validate external data and service return values before they can trigger sqlstate hy000 unknown database.

Web server / environment notes

fpm, cli, docker, linux. The failure occurs where the server is reachable but the schema named by the DSN does not exist

Tags: fpm,cli,docker,linux

Categories