MySQL 8.4Lab-verified · 2026-12-02

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

A database with that name already exists on the server.

MySQL 8.4.13
1007 (HY000): Can't create database 'shop'; database exists

Jump to a cause

  1. 01The database already exists (the script ran before)

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-12-02 · container image mysql:8.4

CREATE DATABASE fails with 1007 when the name is already taken. Nothing is changed: the existing database and its tables stay as they were.

This usually shows up when an install or migration script runs a second time. information_schema.schemata tells you whether the database is really there.

01The database already exists (the script ran before)

shop was created earlier and already has a products table with 1 row. Running CREATE DATABASE shop again fails. information_schema.schemata shows shop with character set utf8mb4.

Setup
Setuprun as root
CREATE DATABASE shop;
CREATE TABLE shop.products (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO shop.products VALUES (1, 'Pen');
OUTPUT
Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)
Reproducerun as root
CREATE DATABASE shop;
✕ ERROR
ERROR 1007 (HY000): Can't create database 'shop'; database exists
Check the causerun as root
SELECT schema_name, default_character_set_name AS charset FROM information_schema.schemata WHERE schema_name = 'shop';
SELECT COUNT(*) FROM shop.products;
OUTPUT
+-------------+---------+
| SCHEMA_NAME | charset |
+-------------+---------+
| shop        | utf8mb4 |
+-------------+---------+
1 row in set (0.00 sec)

+----------+
| COUNT(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

shop is listed in information_schema.schemata, and shop.products already holds 1 row, so the database is in use — do not drop it.

CREATE DATABASE IF NOT EXISTS shop succeeds with 1 warning, and SHOW WARNINGS shows the same 1007 as a Note. The existing data is untouched: shop.products still has 1 row.

Fix: run the corrected statementrun as root
CREATE DATABASE IF NOT EXISTS shop;
SHOW WARNINGS;
SELECT COUNT(*) FROM shop.products;
✓ SUCCESS
Query OK, 1 row affected, 1 warning (0.01 sec)

+-------+------+-----------------------------------------------+
| Level | Code | Message                                       |
+-------+------+-----------------------------------------------+
| Note  | 1007 | Can't create database 'shop'; database exists |
+-------+------+-----------------------------------------------+
1 row in set (0.00 sec)

+----------+
| COUNT(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

FAQ

How do I fix MySQL error 1007 database exists?

If the database should already be there, use CREATE DATABASE IF NOT EXISTS. Check first with SELECT schema_name FROM information_schema.schemata.

Does CREATE DATABASE IF NOT EXISTS overwrite the existing database?

No. In our test the existing shop.products table still had its 1 row after CREATE DATABASE IF NOT EXISTS shop. The statement only adds a Note-level warning.

Why does CREATE DATABASE IF NOT EXISTS show 1 warning?

MySQL reports the existing database as a Note with code 1007 instead of an error. You can see it with SHOW WARNINGS.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13