SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

MySQL | DROP USER

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.

Syntax:

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'.

Example:

  1. Drop a Single User:

    DROP USER 'someuser'@'localhost';
    

    This command will remove the user someuser who connects from localhost from the MySQL server.

  2. Drop Multiple Users:

    DROP USER 'user1'@'localhost', 'user2'@'localhost';
    

    This will drop both user1 and user2 that connect from localhost.

  3. 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.

Important Points:

  1. Privileges: To use DROP USER, you must have the global CREATE USER privilege or the DELETE privilege on the mysql database.

  2. 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.

  3. 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.

  1. How to use DROP USER in MySQL:

    • The DROP USER statement in MySQL is used to remove a user account.
    DROP USER 'username'@'host';
    
  2. Removing a MySQL user account:

    • To remove a MySQL user account, use the DROP USER statement followed by the username and host.
    DROP USER 'john_doe'@'localhost';
    
  3. Managing privileges before dropping a user in MySQL:

    • Before dropping a user, it's essential to revoke any privileges associated with the user.
    REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'host';
    
  4. DROP USER with host specification in MySQL:

    • You can specify the host when dropping a user to remove a user for a specific host.
    DROP USER 'username'@'localhost';
    
  5. Revoking privileges before dropping a user:

    • Use the REVOKE statement to remove privileges before dropping a user.
    REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'host';
    
  6. DROP USER and CASCADE option in MySQL:

    • The CASCADE option allows you to automatically revoke privileges before dropping the user.
    DROP USER 'username'@'host' CASCADE;
    
  7. Checking existing users in a MySQL database:

    • To see existing users, query the 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';