To check how many users exist in MySQL, use the MySQL.user table. The syntax to check how many users are currently there is as follows.
mysql> SELECT User FROM mysql.user;
The following output shows the user -
+------------------+ | User | +------------------+ | Mac | | Manish | | mysql.infoschema | | mysql.session | | mysql.sys | | root | | Smith | | am | +------------------+ 8 rows in set (0.00 sec)
Now you can check and delete the user if it exists.
The syntax to delete a user from MySQL is as follows -
DROP USER IF EXISTS yourUserName;
Now you can implement the above syntax to delete the user if it exists. I'm 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 if user "Mac" has been deleted. The query is as follows -
mysql> SELECT User FROM mysql.user;
The following is the output showing that the user "Mac" has been successfully deleted -
+------------------+ | User | +------------------+ | Manish | | mysql.infoschema | | mysql.session | | mysql.sys | | root | | Smith | | am | +------------------+ 7 rows in set (0.00 sec)
The above is the detailed content of Check if user exists in MySQL and delete it?. For more information, please follow other related articles on the PHP Chinese website!