Cara Menampilkan dan Mengelola Pengguna MySQL: Tampilkan Pengguna, Hak Akses, Buat & Perbaiki Kesalahan

1. Introduction

Managing users in MySQL is an essential task for maintaining database security and ensuring efficient operations. By understanding which users can access the database and what privileges they have, you can enforce proper access control and strengthen overall security.

This article explains how to display a list of MySQL users, retrieve detailed user information, create and manage users, and resolve common errors. By the end, you will understand both the fundamentals and practical procedures of MySQL user management.

2. How to Retrieve a List of Users

To retrieve a list of users in MySQL, you use the mysql.user table. This table stores information about all users registered in MySQL, and you can display the necessary information using simple commands.

Basic Method to Display the User List

First, log in to MySQL and execute the following command:

SELECT Host, User FROM mysql.user;

This command displays a list of each user’s Host and User name. For example, host names such as “localhost” or “127.0.0.1” may appear. These typically represent local connections (IPv4 or IPv6). This configuration is commonly seen in local environment user management.

Retrieving All User Information

If you need more detailed information, you can display all columns in the mysql.user table with the following command:

SELECT * FROM mysql.user;

This command displays detailed information for each user (such as authentication details and privilege settings). However, it is generally easier to read when you specify only the required columns. While the above command provides comprehensive information, selecting specific columns often results in more organized and readable output.

3. Retrieving Detailed User Information

In MySQL, you can also retrieve password hashes and privilege information for users, making user management more efficient.

How to Display Password Hashes

In MySQL, passwords are encrypted and stored as hashes in the authentication_string column. Use the following command to check the host name, user name, and password hash:

SELECT Host, User, authentication_string FROM mysql.user;

This allows you to verify whether a password is set for each user. Although the hashed value is displayed, it cannot be decrypted, which maintains security. This method is especially useful when verifying newly created users or checking privilege assignments.

Checking Privileges for a Specific User

To check what privileges a user has on databases or tables, use the SHOW GRANTS command:

SHOW GRANTS FOR 'username'@'hostname';

This command displays all privileges granted to the specified user. It is particularly useful for database administrators when determining or reviewing assigned permissions.

4. Creating and Managing Users

This section introduces how to create new users in MySQL and how to configure or modify their privileges.

Creating a New User

To create a new user, use the CREATE USER statement. The following example creates a user named example_user with the password password123:

CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'password123';

After creating a user, you must run FLUSH PRIVILEGES to apply the changes in MySQL:

FLUSH PRIVILEGES;

Important Notes When Deleting a User

To remove an unnecessary user, use the DROP USER command. However, before deleting a user, it is important to review related privileges and the potential impact on data. For example, if the user being deleted is linked to other systems or services, removing the account may cause access failures. Always proceed with caution.

5. Common Errors and Their Solutions

Errors may occur during MySQL user management. Below are some common errors and how to resolve them.

Permission Error: “Access denied”

The “Access denied” error occurs when a user does not have the appropriate privileges for a database or table. In this case, an administrator can resolve the issue by granting the required privileges using the following command:

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'hostname';
FLUSH PRIVILEGES;

Kesalahan Otentikasi Pengguna: “Unknown user”

The “Unknown user” error occurs when the specified user does not exist. If you encounter this error, verify that the user name and host name are correctly specified, and confirm that the user exists in the mysql.user table.

Kesalahan Terkait Kata Sandi

If you experience password mismatches or authentication errors, you can resolve the issue by resetting the password. Use the following command to update the password:

ALTER USER 'username'@'hostname' IDENTIFIED BY 'new_password';

6. Kesimpulan

This article explained how to retrieve a list of MySQL users, check detailed information, create and manage users, and resolve common errors. Proper user management directly contributes to strengthening database security. Regularly reviewing user accounts and privileges helps reduce the risk of unauthorized access.

Use these methods to operate MySQL securely and efficiently.