How to remove a MySQL/MariaDB user
Last updated: Aug 10, 2022
Colleagues hello to all.
In today's article, I'll tell you how to correctly delete a user from a Mysql database. If you have outdated or accounts in your database it's best to get rid of them. Having even one unnecessary user is an additional vulnerability and an opportunity for an attacker to attack your database.
First, we need to review all the users in our Mysql database to determine which user we no longer need. To revise users you can use sql query:
mysql> SELECT user, host FROM mysql.user;
As a result, the database will show us all the users that exist in our Mysql database.
For example, today we will be deleting the user application. You can of course delete it right away, but it is considered good practice to start checking its rights. To view user rights, use the sql query:
mysql> SHOW GRANTS FOR 'application'@'%';
As you can see, the user has no particular rights.
If the user has any rights, then first they need to be removed, for this we will use the sql query:
mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'application'@'%';
This command will remove absolutely all the rights that the user has.
Now that we have decided which user we will delete and stripped him of all rights in the database, now we can safely delete him. To delete, use the sql query:
mysql> DROP USER 'application'@'%';
User deleted successfully!
Now we need to execute the sql command, which resets the cache on the table with users and the table permissions.
mysql> FLUSH PRIVILEGES;
So, colleagues in today's article, we got to know how to correctly delete users from the Mysql database, this approach will also work for Mariadb.
Thank you all, I hope that my article helped you in some way.