How can I get a list of user accounts using the command line in MySQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When you are managing a MySQL database, knowing how to list user accounts via the command line interface (CLI) is a fundamental skill. This capability is crucial for database administration, securing the database, and performing audits on user access and privileges. This article will guide you through the process of fetching a list of user accounts in MySQL, using command line tools and SQL commands.
Accessing MySQL Command Line
To start, you need to access the MySQL server through its command line client. You can log in to your MySQL server with the following command:
Here, -u specifies the username you want to log in with, and -p prompts for a password. Replace root with another user if necessary.
Basic SQL Query to List Users
Once you're logged into the MySQL CLI, you can execute SQL commands. To get a list of all user accounts, MySQL maintains user details in a table named user within the database mysql, which is a system database that contains system tables storing such information. You can query this table as follows:
This command lists out all the usernames and the host from which they can connect.
Understanding the Output
The output columns of this query are crucial for understanding user configuration in MySQL:
User: This is the name of the user account.Host: Indicates the host from which the user can connect to the server. Using%as a host means the user can connect from any host.
Additional Filters and Information
You might want more detailed information about each user, or you might want to filter the list to specific users or hosts. Here’s how you can enhance your SQL commands:
Getting More Details
To view more detailed information about each user, such as authentication type and privilege details, you might extend the SELECT statement:
This command would now also show you the password hash (in the authentication_string column) and whether the user has the privileges to perform SELECT and INSERT operations.
Filtering Users
If you're interested in a specific user or users from a specific host, you can add a WHERE clause to the SQL query:
This query will return only those users that can connect from localhost.
Administrative Commands
For more robust user account management, MySQL also provides administrative commands:
SHOW GRANTS
To see what privileges a specific user has, you can use:
Replace 'username'@'host' with the actual username and host. This command is very useful for auditing user privileges.
Summary Table
Here’s a quick reference table summarizing some of the key commands discussed:
| Command | Description |
SELECT User, Host FROM mysql.user; | Lists all users and the hosts they can connect from. |
SELECT User, Host, authentication_string, Select_priv, Insert_priv FROM mysql.user; | Lists detailed user information including privileges. |
SHOW GRANTS FOR 'username'@'host'; | Shows the privileges for a specific user. |
Additional Considerations
Always ensure you have the necessary permissions to view or modify user information in the MySQL database. Running these commands as the root user or another privileged account is typically required. Operational best practices include conducting regular audits of your database users and their privileges to ensure that your database remains secure.
In conclusion, using the MySQL command line to list and audit user accounts is a powerful way to manage and secure your databases. By familiarizing yourself with these commands and understanding their output, you can maintain robust access control and security posture.

