MySQL error 1396: CREATE USER or DROP USER failed, and how to clean up
Error 1396 usually means the account exists already, or with another host than you typed. List user and host together, then use IF NOT EXISTS and ALTER USER.

On this page
The message doesn't say what's wrong, only which operation and which account:
DROP USER IF EXISTS 'lab_anna'@'localhost', 'lab_anna'@'%';
CREATE USER 'lab_anna'@'localhost' IDENTIFIED BY 'first password';CREATE USER 'lab_anna'@'localhost' IDENTIFIED BY 'second password';ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'lab_anna'@'localhost'In nearly all cases the reason is simple: for CREATE USER the account exists, for DROP USER and ALTER USER it doesn't. What makes it confusing is that an account is a name and a host together, and the part you didn't type is filled in for you.
Look at what is there, both columns:
SELECT user, host, plugin, account_locked FROM mysql.user WHERE user = 'lab_anna';+----------+-----------+-----------------------+----------------+
| user | host | plugin | account_locked |
+----------+-----------+-----------------------+----------------+
| lab_anna | localhost | caching_sha2_password | N |
+----------+-----------+-----------------------+----------------+All runs are on MySQL 8.0.33. MariaDB uses the same number and text.
The host you didn't type
Without a host, MySQL assumes %, which stands for any host:
DROP USER 'lab_anna';ERROR 1396 (HY000) at line 1: Operation DROP USER failed for 'lab_anna'@'%'The message shows it: you asked for 'lab_anna'@'%', and the account is 'lab_anna'@'localhost'. The manual on account names describes the rule. Use the host from the list:
DROP USER 'lab_anna'@'localhost';Quotes are the variant of this mistake that's hardest to see. 'lab_anna@localhost' in one pair of quotes is a user whose name contains an at sign, with the host %. Name and host need a pair each:
CREATE USER 'lab_anna@localhost' IDENTIFIED BY 'x';
SELECT user, host FROM mysql.user WHERE user LIKE 'lab_anna%';
DROP USER 'lab_anna@localhost'@'%';+--------------------+------+
| user | host |
+--------------------+------+
| lab_anna@localhost | % |
+--------------------+------+An account like that also explains a login that fails with 1045 although "the user was created".
Create it if it isn't there
For scripts that may run more than once:
CREATE USER IF NOT EXISTS 'lab_anna'@'localhost' IDENTIFIED BY 'first password';
CREATE USER IF NOT EXISTS 'lab_anna'@'localhost' IDENTIFIED BY 'second password';
SHOW WARNINGS;+-------+------+---------------------------------------------------------+
| Level | Code | Message |
+-------+------+---------------------------------------------------------+
| Note | 3163 | Authorization ID 'lab_anna'@'localhost' already exists. |
+-------+------+---------------------------------------------------------+Read the note as it's meant. The second statement did nothing at all, and the password is still the first one. If the script should also set the password, add an ALTER USER, which is the statement for changing an account that exists:
ALTER USER 'lab_anna'@'localhost' IDENTIFIED BY 'second password';This pair, CREATE USER IF NOT EXISTS and ALTER USER, gives the same result whether the account was there or not. DROP USER IF EXISTS followed by CREATE USER looks equivalent and isn't: it throws away all rights the account had.
MariaDB also has CREATE OR REPLACE USER, which is a drop and a create in one.
Rights don't come back by themselves
DROP USER removes the account with its grants. Before you drop and create one again, write down what it may do:
CREATE DATABASE IF NOT EXISTS lab_1396;
GRANT SELECT, INSERT ON lab_1396.* TO 'lab_anna'@'localhost';
SHOW CREATE USER 'lab_anna'@'localhost';
SHOW GRANTS FOR 'lab_anna'@'localhost';The first of the two SHOW statements prints a complete CREATE USER with the hashed password, so an account can be moved to another server without knowing its password. The second prints the GRANT lines. Together they're a backup of the account. For a whole server, run them in a loop over mysql.user and keep the result next to the nightly dump: a dump of your own databases contains no accounts at all, and a restore on a new machine then ends with a site that can't log in. Read the file before you use it, since it also lists accounts that nobody needs any more.
Renaming in place of creating
If the account exists with the wrong host, it doesn't have to be created again:
RENAME USER 'lab_anna'@'localhost' TO 'lab_anna'@'10.0.0.%';
SELECT user, host FROM mysql.user WHERE user = 'lab_anna';
SHOW GRANTS FOR 'lab_anna'@'10.0.0.%';+----------+----------+
| user | host |
+----------+----------+
| lab_anna | 10.0.0.% |
+----------+----------+
+---------------------------------------------------------------+
| Grants for lab_anna@10.0.0.% |
+---------------------------------------------------------------+
| GRANT USAGE ON *.* TO `lab_anna`@`10.0.0.%` |
| GRANT SELECT, INSERT ON `lab_1396`.* TO `lab_anna`@`10.0.0.%` |
+---------------------------------------------------------------+Password and rights stay. If the target exists already, this too fails with 1396, with RENAME USER in the message.
It isn't in the list, and the error stays
Then the tables and the server's memory disagree. The server reads the accounts from the tables of the mysql schema when it starts and keeps them in memory. The statements for accounts change both. An INSERT or DELETE written by hand changes only the table:
DELETE FROM mysql.user WHERE user = 'anna'; -- don'tAfterwards CREATE USER 'anna' still fails, because the account is in memory. The repair has three steps:
FLUSH PRIVILEGES;
DROP USER IF EXISTS 'anna'@'localhost';
CREATE USER 'anna'@'localhost' IDENTIFIED BY '...';FLUSH PRIVILEGES loads the tables again. The DROP USER IF EXISTS is there for the remains: a hand-made DELETE from mysql.user leaves rows in mysql.db, mysql.tables_priv and others, with the rights of an account that no longer exists. If somebody creates an account with the same name a year later, it inherits them. DROP USER cleans all of these tables, even if the row in mysql.user is missing.
You can look for such orphans:
SELECT d.user, d.host, d.db
FROM mysql.db d LEFT JOIN mysql.user u ON u.user = d.user AND u.host = d.host
WHERE u.user IS NULL;An empty result is the good answer.
The other way to this state is a restored dump of the mysql schema, from an older server or another machine. Run FLUSH PRIVILEGES after it, and restart the server when the versions differ, so that it can bring the system tables up to date. Moving accounts with SHOW CREATE USER and SHOW GRANTS avoids the problem.
When it's the right, not the account
Without the privilege to create accounts the error is 1227, "Access denied; you need (at least one of) the CREATE USER privilege(s)". With a server in read-only mode it's 1290. With a password that the validation component refuses it's 1819. These say what they mean. 1396 comes only for the account itself, or for a role, since roles are stored as accounts:
CREATE ROLE IF NOT EXISTS 'lab_role';CREATE USER 'lab_role'@'%' IDENTIFIED BY 'x';ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'lab_role'@'%'Roles share the name space with users. In mysql.user a role is an account that is locked and has no password. If the list shows a locked row with your name and the host %, that's the one.
In a replica set or a managed database
On a replica, CREATE USER arrives through replication. If somebody created the account on the replica by hand before, replication stops with 1396. Drop the local account, then start replication again. Accounts belong on the source only, unless they're for the replica alone.
Hosting panels keep a list of their own. An account that was dropped in SQL is still in the panel, and the panel then fails to delete it with this error. Create it once more by hand with the same name and host, and delete it through the panel.
DROP USER 'lab_anna'@'10.0.0.%';
DROP ROLE 'lab_role';
DROP DATABASE lab_1396;The statements themselves have pages with every option: CREATE USER and DROP USER. If what fails is the syntax and not the account, for example GRANT ... IDENTIFIED BY on MySQL 8, it's error 1064. In phpMyAdmin all of this happens behind the tab User accounts, which shows user and host in two columns for this reason; if you can't open it at all, start with its login checks.
Same subject
MySQL EXPLAIN in practice: from a slow query to the right index
Put EXPLAIN in front of the query and read four columns: type, key, rows and Extra. They show whether an index is used, how well, and what is done after it.
MySQL server has gone away (2006) and lost connection (2013): the causes
MySQL error 2006 means the connection was closed before your query arrived: check idle timeout, packet size and server restarts, in that order, then reconnect.
MySQL error 1064: how to read the message and find the broken part
MySQL error 1064 quotes the statement from the first token the parser could not accept, so look at that word and the one just before it for the cause.
PHPSolvedThe fix first, then the reason