MySQL Error 1049 — Unknown Database
Fix MySQL Error 1049 (Unknown database). Learn about database creation, naming conventions, connection string configuration, and common ORM setup mistakes.
Detailed Explanation
MySQL Error 1049: Unknown Database
Error 1049 occurs when you try to connect to or use a database that does not exist on the MySQL server.
Error Format
ERROR 1049 (42000): Unknown database 'myapp_production'
Common Causes
1. Database not created yet:
-- List existing databases
SHOW DATABASES;
-- Create the database
CREATE DATABASE myapp_production
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
2. Typo in database name: MySQL database names are case-sensitive on Linux (because they map to filesystem directories), but case-insensitive on macOS and Windows.
# Check the lower_case_table_names setting
mysql -e "SHOW VARIABLES LIKE 'lower_case_table_names'"
3. Wrong environment configuration:
# .env file might reference the wrong database
DATABASE_URL=mysql://user:pass@localhost:3306/wrong_name
# Fix: use the correct database name
DATABASE_URL=mysql://user:pass@localhost:3306/correct_name
4. ORM migration not run:
# Rails
rails db:create
# Django
python manage.py migrate
# Prisma
npx prisma db push
# Sequelize
npx sequelize db:create
Docker-Specific Issues
When using Docker, the database is created inside the container. If you recreate the container without a persistent volume, the database is lost:
# docker-compose.yml - add a volume!
services:
db:
image: mysql:8
environment:
MYSQL_DATABASE: myapp
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:
Prevention
- Use environment-specific database names (myapp_dev, myapp_test, myapp_prod)
- Include database creation in your setup scripts
- Document the setup process in your project README
- Use Docker volumes for persistent data
Use Case
Error 1049 typically hits developers during initial project setup, environment configuration, or when switching between development and production databases. It is especially common in containerized environments where databases are ephemeral unless explicitly persisted with volumes. Understanding this error and the associated ORM tooling prevents lost data and configuration confusion.