Create Roles in MySQL
Create Roles in MySQL
mysql configurationintermediate
by MYSQL
14 views
Create Roles in MySQL
1
A MySQL role is a named collection of privileges. Like user accounts, roles can have privileges granted to and revoked from them.123456789101112131415
mysql>
CREATE ROLE 'DELL_APP_RO', 'DELL_APP_RW';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
GRANT SELECT ON dell.* TO 'DELL_APP_RO';
Query OK, 0 rows affected (0.01 sec)
mysql>
GRANT SELECT, INSERT, UPDATE, DELETE ON dell.* TO 'DELL_APP_RW';
Query OK, 0 rows affected (0.01 sec)
mysql>1234567891011121314151617181920
-- Create new users
mysql>
CREATE USER 'raj_ro_user'@'localhost' IDENTIFIED BY 'Raj_ro_user1%';
Query OK, 0 rows affected (0.01 sec)
mysql>
CREATE USER 'raj_rw_user'@'localhost' IDENTIFIED BY 'Raj_rw_user1%';
Query OK, 0 rows affected (0.01 sec)
mysql>
-- Grants Roles
mysql>
GRANT DELL_APP_RO TO 'raj_ro_user'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql>
GRANT DELL_APP_RW TO 'raj_rw_user'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
mysql>
SELECT * FROM mysql.role_edges;
+-----------+-------------+-----------+-------------+-------------------+
| FROM_HOST | FROM_USER | TO_HOST | TO_USER | WITH_ADMIN_OPTION |
+-----------+-------------+-----------+-------------+-------------------+
| % | DELL_APP_RO | localhost | raj_ro_user | N |
| % | DELL_APP_RW | localhost | raj_rw_user | N |
+-----------+-------------+-----------+-------------+-------------------+
2 rows in set (0.00 sec)
mysql>
------------ OR -------------
-- TO list ALL Roles (you can notice roles also)
select * from mysql.user;
[root@rac1 ~]#
mysql -u raj_ro_user -h localhost -pRaj_ro_user1%
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
select current_user ();
+-----------------------+
| current_user () |
+-----------------------+
| raj_ro_user@localhost |
+-----------------------+
1 row in set (0.00 sec)
mysql>
SHOW GRANTS;
+------------------------------------------------------+
| Grants for raj_ro_user@localhost |
+------------------------------------------------------+
| GRANT USAGE ON *.* TO `raj_ro_user`@`localhost` |
| GRANT `DELL_APP_RO`@`%` TO `raj_ro_user`@`localhost` |
+------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
SELECT CURRENT_ROLE();
+----------------+
| CURRENT_ROLE() |
+----------------+
| NONE |
+----------------+
1 row in set (0.00 sec)
mysql>
set role DELL_APP_RO;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT CURRENT_ROLE();
+-------------------+
| CURRENT_ROLE() |
+-------------------+
| `DELL_APP_RO`@`%` |
+-------------------+
1 row in set (0.00 sec)
mysql>
SHOW GRANTS;
+-------------------------------------------------------+
| Grants for raj_ro_user@localhost |
+-------------------------------------------------------+
| GRANT USAGE ON *.* TO `raj_ro_user`@`localhost` |
| GRANT SELECT ON `dell`.* TO `raj_ro_user`@`localhost` |
| GRANT `DELL_APP_RO`@`%` TO `raj_ro_user`@`localhost` |
+-------------------------------------------------------+
3 rows in set (0.00 sec)
mysql>
############################################################
-- This command can set permanatly, no need to set manually
mysql>
select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql>
mysql>
set global activate_all_roles_on_login=1;
Query OK, 0 rows affected (0.00 sec)
mysql>
#############################################################
-- Set multiple default roles for one user
SET DEFAULT ROLE ROLE1,ROLE2,ROLE3 TO TO 'username'@'localhost';
-- A default role can be assigned to multiple users in one command as above
SET DEFAULT ROLE ROLE1 TO 'user1'@'localhost', 'user2'@'localhost', 'user3'@'localhost';1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
To set and persist mandatory_roles at runtime,use a statement like this:
-- if you create new user, then
automatically
this role will be assigned.
-- these changes are fixed ever after restart MySQL.
mysql>
CREATE ROLE MY_MANDATORY_ROLE;
Query OK, 0 rows affected (0.01 sec)
mysql>
GRANT DELL_APP_RO,DELL_APP_RW TO MY_MANDATORY_ROLE;
Query OK, 0 rows affected (0.01 sec)
mysql>
GRANT SELECT ON sys.version to MY_MANDATORY_ROLE;
Query OK, 0 rows affected (0.00 sec)
mysql>
SET PERSIST mandatory_roles = 'MY_MANDATORY_ROLE';
Query OK, 0 rows affected (0.00 sec)
mysql>
select @@mandatory_roles;
+-------------------+
| @@mandatory_roles |
+-------------------+
| MY_MANDATORY_ROLE |
+-------------------+
1 row in set (0.00 sec)
mysql>
create user lenovo@localhost identified by 'Lenovo123%';
Query OK, 0 rows affected (0.01 sec)
mysql>
[root@rac1 ~]#
mysql -u lenovo -h localhost -pLenovo123%
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
select user();
+------------------+
| user() |
+------------------+
| lenovo@localhost |
+------------------+
1 row in set (0.00 sec)
mysql>
show grants;
+-------------------------------------------------------+
| Grants for lenovo@localhost |
+-------------------------------------------------------+
| GRANT USAGE ON *.* TO `lenovo`@`localhost` |
|
GRANT `MY_MANDATORY_ROLE`@`%` TO `lenovo`@`localhost` |
+-------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mysql>
show grants for DELL_APP_RO;
+-----------------------------------------------+
| Grants for DELL_APP_RO@% |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO `DELL_APP_RO`@`%` |
|
GRANT SELECT ON `dell`.* TO `DELL_APP_RO`@`%`
|
+-----------------------------------------------+
2 rows in set (0.00 sec)
mysql>
show grants for DELL_APP_RW;
+-----------------------------------------------------------------------+
| Grants for DELL_APP_RW@% |
+-----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `DELL_APP_RW`@`%` |
|
GRANT SELECT, INSERT, UPDATE, DELETE ON `dell`.* TO `DELL_APP_RW`@`%`
|
+-----------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
mysql>
REVOKE SELECT ON dell.* FROM DELL_APP_RO;
Query OK, 0 rows affected (0.01 sec)
mysql>
REVOKE SELECT,INSERT,UPDATE,DELETE ON dell.* FROM DELL_APP_RW;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
show grants for DELL_APP_RO;
+-----------------------------------------+
| Grants for DELL_APP_RO@% |
+-----------------------------------------+
| GRANT USAGE ON *.* TO `DELL_APP_RO`@`%` |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql>
show grants for DELL_APP_RW;
+-----------------------------------------+
| Grants for DELL_APP_RW@% |
+-----------------------------------------+
| GRANT USAGE ON *.* TO `DELL_APP_RW`@`%` |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql>
SELECT * FROM mysql.role_edges;
+-----------+-------------+-----------+-------------------+-------------------+
| FROM_HOST | FROM_USER | TO_HOST | TO_USER | WITH_ADMIN_OPTION |
+-----------+-------------+-----------+-------------------+-------------------+
| % | DELL_APP_RO | % | MY_MANDATORY_ROLE | N |
| % | DELL_APP_RO | localhost | raj_ro_user | N |
| % | DELL_APP_RW | % | MY_MANDATORY_ROLE | N |
| % | DELL_APP_RW | localhost | raj_rw_user | N |
+-----------+-------------+-----------+-------------------+-------------------+
4 rows in set (0.00 sec)
mysql>
-- Revoke Roles from user
mysql>
REVOKE DELL_APP_RO FROM 'raj_ro_user'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql>
REVOKE DELL_APP_RW FROM 'raj_rw_user'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql>
-- Revoke Roles from Roles
mysql>
REVOKE DELL_APP_RO FROM 'MY_MANDATORY_ROLE';
Query OK, 0 rows affected (0.01 sec)
mysql>
REVOKE DELL_APP_RW FROM 'MY_MANDATORY_ROLE';
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
SELECT * FROM mysql.role_edges;
Empty set (0.00 sec)
mysql>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
mysql>
select user from mysql.user;
+-------------------+
| user |
+-------------------+
|
DELL_APP_RO |
| DELL_APP_RW |
| MY_MANDATORY_ROLE |
| TEST_RO |
| raj |
| scott1 |
| srinu |
| scott |
| scott |
| sugi |
| scott |
| teja |
| bose |
| lenovo |
| lori |
| may |
| mike |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| raj |
| raj_ro_user |
| raj_rw_user |
| root |
| sunil |
+-------------------+
25 rows in set (0.00 sec)
mysql>
mysql>
DROP ROLE DELL_APP_RO;
Query OK, 0 rows affected (0.00 sec)
mysql>
DROP ROLE DELL_APP_RW;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
DROP ROLE MY_MANDATORY_ROLE;
ERROR 3628 (HY000): The role `MY_MANDATORY_ROLE`@`%` is a mandatory role and can't be revoked or dropped. The restriction can be lifted by excluding the role identifier from the global variable mandatory_roles.
mysql>
mysql>
select @@mandatory_roles;
+-------------------+
| @@mandatory_roles |
+-------------------+
|
MY_MANDATORY_ROLE
|
+-------------------+
1 row in set (0.00 sec)
mysql>
mysql>
SET PERSIST mandatory_roles = '
NONE
';
Query OK, 0 rows affected (0.00 sec)
mysql>
select @@mandatory_roles;
+-------------------+
| @@mandatory_roles |
+-------------------+
|
NONE
|
+-------------------+
1 row in set (0.00 sec)
mysql>
DROP ROLE MY_MANDATORY_ROLE;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
select user from mysql.user;
+------------------+
| user |
+------------------+
| TEST_RO |
| raj |
| scott1 |
| srinu |
| scott |
| scott |
| sugi |
| scott |
| teja |
| bose |
| lenovo |
| lori |
| may |
| mike |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| raj |
| raj_ro_user |
| raj_rw_user |
| root |
| sunil |
+------------------+
22 rows in set (0.00 sec)
mysql>Please to add comments
No comments yet. Be the first to comment!