This error occurs when you attempt to create a database with CREATE DATABASE but a database with that name already exists. The fix is to use CREATE DATABASE IF NOT EXISTS to make the statement idempotent, or drop the existing database first if you want to start fresh.
MySQL error 1007 (SQLSTATE: HY000, ER_DB_CREATE_EXISTS) is a protective mechanism that prevents duplicate databases from being created on your server. When you execute a CREATE DATABASE statement, MySQL checks if a database with that name already exists. If it does, the operation fails immediately with this error to preserve data integrity and prevent accidental overwrites.
First, verify what databases currently exist on your MySQL server:
SHOW DATABASES;Look through the list to confirm whether the database name you're trying to create already exists. On Unix systems, names are case-sensitive, so "myapp" and "MyApp" are different databases.
Modify your CREATE DATABASE statement to include the IF NOT EXISTS clause:
CREATE DATABASE IF NOT EXISTS myapp;This syntax tells MySQL to create the database only if it doesn't already exist. If the database exists, the statement succeeds silently without error. This is the safest approach for scripts that may run multiple times.
If you want to completely reset the database (delete all data), use DROP DATABASE IF EXISTS before creating:
DROP DATABASE IF EXISTS myapp;
CREATE DATABASE myapp;The IF EXISTS clause prevents errors if the database doesn't exist. Use this only when you're certain all data in the database should be deleted.
When restoring from a mysqldump backup, you have two options:
Option 1: Modify the dump file before importing. Open the SQL dump file in a text editor and remove or modify the CREATE DATABASE line to include IF NOT EXISTS:
CREATE DATABASE IF NOT EXISTS myapp;Option 2: Add the --add-drop-database flag when creating the dump initially (for future backups):
mysqldump --add-drop-database -u root -p myapp > backup.sqlThis automatically includes DROP DATABASE IF EXISTS in the dump file.
If you don't need to recreate the database and just need to work with it, simply select it:
USE myapp;You can verify which database is currently selected with:
SELECT DATABASE();On Unix/Linux systems, database names are case-sensitive by default, while Windows treats them case-insensitively. This means on Unix, "Sales" and "sales" are two distinct databases, but they would collide on Windows. Always use consistent casing in your scripts and connection strings. For high-availability setups with replication, ensure all replica servers have the IF NOT EXISTS clause in their initialization scripts to prevent cascade failures across the replication chain. When using Docker or containerized MySQL, the initialization scripts (typically in /docker-entrypoint-initdb.d/) should always use IF NOT EXISTS to allow containers to be recreated without manual cleanup.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL