Skip to content
Latchkey

Laravel "SQLSTATE[HY000] [1049] Unknown database" in CI

MySQL accepted the login but the database named in DB_DATABASE does not exist. The CI service only creates the schema you name in MYSQL_DATABASE, so DB_DATABASE must match it or you must create the schema before migrating.

What this error means

Connecting works but migrate fails with "SQLSTATE[HY000] [1049] Unknown database 'testing'". Laravel authenticated fine; the target schema was never created.

php artisan
Illuminate\Database\QueryException

  SQLSTATE[HY000] [1049] Unknown database 'testing'

Common causes

DB_DATABASE does not match MYSQL_DATABASE

The service auto-creates only the schema in MYSQL_DATABASE. If Laravel points at a different name, that database does not exist.

No database was created at all

The service was started without MYSQL_DATABASE, so no application schema exists for migrations to target.

How to fix it

Create the database in the service and match names

  1. Set MYSQL_DATABASE in the service to the schema name you want.
  2. Set DB_DATABASE in Laravel to the exact same name.
  3. Run migrate after the service is healthy.
.github/workflows/ci.yml
services:
  mysql:
    image: mysql:8.0
    env:
      MYSQL_DATABASE: testing
      MYSQL_ROOT_PASSWORD: password
env:
  DB_DATABASE: testing

Create the schema explicitly before migrating

If the service cannot pre-create it, create it with a client command first.

Terminal
mysql -h 127.0.0.1 -uroot -ppassword -e "CREATE DATABASE IF NOT EXISTS testing;"

How to prevent it

  • Keep DB_DATABASE identical to MYSQL_DATABASE.
  • Always define MYSQL_DATABASE in the DB service.
  • Create the schema before migrate if the image does not.

Frequently asked questions

What causes ""[1049] Unknown database""?
The service auto-creates only the schema in MYSQL_DATABASE. If Laravel points at a different name, that database does not exist.
How do I fix "[1049] Unknown database"?
Create the database in the service and match names

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card