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.
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
- Set
MYSQL_DATABASEin the service to the schema name you want. - Set DB_DATABASE in Laravel to the exact same name.
- Run migrate after the service is healthy.
services:
mysql:
image: mysql:8.0
env:
MYSQL_DATABASE: testing
MYSQL_ROOT_PASSWORD: password
env:
DB_DATABASE: testingCreate the schema explicitly before migrating
If the service cannot pre-create it, create it with a client command first.
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.