Check if a user exists in MySQL and drop it?


To check how many users are present in MySQL, use MySQL.user table. The syntax is as follows to check how many users are present.

mysql> SELECT User FROM mysql.user;

The following output displays the users −

+------------------+
| User             |
+------------------+
| Mac              |
| Manish           |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| Smith            |
| am               |
+------------------+
8 rows in set (0.00 sec)

Now you can check and drop the user if it exist.

The syntax is as follows to drop a user from MySQL −

DROP USER IF EXISTS yourUserName;

Now you can implement the above syntax in order to drop if the user exists. I am applying drop on user ‘Mac’. The query is as follows.

mysql> DROP USER IF EXISTS Mac;
Query OK, 0 rows affected (0.11 sec)

Check whether the user ‘Mac’ has been deleted or not. The query is as follows −

mysql> SELECT User FROM mysql.user;

The following is the output that displays user ‘Mac’ successfully deleted −

+------------------+
| User             |
+------------------+
| Manish           |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| Smith            |
| am               |
+------------------+
7 rows in set (0.00 sec)

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements