MySQL & MariaDB error 1396: Operation CREATE USER / DROP USER failed
Operation CREATE USER failed for 'app1'@'%' (5.6.51) The account statement hit an account that does or doesn't exist. Accounts are user@host pairs, so 'app'@'%' and 'app'@'localhost' are different accounts.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_CANNOT_USER | ER_CANNOT_USER |
| SQLSTATE | HY000 | HY000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
The message names the operation and the account, e.g. Operation CREATE USER failed for 'app'@'%'. CREATE USER fails because the account already exists; DROP USER, RENAME USER and ALTER USER fail because it doesn’t.
Why it happens
- A setup script runs twice and creates the same account again.
- The host part doesn’t match.
DROP USER 'app'means'app'@'%', which isn’t the same account as'app'@'localhost'or'app'@'10.0.%'. - Someone removed rows from
mysql.userwithDELETEinstead ofDROP USER, so other grant tables or the in-memory cache still know the account.CREATE USERthen fails untilFLUSH PRIVILEGES. Never edit the grant tables directly.
How to fix it
- List the accounts:
SELECT user, host FROM mysql.user WHERE user = 'app';and use the exactuser@hostpair. - Make scripts repeatable with
CREATE USER IF NOT EXISTSandDROP USER IF EXISTS(MySQL 5.7+, MariaDB). MySQL 5.6 doesn’t supportIF [NOT] EXISTShere. - If grant tables were edited by hand,
FLUSH PRIVILEGES;and thenDROP USERthe account properly before creating it again.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database:CREATE DATABASE demo; USE demo;Run:
CREATE USER 'app1'@'%' IDENTIFIED BY 'Test#1234'; CREATE USER 'app1'@'%' IDENTIFIED BY 'Test#1234';
You get: Operation CREATE USER failed for 'app1'@'%'(11.8.9)
Verified fixes
Check which user@host pairs exist before creating or dropping
CREATE USER 'app1'@'%' IDENTIFIED BY 'Test#1234'; SELECT user, host FROM mysql.user WHERE user = 'app1';CREATE USER IF NOT EXISTS / DROP USER IF EXISTS (MySQL 5.7+, MariaDB)
CREATE USER IF NOT EXISTS 'app1'@'%' IDENTIFIED BY 'Test#1234'; CREATE USER IF NOT EXISTS 'app1'@'%' IDENTIFIED BY 'Test#1234'; DROP USER IF EXISTS 'app1'@'localhost'; SELECT COUNT(*) FROM mysql.user WHERE user = 'app1';
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Create an account that already exists repro | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 |
| Drop an account that doesn't exist (the host part must match exactly) version check | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 | ✓ 1396 |
| Check which user@host pairs exist before creating or dropping fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| CREATE USER IF NOT EXISTS / DROP USER IF EXISTS (MySQL 5.7+, MariaDB) fix | ✗ 1064 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
What the warnings and failures say (1)
- CREATE USER IF NOT EXISTS / DROP USER IF EXISTS (MySQL 5.7+, MariaDB) (MySQL 5.6): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS 'app1'@'%' IDENTIFIED BY 'Test#