Menu

MySQL Error 1007: Database Already Exists (HY000)

Fix MySQL Error 1007 when CREATE DATABASE finds an existing database. Check its settings, reuse it, alter its defaults, or replace it safely.

Posted on By
On this page

MySQL Error 1007 (HY000, ER_DB_CREATE_EXISTS) means that a CREATE DATABASE statement tried to create a database whose name is already in use. The usual message is:

ERROR 1007 (HY000): Can't create database 'app_db'; database exists

The MySQL 8.4 error reference identifies this as an attempt to create an existing database. Confirm that app_db is the database you intended before changing or deleting anything.

Check the existing database

Use INFORMATION_SCHEMA to confirm the exact database name, then inspect its defaults:

SELECT SCHEMA_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'app_db';

SHOW CREATE DATABASE `app_db`;

SHOW CREATE DATABASE displays the statement and options for the existing database, including its default character set and collation. If the database is missing from this server, check the connection host and account; the failing command may be reaching a different server or environment than expected.

Choose the fix that matches your intent

Use the existing database

If app_db is the intended database, do not create it again. Select it and continue:

USE `app_db`;
SELECT DATABASE();

The second statement confirms which database the session selected. A database name can exist without being the current database for the session.

Make a setup script safe to rerun

If a bootstrap script may run more than once, add IF NOT EXISTS:

CREATE DATABASE IF NOT EXISTS `app_db`;

This avoids Error 1007 when the database is already there. It keeps the existing database in place; it does not replace it or update its defaults. Use SHOW CREATE DATABASE to check the current settings. If database defaults need to change, use a reviewed ALTER DATABASE statement and review table definitions separately.

Create a separate database

If you intended to create an independent database, choose a distinct name and confirm that the application or migration will use that name consistently.

Replace a database only if its contents can be discarded

Dropping a database removes the database and all of its tables. Back up and verify the target before replacing it; never add DROP DATABASE merely to silence Error 1007. See the MySQL DROP DATABASE guide for its effects.

Distinguish Error 1007 from Error 1050

Error 1007 means a database already exists. Error 1050 means a table already exists. Check which object the failing CREATE statement names before changing the schema.

For the full syntax and examples, see the MySQL CREATE DATABASE tutorial. Browse more fixes in MySQL error troubleshooting.