Вы находитесь на странице: 1из 12

04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

ADVANCED

STORED PROCEDURES

TRIGGERS

VIEWS

INDEXES

MYSQL ADMINISTRATION

HOME START HERE BASICS TIPS

FUNCTIONS

AGGREGATE FUNCTIONS API

COMPARISON FUNCTIONS PHP

DATE FUNCTIONS PYTHON

STRING FUNCTIONS PERL

WINDOW FUNCTIONS NODE.JS

MATH FUNCTIONS JDBC TRYIT

Home / MySQL Administration / MySQL SHOW GRANTS

MySQL SHOW GRANTS


ADVERTISEMENT

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 1/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

Summary: in this tutorial, you will learn how to use the MySQL SHOW GRANTS statement to display the
privileges and roles assigned to an account user.

Introduction to MySQL SHOW GRANTS statement

The MySQL SHOW GRANTS statement returns all privileges and roles granted to an account user or role.

Here is the basic syntax of the SHOW GRANTS statement:


SHOW GRANTS
[FOR {user | role}
[USING role [, role] ...]]

In this syntax:

First, specify the name of the user account or role that you want to display the privileges that are
previously granted to the user account or role after the FOR keyword. If you skip the FOR clause,
the SHOW GRANTS returns the privileges of the current user.

Second, use the USING clause to examine the privileges associated with roles for the user. The
roles that you specify in the USING clause must previously granted to the user.

To execute the SHOW GRANTS statement, you need to have SELECT privilege for the mysql system
database, except to show privileges and roles for the current user.

MySQL SHOW GRANTS statement examples

Let’s take some examples of using the MySQL SHOW GRANTS statement.

A) Using MySQL SHOW GRANTS to display the privileges granted for the current
user
The following statement uses the SHOW GRANTS statement to display the privileges granted for the
current user:


SHOW GRANTS;

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 2/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

It is equivalent to the following statement:


SHOW GRANTS FOR CURRENT_USER;

and


SHOW GRANTS FOR CURRENT_USER();

Both CURRENT_USER and CURRENT_USER() return the current user.

B) Using MySQL SHOW GRANTS to display the privileges granted for a user

First, create a new database named vehicles :


CREATE DATABASE vehicles;

Second, select the database vehicles :


USE vehicles;

Third, create a new table called cars in the vehicles database:


CREATE TABLE cars (
id INT AUTO_INCREMENT,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);

Fourth, create a new user called musk@localhost :


CREATE USER musk@localhost
IDENTIFIED BY 'Super1Pass!';

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 3/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

Fifth, show the default privileges granted to the user musk@localhost :


SHOW GRANTS
FOR musk@localhost;

The GRANT USAGE is the synonym of no privilege. By default, when a new user created, it has no
privilege.

Sixth, grant all privileges on the vehicles database to the user musk@localhost :


GRANT ALL
ON vehicles.*
TO musk@localhost;

Finally, show the privileges granted for the user musk@localhost :

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 4/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples


SHOW GRANTS
FOR musk@localhost;

C) Using MySQL SHOW GRANTS to display the privileges granted for a role

First, create a new role called writer@localhost :


CREATE ROLE writer@localhost;

Second, show privileges granted for the role writer@localhost :


SHOW GRANTS
FOR writer@localhost;

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 5/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

Third, grant SELECT , INSERT , UPDATE , and DELETE privileges on the vehicles database to the
writer@localhost :


GRANT
SELECT,
INSERT,
UPDATE,
DELETE
ON vehicles.*
TO writer@localhost;

Fourth, show privileges granted for the role writer@localhost :


SHOW GRANTS
FOR writer@localhost;

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 6/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

D) Using MySQL SHOW GRANTS with USING clause example

First, create a new account user called jame@localhost :


CREATE USER jame@localhost
IDENTIFIED BY 'Secret@Pass1';

Second, grant the EXECUTE privilege to the user jame@localhost :


GRANT EXECUTE
ON vehicles.*
TO jame@localhost;

Third, grant the role writer@localhost to the user jame@localhost :


GRANT writer@localhost
TO jame@localhost;

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 7/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

Fourth, display the privileges granted for the user jame@localhost :


SHOW GRANTS
FOR jame@localhost;

Finally, use the USING clause in the SHOW GRANTS statement to display privileges associated with the
writer@localhost role:


SHOW GRANTS
FOR jame@localhost
USING writer@localhost;

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 8/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

In this tutorial, you have learned how to use the MySQL SHOW GRANTS statement to display privileges
granted for an account user or role.

Was this tutorial helpful?

 Yes  No

ADVERTISEMENT

 Previous
MySQL GRANT
Next
MySQL REVOKE 

Search this website

What Is MySQL?

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 9/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

Install MySQL Database Server

Connect to MySQL Server

Download MySQL Sample Database

Load Sample Database

ADVERTISEMENT

START & STOP MYSQL

Start MySQL Server

Restart MySQL Server

Stop MySQL Server

USER MANAGEMENTS

Create Users

Grant Privileges

Revoke Privileges

Manage Roles

Show Privileges

Drop Users

Change Passwords

Lock User Accounts

Unlock User Accounts

ADVERTISEMENT

BACKUP & RESTORE

MySQL Backup

MySQL Restore

DATABASE MAINTENANCE

Maintain Database Tables

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 10/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

SHOW COMMANDS

Show Databases

Show Tables

Show Columns

Show ProcessList

ADVERTISEMENT

MYSQL PROGRAMMING INTERFACES RECENT MYSQL TUTORIALS

PHP MySQL Tutorial Advanced MySQL

Node.js MySQL Tutorial MySQL Application Programming Interfaces

Python MySQL Tutorial MySQL Stored Object Access Control

Perl MySQL Tutorial Install MySQL on Ubuntu

MySQL JDBC Tutorial Install MySQL CentOS

Getting Started with MySQL


OTHERS
Connect to MySQL Server
MySQL Cheat Sheet
How To Lock User Accounts in MySQL
MySQL Resources
How To Unlock User Accounts in MySQL
MySQL Books Server

Restart MySQL Server

ABOUT MYSQL TUTORIAL WEBSITE

MySQLTutorial.org is a website dedicated to


MySQL database. We regularly publish useful
MySQL tutorials to help web developers and
database administrators learn MySQL faster
and more effectively.

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 11/12
04/08/2020 MySQL SHOW GRANTS Explained By Practical Examples

All MySQL tutorials are practical and easy-to-


follow, with SQL script and screenshots
available. More About Us

SITE LINKS

About Us

Contact Us

Request a Tutorial

Privacy Policy

Donations

Copyright © 2020 by www.mysqltutorial.org. All Rights Reserved.

https://www.mysqltutorial.org/mysql-adminsitration/mysql-show-grants/ 12/12

Вам также может понравиться