- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
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.
Lab 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
rootuser and run following statement to create roles for thedevdatabase: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
proddatabase: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
devdatabase to thedev_allrole:mysql> GRANT ALL ON dev.* TO 'dev_all';To grant the SELECT privilege on the
devdatabase to thedev_readrole, run this:mysql> GRANT SELECT ON dev.* TO 'dev_read';Use the following command to grant the INSERT, UPDATE, AND DELETE privileges on the
devdatabase to thedev_writerole: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
proddatabase to theprod_allrole:mysql> GRANT ALL ON prod.* TO 'prod_all';Use the following command to grant the SELECT privilege on the
devdatabase to theprod_readrole:mysql> GRANT SELECT ON prod.* TO 'prod_read';Use the following command to grant the INSERT, UPDATE, AND DELETE privileges on the
devdatabase to theprod_writerole: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_allrole to the usercorey:mysql> GRANT dev_all to 'corey'@'localhost';Assign the
dev_readrole to the userwill:mysql> GRANT dev_read to 'will'@'localhost';Assign the
dev_writeanddev_readroles 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_allrole to the userkenny:mysql> GRANT prod_all to 'kenny'@'localhost';Assign the
prod_readrole to the usermyles:mysql> GRANT prod_read to 'myles'@'localhost';Assign the
prod_writeandprod_readroles 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
coreydoes not have access to theproddatabase:mysql> select * from prod.products;Ensure that the user
coreyhas access to thedevdatabase:mysql> select * from dev.products;Ensure that the user
willhas read access to thedevdatabase:mysql> select * from dev.products;Ensure that the user
aaronhas write access to thedevdatabase:mysql> INSERT INTO dev.orders (orderID,userName,orderType,purchaseDate) VALUES (4,'mike','laptop','2018-04-08');Ensure that the user
kennydoes not have access to thedevdatabase:mysql> select * from dev.products;Ensure that the user
kennyhas access to theproddatabase:mysql> select * from prod.products;Ensure that the user
myleshas read access to theproddatabase:mysql> select * from prod.products;Ensure that the user
aaronhas write access to theproddatabase:mysql> INSERT INTO prod.orders (orderID,userName,orderType,purchaseDate) VALUES (4,'mike','laptop','2018-04-08');
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.