Version sans cache

Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

Rechercher dans le manuel MySQL

13.7.6.21 SHOW GRANTS Syntax

  1. SHOW GRANTS
  2.     [FOR user_or_role
  3.         [USING role [, role] ...]]
  4.  
  5. user_or_role: {
  6.     user
  7.   | role
  8. }

This statement displays the privileges and roles that are assigned to a MySQL user account or role, in the form of GRANT statements that must be executed to duplicate the privilege and role assignments.

Note

To display nonprivilege information for MySQL accounts, use the SHOW CREATE USER statement. See Section 13.7.6.12, “SHOW CREATE USER Syntax”.

SHOW GRANTS requires the SELECT privilege for the mysql system database, except to display privileges and roles for the current user.

To name the account or role for SHOW GRANTS, use the same format as for the GRANT statement; for example, 'jeffrey'@'localhost':

  1. mysql> SHOW GRANTS FOR 'jeffrey'@'localhost';
  2. +------------------------------------------------------------------+
  3. | Grants for jeffrey@localhost                                     |
  4. +------------------------------------------------------------------+
  5. | GRANT USAGE ON *.* TO `jeffrey`@`localhost`                      |
  6. | GRANT SELECT, INSERT, UPDATE ON `db1`.* TO `jeffrey`@`localhost` |
  7. +------------------------------------------------------------------+

The host part, if omitted, defaults to '%'. For additional information about specifying account and role names, see Section 6.2.4, “Specifying Account Names”, and Section 6.2.5, “Specifying Role Names”.

To display the privileges granted to the current user (the account you are using to connect to the server), you can use any of the following statements:

  1. SHOW GRANTS;
  2. SHOW GRANTS FOR CURRENT_USER;
  3. SHOW GRANTS FOR CURRENT_USER();

If SHOW GRANTS FOR CURRENT_USER (or any of the equivalent syntaxes) is used in definer context, such as within a stored procedure that executes with definer rather than invoker privileges, the grants displayed are those of the definer and not the invoker.

In MySQL 8.0 compared to previous series, SHOW GRANTS no longer displays ALL PRIVILEGES in its global-privileges output because the meaning of ALL PRIVILEGES at the global level varies depending on which dynamic privileges are defined. Instead, SHOW GRANTS explictly lists each granted global privilege:

  1. mysql> SHOW GRANTS FOR 'root'@'localhost';
  2. +---------------------------------------------------------------------+
  3. | Grants for root@localhost                                           |
  4. +---------------------------------------------------------------------+
  5. | GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD,         |
  6. | SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES,  |
  7. | SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION   |
  8. | SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE,  |
  9. | CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT      |
  10. | OPTION                                                              |
  11. | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
  12. +---------------------------------------------------------------------+

Applications that process SHOW GRANTS output should be adjusted accordingly.

At the global level, GRANT OPTION applies to all granted static global privileges if granted for any of them, but applies individually to granted dynamic privileges. SHOW GRANTS displays global privileges this way:

  • One line listing all granted static privileges, if there are any, including WITH GRANT OPTION if appropriate.

  • One line listing all granted dynamic privileges for which GRANT OPTION is granted, if there are any, including WITH GRANT OPTION.

  • One line listing all granted dynamic privileges for which GRANT OPTION is not granted, if there are any, without WITH GRANT OPTION.

With the optional USING clause, SHOW GRANTS enables you to examine the privileges associated with roles for the user. Each role named in the USING clause must be granted to the user.

Suppose that user u1 is assigned roles r1 and r2, as follows:

  1. CREATE ROLE 'r1', 'r2';
  2. GRANT SELECT ON db1.* TO 'r1';
  3. GRANT INSERT, UPDATE, DELETE ON db1.* TO 'r2';
  4. CREATE USER 'u1'@'localhost' IDENTIFIED BY 'u1pass';
  5. GRANT 'r1', 'r2' TO 'u1'@'localhost';

SHOW GRANTS without USING shows the granted roles:

  1. mysql> SHOW GRANTS FOR 'u1'@'localhost';
  2. +---------------------------------------------+
  3. | Grants for u1@localhost                     |
  4. +---------------------------------------------+
  5. | GRANT USAGE ON *.* TO `u1`@`localhost`      |
  6. | GRANT `r1`@`%`,`r2`@`%` TO `u1`@`localhost` |
  7. +---------------------------------------------+

Adding a USING clause causes the statement to also display the privileges associated with each role named in the clause:

  1. mysql> SHOW GRANTS FOR 'u1'@'localhost' USING 'r1';
  2. +---------------------------------------------+
  3. | Grants for u1@localhost                     |
  4. +---------------------------------------------+
  5. | GRANT USAGE ON *.* TO `u1`@`localhost`      |
  6. | GRANT SELECT ON `db1`.* TO `u1`@`localhost` |
  7. | GRANT `r1`@`%`,`r2`@`%` TO `u1`@`localhost` |
  8. +---------------------------------------------+
  9. mysql> SHOW GRANTS FOR 'u1'@'localhost' USING 'r2';
  10. +-------------------------------------------------------------+
  11. | Grants for u1@localhost                                     |
  12. +-------------------------------------------------------------+
  13. | GRANT USAGE ON *.* TO `u1`@`localhost`                      |
  14. | GRANT INSERT, UPDATE, DELETE ON `db1`.* TO `u1`@`localhost` |
  15. | GRANT `r1`@`%`,`r2`@`%` TO `u1`@`localhost`                 |
  16. +-------------------------------------------------------------+
  17. mysql> SHOW GRANTS FOR 'u1'@'localhost' USING 'r1', 'r2';
  18. +---------------------------------------------------------------------+
  19. | Grants for u1@localhost                                             |
  20. +---------------------------------------------------------------------+
  21. | GRANT USAGE ON *.* TO `u1`@`localhost`                              |
  22. | GRANT SELECT, INSERT, UPDATE, DELETE ON `db1`.* TO `u1`@`localhost` |
  23. | GRANT `r1`@`%`,`r2`@`%` TO `u1`@`localhost`                         |
  24. +---------------------------------------------------------------------+
Note

A privilege granted to an account is always in effect, but a role is not. The active roles for an account can differ across and within sessions, depending on the value of the activate_all_roles_on_login system variable, the account default roles, and whether SET ROLE has been executed within a session.

SHOW GRANTS does not display privileges that are available to the named account but are granted to a different account. For example, if an anonymous account exists, the named account might be able to use its privileges, but SHOW GRANTS does not display them.

SHOW GRANTS displays mandatory roles named in the mandatory_roles system variable value as follows:

  • SHOW GRANTS without a FOR clause displays privileges for the current user, and includes mandatory roles.

  • SHOW GRANTS FOR user displays privileges for the named user, and does not include mandatory roles.

This behavior is for the benefit of applications that use the output of SHOW GRANTS FOR user to determine which privileges are granted explicitly to the named user. Were that output to include mandatory roles, it would be difficult to distinguish roles granted explicitly to the user from mandatory roles.

For the current user, applications can determine privileges with or without mandatory roles by using SHOW GRANTS or SHOW GRANTS FOR CURRENT_USER, respectively.


Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-show-grants.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut