- Lab
- A Cloud Guru
Creating and Assigning Roles in MySQL
Managing access and granting privileges to individual users can be very time consuming and cumbersome. That is why MySQL provides the ability to create roles. Roles are granted privileges, then roles are assigned to users. The users inherit the privileges of the roles. In this lab, we'll create a set of roles and grant those roles specific privileges. Once the roles have been created, we'll assign them to the appropriate users, in order to grant access to databases and tables within the MySQL server.
Path Info
Table of Contents
-
Challenge
Create the Following Three Roles for Access to the `dev` Database: `dev_all`, `dev_read`, and `dev_write`
Login to the MySQL server as the
root
user and run following statement to create roles for thedev
database:mysql> CREATE ROLE 'dev_all', 'dev_read', 'dev_write';
-
Challenge
Create the Following Three Roles for Access to the `prod` Database: `prod_all`, `prod_read`, and `prod_write`
Run following statement to create roles for the
prod
database:mysql> CREATE ROLE 'prod_all', 'prod_read', 'prod_write';
-
Challenge
Grant Privileges to the `dev_all`, `dev_read`, and `dev_write` Roles According to the Information Provided in the Instructions
This command will grant all privileges on the
dev
database to thedev_all
role:mysql> GRANT ALL ON dev.* TO 'dev_all';
To grant the SELECT privilege on the
dev
database to thedev_read
role, run this:mysql> GRANT SELECT ON dev.* TO 'dev_read';
Use the following command to grant the INSERT, UPDATE, AND DELETE privileges on the
dev
database to thedev_write
role:mysql> GRANT INSERT, UPDATE, DELETE ON dev.* TO 'dev_write';
-
Challenge
Grant Privileges to the `prod_all`, `prod_read`, and `prod_write` Roles According to the Information Provided in the Instructions
Use the following command to grant all privileges on the
prod
database to theprod_all
role:mysql> GRANT ALL ON prod.* TO 'prod_all';
Use the following command to grant the SELECT privilege on the
dev
database to theprod_read
role:mysql> GRANT SELECT ON prod.* TO 'prod_read';
Use the following command to grant the INSERT, UPDATE, AND DELETE privileges on the
dev
database to theprod_write
role:mysql> GRANT INSERT, UPDATE, DELETE ON prod.* TO 'prod_write';
-
Challenge
Assign the Roles for the `dev` Database to the Appropriate Users According to the Information Provided in the Instructions
Assign the
dev_all
role to the usercorey
:mysql> GRANT dev_all to 'corey'@'localhost';
Assign the
dev_read
role to the userwill
:mysql> GRANT dev_read to 'will'@'localhost';
Assign the
dev_write
anddev_read
roles to the useraaron
:mysql> GRANT dev_write, dev_read to 'aaron'@'localhost';
Set the default roles for the users so that the granted roles are active on login:
mysql> SET DEFAULT ROLE ALL TO 'corey'@'localhost', 'will'@'localhost', 'aaron'@'localhost';
-
Challenge
Assign the Roles for the `prod` Database to the Appropriate Users According to the Information Provided in the Instructions
Assign the
prod_all
role to the userkenny
:mysql> GRANT prod_all to 'kenny'@'localhost';
Assign the
prod_read
role to the usermyles
:mysql> GRANT prod_read to 'myles'@'localhost';
Assign the
prod_write
andprod_read
roles to the usermike
:mysql> GRANT prod_write, prod_read to 'mike'@'localhost';
Set the default roles for the users so that the granted roles are active on login:
mysql> SET DEFAULT ROLE ALL TO 'kenny'@'localhost', 'myles'@'localhost', 'mike'@'localhost';
-
Challenge
Optionally, Validate the Newly Assigned Roles by Testing User Access
Ensure that the user
corey
does not have access to theprod
database:mysql> select * from prod.products;
Ensure that the user
corey
has access to thedev
database:mysql> select * from dev.products;
Ensure that the user
will
has read access to thedev
database:mysql> select * from dev.products;
Ensure that the user
aaron
has write access to thedev
database:mysql> INSERT INTO dev.orders (orderID,userName,orderType,purchaseDate) VALUES (4,'mike','laptop','2018-04-08');
Ensure that the user
kenny
does not have access to thedev
database:mysql> select * from dev.products;
Ensure that the user
kenny
has access to theprod
database:mysql> select * from prod.products;
Ensure that the user
myles
has read access to theprod
database:mysql> select * from prod.products;
Ensure that the user
aaron
has write access to theprod
database:mysql> INSERT INTO prod.orders (orderID,userName,orderType,purchaseDate) VALUES (4,'mike','laptop','2018-04-08');
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.