How to Delete MySQL User Accounts with DROP USER

By 

Updated on

3 min read

Removing MySQL user accounts

MySQL allows you to create multiple user accounts and grant appropriate privileges so that the users can connect and manage databases.

If the user account is no longer needed, it is a good idea to either remove the user privileges or to completely delete the user account.

This tutorial explains how to delete MySQL/MariaDB user accounts.

DROP USER Statement

In MySQL, you can remove one or more users and assigned privileges with the DROP USER statement. The general syntax of this statement is as follows:

mysql
DROP USER [IF EXISTS] USER_ACCOUNT [, USER_ACCOUNT] ...

For example, to remove the brian@localhost user account, log in to the MySQL shell and run:

mysql
DROP USER 'brian'@'localhost';

On success the command will return:

output
Query OK, 0 rows affected (0.00 sec)

To remove multiple user accounts in a single command, list the accounts separated by commas:

mysql
DROP USER 'brian'@'localhost', 'any'@'localhost';

If you try to drop an account that does not exist without the IF EXISTS clause, the command returns an error.

If the user you are trying to remove is currently logged in, the user session will not be closed and the user will be able to run queries until the session ends. Once the session is closed the user is removed and it will no longer be able to log in to the MySQL server.

Databases and objects created by the user are not removed automatically. You must drop them separately if you no longer need them. In current MySQL releases, DROP USER can also fail if the account is used as the DEFINER for a stored procedure, view, trigger, or event. Check and update those objects before removing the account.

Removing MySQL User Accounts

This section walks through listing and removing MySQL user accounts in practice.

First, log in to the MySQL shell with the root or another administrative user. To do so type the following command:

Terminal
sudo mysql

If you are using the old, native MySQL authentication plugin to log in as root run the command below and enter the password when prompted:

Terminal
mysql -u root -p

The commands below are executed inside the MySQL shell.

MySQL stores information about the users, in the user table in the mysql database. Use the following SELECT statement to get a list of all MySQL user accounts :

Terminal
SELECT User, Host FROM mysql.user;

The output should look something like this:

output
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | localhost |
| luke             | %         |
| jabba            | localhost |
| jabba            | 10.10.8.8 |
| chewbacca        | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)
Info
In MySQL, a user account consists of a user name and hostname parts. jabba@localhost and jabba@10.10.8.8 are different user accounts.

Say the chewbacca@localhost user account is no longer needed and you want to remove it.

To delete the user run:

Terminal
DROP USER 'chewbacca'@'localhost';
output
Query OK, 0 rows affected (0.00 sec)

The command will remove the user account and its privileges.

Now that the user is removed you may also want to remove the databases associated with that user.

Conclusion

To remove a MySQL user account use the DROP USER statement followed by the name of the user you want to remove. For a broader overview of user and database administration, see our guide on managing MySQL databases and users .

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page