SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The DROP USER
statement in MySQL is used to remove one or more MySQL accounts and their privileges. Once an account is dropped, the user will no longer be able to log into the MySQL server.
DROP USER [IF EXISTS] user [, user] ...;
IF EXISTS
: This option allows the statement to succeed even if the specified user account does not exist. If you don't use this clause and the user does not exist, MySQL will return an error.
user
: The name of the user account to drop, specified as 'username'@'hostname'
.
Drop a Single User:
DROP USER 'someuser'@'localhost';
This command will remove the user someuser
who connects from localhost
from the MySQL server.
Drop Multiple Users:
DROP USER 'user1'@'localhost', 'user2'@'localhost';
This will drop both user1
and user2
that connect from localhost
.
Using IF EXISTS
:
DROP USER IF EXISTS 'nonexistentuser'@'localhost';
If the user nonexistentuser
does not exist, this command will not produce an error because of the IF EXISTS
clause.
Privileges: To use DROP USER
, you must have the global CREATE USER
privilege or the DELETE
privilege on the mysql
database.
Impact: The DROP USER
statement will not immediately remove the user's current connections. Those will remain until the user disconnects or until the server is restarted. However, the user won't be able to establish new connections after being dropped.
Cleanup: When a user is dropped using DROP USER
, all global, database, table, and column privileges for the account are revoked. However, if the user has granted privileges to other accounts, those privileges are not revoked by DROP USER
. You might want to ensure that privileges granted by an account are not inadvertently retained by other users when the account is removed.
Always exercise caution when using the DROP USER
statement to ensure you don't unintentionally revoke access from legitimate users. It's typically a good practice to back up your MySQL user
table from the mysql
database before making major changes to user accounts.
How to use DROP USER in MySQL:
DROP USER
statement in MySQL is used to remove a user account.DROP USER 'username'@'host';
Removing a MySQL user account:
DROP USER
statement followed by the username and host.DROP USER 'john_doe'@'localhost';
Managing privileges before dropping a user in MySQL:
REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'host';
DROP USER with host specification in MySQL:
DROP USER 'username'@'localhost';
Revoking privileges before dropping a user:
REVOKE
statement to remove privileges before dropping a user.REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'host';
DROP USER and CASCADE option in MySQL:
CASCADE
option allows you to automatically revoke privileges before dropping the user.DROP USER 'username'@'host' CASCADE;
Checking existing users in a MySQL database:
mysql.user
table.SELECT user, host FROM mysql.user;
DROP USER 'john_doe'@'localhost';
REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'host'; DROP USER 'username'@'host';