How to delete or remove a MySQL/MariaDB user account on Linux/Unix

 

 

Step 1 – Steps for removing a MySQL/MariaDB user

If you decided to remove open source application such as WordPress or Drupal you need to remove that user account. You need to remove all permissions/grants, and delete the user from the MySQL table. First, login as mysql root user to the MySQL/MariaDB server using the shell, run:

$ mysql -u root -p mysql
OR
$ mysql -u root -h server-name-here -p mysql

Step 2 – List all mysql users

Once you have a MySQL or MariaDB prompt that looks very similar to fig.01, type the following command at mysql> or mariadb> prompt to see a list of MySQL/MariaDB users:

mariadb> SELECT User,Host FROM mysql.user;

In this above example, I need to delete a mysql user named ‘bloguser’@’localhost’.

Step 3 – List grants for a mysql user

To see what grants bloguser have, enter:

mariadb> SHOW GRANTS FOR 'bloguser'@'localhost';

Where,

  1. bloguser – Mysql/Maridb user name
  2. localhost – Mysql/Mariadb host name
  3. mywpblog – Database name

Step 4 – Revoke all grants for a mysql user

Type the following sql command:
mariadb> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'bloguser'@'localhost';

Step 5 – Remove/Delete the user from the user table

Type the following sql command:
mariadb> DROP USER 'bloguser'@'localhost';

Step 6 – Delete the database

Type the following command:
mariadb> DROP DATABASE mywpblog;

And there you have it. A MySQL/MariaDB user deleted or removed from the server on Unix or Linux via command line option.

A note about DROP USER sql command to delete or remove a MySQL/MariaDB user account

The DROP USER statement removes one or more MariaDB accounts. It removes privilege rows for the account from all grant tables. To use this statement, you must have the global CREATE USER privilege or the DELETE privilege for the mysql database. The syntax is:
mariadb> DROP USER foo;
mariadb> DROP USER IF EXISTS bar;