Skip to main content
Documents
Clauses StatementsKeywordsExpressionsFunctionsIndexesPerformance Optimization

MySQL SHOW GRANTS Statements

The `SHOW GRANTS` statement in MySQL is used to display the privileges and roles granted to a MySQL user account. It is useful for reviewing user permissions to ensure they align with security and access requirements.

Usage

The `SHOW GRANTS` statement is employed to ascertain what permissions a user has been assigned within a database environment. It is particularly useful for database administrators managing user access and security.

SHOW GRANTS FOR 'username'@'host';

In this syntax, `'username'@'host'` specifies the account for which you want to view the granted permissions. Note that users authenticated via external authentication plugins might not have all effective privileges displayed.

Examples

1. Display Grants for Current User

SHOW GRANTS;

This statement shows all the privileges granted to the user who is currently logged into the MySQL session.

2. Display Grants for a Specific User

SHOW GRANTS FOR 'john'@'localhost';

This command displays all privileges and roles assigned to the user `john` at the `localhost` host, providing insight into what actions they can perform.

3. Display Grants for a User with Wildcard Host

SHOW GRANTS FOR 'admin'@'%';

This example retrieves the grants for the `admin` user across all hosts, using the wildcard `%` to specify any host.

Permissions

To execute the `SHOW GRANTS` statement, a user typically needs to have the `SELECT` privilege on the `mysql` system database or the `SHOW DATABASES` privilege. Without these, a user can only display grants for their own account.

Output Format

The output of the `SHOW GRANTS` statement lists the privileges in a SQL format, which can be useful for recreating user accounts or transferring permissions.

Tips and Best Practices

  • Regular reviews. Regularly check user privileges to ensure they are aligned with current security policies.
  • Precise host specifications. Avoid using wide-ranging wildcards; instead, specify exact hostnames to enhance security.
  • Syntax verification. Ensure correct syntax, especially for usernames with special characters.
  • User account management. Be aware of users with multiple accounts, as different privileges can impact security management.

Additional Considerations

  • Version-specific behavior. Check for any enhancements or changes in later MySQL versions that may affect how the `SHOW GRANTS` statement functions.

SQL Upskilling for Beginners

Gain the SQL skills to interact with and query your data.
Start Learning for Free