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.
Introduction
Listing MySQL user accounts from the command line is a standard administrative task for auditing access and troubleshooting authentication issues. The exact query depends on MySQL version and your privileges. A secure workflow should enumerate accounts, inspect grants, and avoid exposing sensitive metadata unnecessarily.
Connect with an Account That Can Inspect Users
To query user metadata, connect with an administrative account or one that has permission to read user-related system tables.
Once connected, confirm server version first because privilege metadata layout differs between major versions.
Knowing the version helps you choose the safest and most portable query path.
List Accounts from the MySQL System Schema
For many versions, account identity is represented by user and host pair. This pair is the real principal key in MySQL authentication.
Typical output includes entries such as:
rootatlocalhost- application users bound to specific subnets
- service accounts limited to loopback
Always interpret User with Host. The username alone is not unique in MySQL account management.
Use SHOW GRANTS for Effective Permissions
Enumerating accounts is only step one. You usually need effective privilege review.
This command reveals role grants and privilege scopes that may not be obvious from account listing alone.
For broad audits, generate statements dynamically:
Run generated commands in a controlled environment and store results in audit records.
MySQL 8 and Privilege Metadata Notes
MySQL 8 introduced role features and additional metadata tables. While mysql.user remains common for basic listing, role-based access design means you should evaluate both direct grants and role assignments.
Useful role inspection pattern:
This helps map who inherits which role-based privileges.
Command-Line Friendly Reporting
If you need machine-readable output for scripts or audits, use batch mode options.
This avoids formatted table borders and simplifies integration with shell tooling.
For CSV-like output:
Keep generated files protected because account names and host patterns are sensitive operational data.
Security and Operational Practices
Account enumeration is useful, but it should be handled with least-privilege and audit discipline.
Recommended practices:
- restrict who can read account metadata
- rotate credentials for script-based admin jobs
- review wildcard hosts such as
%regularly - track dormant accounts and remove unused principals
- include account review in change-management process
These steps reduce attack surface and prevent privilege drift over time.
Troubleshooting Access Errors
If SELECT from mysql.user fails with permission error:
- verify current account identity
- check
SHOW GRANTSfor current account - escalate with a dedicated audit role rather than full superuser when possible
Also confirm you are querying the intended server. In multi-environment setups, administrators often connect to the wrong host and misread account state.
Common Pitfalls
- Treating username alone as unique account identity and ignoring host component.
- Running audit queries from over-privileged accounts without access controls.
- Assuming account list equals effective permissions without checking grants and roles.
- Exporting account metadata to unsecured files or shared locations.
- Using wildcard host entries broadly and forgetting to tighten them later.
Summary
- List MySQL accounts with
UserandHosttogether for accurate identity context. - Use
SHOW GRANTSand role metadata to understand actual privileges. - Adjust audit queries and expectations based on MySQL version.
- Prefer machine-readable command-line output for repeatable reviews.
- Combine enumeration with least-privilege and lifecycle governance for secure operations.

