MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL DROP/DELETE USER: Delete User

To delete a user account in MySQL, you use the DROP USER statement. Here's how to do it:

Step 1: Login to MySQL

First, you need to login to MySQL using the mysql command in your terminal or command prompt:

mysql -u root -p

You will be prompted to enter the password for the root user.

Step 2: Drop User

Once you are logged in, you can delete a user with the DROP USER command:

DROP USER 'username'@'localhost';

Replace 'username' with the name of the user you want to delete.

Note:

  • The 'localhost' part after the @ symbol specifies that you want to drop the account for the user who connects from the localhost. If the user connects from another host, you should specify that host name instead of localhost. If the user can connect from any host, use the '%' wildcard character: 'username'@'%'.

Step 3: Exit MySQL

You can exit the MySQL shell by typing:

EXIT;

Important:

  • Be careful with the DROP USER statement as it permanently removes a user from the MySQL server.
  • You must have the global CREATE USER privilege or the DELETE privilege for the mysql system database to use the DROP USER statement.
  • The DROP USER statement also removes the user's privileges and deletes the account's rows from the user table in the mysql system database.
  • Before MySQL 5.0.2, you cannot drop a user that has any privileges. You need to manually revoke all of its privileges first. Starting from MySQL 5.0.2, you can drop a user regardless of its privileges.
  1. Deleting a user in MySQL:

    • Description: Deleting a user involves removing the user account from the MySQL server.
    • Code:
      DROP USER 'username'@'hostname';
      
  2. How to remove a user in MySQL:

    • Description: Removing a user is synonymous with deleting the user account in MySQL.
    • Code:
      DROP USER 'username'@'hostname';
      
  3. MySQL DELETE USER command:

    • Description: The command to delete/remove a user account in MySQL.
    • Code:
      DROP USER 'username'@'hostname';
      
  4. Revoke privileges and drop user in MySQL:

    • Description: Before deleting a user, it's a good practice to revoke privileges associated with that user.
    • Code:
      REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'hostname';
      DROP USER 'username'@'hostname';
      FLUSH PRIVILEGES;
      
  5. Removing MySQL user accounts:

    • Description: The process of removing user accounts, which includes revoking privileges and deleting the user.
    • Code:
      REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'hostname';
      DROP USER 'username'@'hostname';
      FLUSH PRIVILEGES;
      
  6. Delete user and revoke privileges in MySQL:

    • Description: Combining the steps of revoking privileges and deleting the user into a single operation.
    • Code:
      REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'hostname';
      DROP USER 'username'@'hostname';
      FLUSH PRIVILEGES;
      
  7. MySQL drop user and associated privileges:

    • Description: Dropping a user includes revoking all privileges associated with that user.
    • Code:
      REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'hostname';
      DROP USER 'username'@'hostname';
      FLUSH PRIVILEGES;
      
  8. MySQL user account cleanup:

    • Description: Ensuring a clean removal of a user account by revoking privileges and deleting the user.
    • Code:
      REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'hostname';
      DROP USER 'username'@'hostname';
      FLUSH PRIVILEGES;