MySQL User Management: List Users, Check Privileges & Best Practices

1. Overview of MySQL Users

MySQL user management is a crucial aspect of database security and operation. Proper user management helps prevent unauthorized access to the database and improves operational efficiency by granting appropriate privileges. This article will explain in detail how MySQL user information is stored and how it can be listed.

1.1 Importance of MySQL User Management

Since databases store highly sensitive information, proper user management is essential. By setting granular access privileges for each user, you can enhance security and maintain data integrity.

2. Location of User Information in MySQL

MySQL user information is stored in the user table within the mysql database. This table contains various information related to users, such as usernames, hostnames, passwords (encrypted), and authentication plugins.

2.1 Structure of the mysql.user Table

The mysql.user table has many columns that store various information related to each user. These columns include:

  • Host: The hostname from which the user accesses the database
  • User: The username
  • Password: The encrypted password
  • plugin: The type of authentication plugin
  • Other security-related settings

Access to this table requires root privileges or SELECT privileges on mysql.user.

3. Retrieving a List of MySQL Users

To retrieve a list of MySQL users, you need to query the mysql.user table. Below, we will explain how to obtain a basic user list and how to display passwords.

3.1 Basic Query to List Users

First, we will introduce a basic query to retrieve a list of users from the mysql.user table.

SELECT Host, User FROM mysql.user;

Executing this query will display a list of all users and their respective hosts.

3.2 Displaying Encrypted Passwords

To retrieve a list including the encrypted passwords of users, use the following query:

SELECT Host, User, Password FROM mysql.user;

This query displays the hostname, username, and the encrypted password. However, since the password is encrypted, you cannot know the actual password.

4. Retrieving Other User Information

MySQL allows you to retrieve further information about users. In particular, checking user privileges and information about currently connected users can be useful.

4.1 Checking User Privileges

To check the privileges set for each user, use the SHOW GRANTS statement.

SHOW GRANTS FOR 'username'@'host';

This command displays all the privileges granted to the specified user. Properly managing user privileges is essential for enhancing database security.

4.2 Checking Currently Connected Users

To check the information of currently connected users, use the user() and current_user() functions.

SELECT user(), current_user();

This query returns the username specified at the time of connection and the username that was actually authenticated. Usually, both are the same, but they may differ in specific situations such as anonymous users.

5. Practical Examples

Next, we will introduce practical examples of displaying the user list in MySQL.

5.1 Steps to Display the User List

  1. Log in to MySQL. As an example, we will use the root user.
    mysql -u root -p
  2. To display the user list, execute the following query:
    SELECT Host, User FROM mysql.user;
  3. To display the list including the encrypted passwords, use the following query:
    SELECT Host, User, Password FROM mysql.user;

5.2 Checking Privileges

To check the privileges of a specific user, use the SHOW GRANTS statement.

SHOW GRANTS FOR 'tanaka'@'localhost';

This command displays the privileges granted to the user tanaka.

6. Best Practices for MySQL User Management

When managing MySQL users, it is important to follow several best practices.

6.1 Regularly Reviewing the User List

It is recommended to regularly check the mysql.user table to identify any unnecessary users or users with inappropriate privileges.

6.2 Using Strong Passwords

Ensure that each user has a strong and complex password. This helps reduce the risk of unauthorized access.

6.3 Granting Minimum Necessary Privileges (Principle of Least Privilege)

Grant users only the minimum privileges necessary to perform their required tasks. This enhances the security of the database.

7. Conclusion

In this article, we have explained MySQL user management in detail. Understanding where user information is stored and how to list it is crucial for maintaining database security. Additionally, knowing how to check user privileges and connection information allows you to manage MySQL more efficiently.