Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
DevOps

How To Remove or Delete MySQL User or Database Account?

Introduction

As a database administrator or developer, you may encounter scenarios where you need to remove outdated or unnecessary user accounts or delete entire databases. In this blog, we will explore the steps to delete MySQL user accounts and databases using the appropriate commands and syntax. Understanding how to revoke privileges, delete users, and remove databases safely is essential to maintaining a well-organized and secure MySQL environment. Continue reading to acquire all the helpful insights. 

Deleting a MySQL Account

MySQL accounts consist of both the user account and the associated database permissions. Every user account is uniquely identified by a username and is linked with specific privileges that explain what actions the user can execute within the MySQL server. Removing a user account is a significant action and requires careful consideration, as it will revoke access to the associated databases and any privileges granted to the user. Therefore, it is essential to double-check and confirm the accounts you intend to delete and their respective privileges before proceeding.

DROP USER Syntax

To delete a MySQL user account, you can utilize the DROP USER statement. The DROP USER statement syntax is as follows:

DROP USER [IF EXISTS] user [, user] …
DROP USER Syntax
  • IF EXISTS (Optional): This clause prevents an error from occurring if the user does not exist. If you attempt to delete a user that does not exist, MySQL will raise an error by default. Including IF EXISTS ensures that the statement executes without error even if the specified user does not exist, making it a safe option for removing users.
  • user: The name of the MySQL user account you want to delete. You can specify multiple users, separated by commas, to delete them in a single DROP USER statement.

For example, to delete a user named “john,” the DROP USER query would be:

DROP USER john;

DROP USER john;

By using the DROP USER statement with or without the IF EXISTS clause, you can efficiently remove MySQL user accounts from the server.

Also Read: Steps to Unzip/Extract tar.gz Files in Linux Using Command Line

Deleting a MySQL Database

MySQL databases can also be removed when they are no longer needed. Before deleting a database, make sure to back up any essential data, as the process is irreversible. Deleting a database will result in the loss of all data and schema associated with it.

To delete a MySQL database, you can use the DROP DATABASE statement. The syntax is given below:

DROP DATABASE [IF EXISTS] database_name;
  • IF EXISTS (Optional): This clause prevents an error from occurring if the database does not exist. It is optional but recommended to use for safe execution.
  • database_name: The name of the MySQL database you want to delete.

For example, to delete a database named “test_db,” the query would be:

DROP DATABASE test_db;

DROP DATABASE test_db;

Remove Multiple MySQL Users

To remove multiple MySQL users in one go, you can utilize the DROP USER statement and list all the users you wish to delete, separated by commas. This method streamlines the process, saving time and effort when managing user accounts..

Follow these detailed steps to delete multiple MySQL users simultaneously:

Step 1: List Existing Users Begin by listing all the user accounts currently present in the MySQL server. You can use the following query to view the users:

SELECT User FROM mysql.user;

This query will display a list of all the MySQL user accounts currently set up on the server.

Step 2: Identify Users to Remove Review the list of users obtained from Step 1 and identify the ones you want to remove. Ensure you have a concise apprehension of their responsibilities, and the authorities they possess before proceeding with the deletion.

Step 3: Use DROP USER with Multiple Users After confirming the users you wish to delete, construct the DROP USER statement to remove them in one execution. Separate each user with a comma, as demonstrated in the example below:

DROP USER user1, user2, user3;

Replace user1, user2, user3, and so on, with the actual names of the MySQL users you want to remove. Executing this statement will drop all the specified users, revoking their access to the server and associated databases.

Step 4: Verify Users Removed To ensure that the removal was successful, re-run the following query to check if the specified users are no longer listed in the MySQL user accounts:

SELECT User FROM mysql.user;
SELECT User FROM mysql.user;

This query will display the current list of MySQL user accounts after the deletion. The users you dropped should no longer appear in the results.

Alternatively, you can use a wildcard pattern to delete users whose names match a specific pattern. For instance, to delete all users whose names start with “test_,” you can employ the following query:

DROP USER 'test_%';

This will remove all users with usernames that begin with “test_”. However, exercise caution when using wildcards for user deletion, as it may unintentionally remove more users than intended.

By following these detailed steps, you can efficiently remove multiple MySQL users at once, simplifying the user management process and ensuring a secure and organized MySQL environment.

Drop a Connected / Active User

When attempting to delete a user who is currently connected to the MySQL server, the deletion may fail. In such cases, you have two options to handle this situation effectively:

Option 1: Terminate the User’s Active Sessions

Identify Active Sessions: To proceed with the deletion, you must first identify the active sessions of the user you wish to remove. To do this, execute the SHOW PROCESSLIST; command. This will show a list of all active connections to the MySQL server along with their corresponding connection IDs.

SHOW PROCESSLIST;

SHOW PROCESSLIST;

Find the User’s Connection ID: In the result of the SHOW PROCESSLIST; command, locate the row that corresponds to the user you want to remove. Note down the connection ID associated with the user’s session.

For example, let’s assume the user “john” has an active session with a connection ID of 123.

Terminate the Active Session: To forcibly disconnect the user from the MySQL server, use the KILL command followed by the connection ID you identified in the previous step.

KILL 123;

KILL 123;

This will terminate the user’s active session, allowing you to proceed with the deletion.

Drop the User Account: Now that the user’s active session has been terminated, you can safely proceed with the user’s account deletion using the DROP USER statement.

DROP USER ‘john’@’localhost’;

DROP USER 'john'@'localhost';

Option 2: Use the DROP USER Command with IF EXISTS

Drop User with IF EXISTS: To avoid errors when attempting to delete a user who is actively connected, you can use the DROP USER command with the IF EXISTS clause. This clause prevents the statement from raising an error if the mentioned user does not exist. However, the user will not be deleted until their sessions are closed.

DROP USER IF EXISTS ‘john’@’localhost’;

This option allows you to attempt the user deletion without waiting for active sessions to be closed, making it a more convenient approach.

It’s highly critical to consider the implications of using the IF EXISTS clause. While it prevents errors, it might not give immediate feedback on whether the user has been successfully deleted if active sessions are still open.

By following these detailed steps and selecting the appropriate option based on your requirements, you can effectively handle the deletion of MySQL users with active connections, maintaining a secure and well-managed database environment.

Also Read: Ping Command Examples for Linux Users

Conclusion

Managing MySQL user accounts and database access is crucial for database security and maintenance. In this blog, we discussed the steps to delete a MySQL account using the DROP USER syntax, as well as how to delete entire databases with the DROP DATABASE statement. We also emphasized the importance of understanding user privileges and their potential impact before removing any accounts.

Remember always to exercise caution when deleting users or databases, as these operations are permanent and can result in data loss. Before proceeding with deletions, ensure you have taken necessary backups and understand the implications of removing users and their associated privileges. By following the outlined steps and using the appropriate SQL statements, you can effectively know how to delete a MySQL database and account as well as delete duplicate MySQL while maintaining the security and integrity of your MySQL environment.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles