Grant/Revoke Privileges in MySQL
Grant/Revoke Privileges in MySQL
mysql configurationintermediate
by MYSQL
13 views
Grant/Revoke Privileges in MySQL
123456789101112
Global Level :
A privilege granted at this level applies to all databases on the server. Privileges granted at the global level stored in "mysql.user" table.
Database Level:
A privilege granted at this level applies to all tables in the specified database. Privileges granted at the global level stored in "mysql.db" table.
Table Level:
A privilege granted at this level applies to all columns in the specified table. Privileges granted at the global level stored in "mysql.tables_priv" table.
Column Level:
A privilege granted at this level applies to only the specified column. Privileges granted at the global level stored in "mysql.columns_priv" table.
Routine Level:
A privilege granted at this level applies to only the specified stored function procedure. Privileges granted at the global level stored in "mysql.procs_priv" table.
Proxy Level:
Grant proxy access to users12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
ALL - All privileges.
CREATE - Allows the user to use CREATE TABLE commands.
ALTER - Allows the user to use ALTER TABLE commands.
DROP - Allows the user to use DROP TABLE commands.
DELETE - Allows the user to use DELETE commands.
INSERT - Allows the user to use INSERT commands.
UPDATE - Allows the user to use UPDATE commands.
SELECT - Allows the user to use SELECT commands.
SHUTDOWN - Allows the user to use "mysqladmin shutdown".
INDEX - Allows the user to create and drop indexes.
CREATE USER - Allows the user to manage user accounts.
CREATE VIEW - Allows the user to user "CREATE VIEW" commands.
CREATE ROUTINE - Allows the user to CREATE PROCEDURE and CREATE FUNCTION
USAGE - No privileges.
[root@rac1 ~]#
mysql -u raj -pRaj_12345%
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 25
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>
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| raj@% |
+----------------+
1 row in set (0.00 sec)
mysql>
mysql>
SHOW GRANTS FOR CURRENT_USER();
+---------------------------------+
| Grants for raj@% |
+---------------------------------+
| GRANT USAGE ON *.* TO `raj`@`%` |
+---------------------------------+
1 row in set (0.00 sec)
mysql>
---------------- OR -----
mysql>
show grants;
+---------------------------------+
| Grants for raj@% |
+---------------------------------+
| GRANT USAGE ON *.* TO `raj`@`%` |
+---------------------------------+
1 row in set (0.00 sec)
mysql>12345678910111213141516171819
mysql>
select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql>
SHOW GRANTS FOR 'raj'@'%';
+---------------------------------+
| Grants for raj@% |
+---------------------------------+
| GRANT USAGE ON *.* TO `raj`@`%` |
+---------------------------------+
1 row in set (0.00 sec)
mysql>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
To grant all privileges to a user account over all databases, use the following command:
GRANT ALL PRIVILEGES ON *.* TO 'database_user'@'localhost';
mysql>
create user raj@localhost identified by 'Rajasekhar_123%';
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> SHOW GRANTS FOR 'raj'@'localhost';
+-----------------------------------------+
| Grants for raj@localhost |
+-----------------------------------------+
| GRANT USAGE ON *.* TO `raj`@`localhost` |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql>
GRANT CREATE ON *.* TO 'raj'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql>
SHOW GRANTS FOR 'raj'@'localhost';
+------------------------------------------+
| Grants for raj@localhost |
+------------------------------------------+
| GRANT CREATE ON *.* TO `raj`@`localhost` |
+------------------------------------------+
1 row in set (0.00 sec)
mysql>
[root@rac1 ~]#
mysql -u raj -h localhost -pRajasekhar_123%
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 26
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@localhost |
+----------------+
1 row in set (0.00 sec)
mysql>
SHOW GRANTS FOR CURRENT_USER();
+------------------------------------------+
| Grants for raj@localhost |
+------------------------------------------+
| GRANT CREATE ON *.* TO `raj`@`localhost` |
+------------------------------------------+
1 row in set (0.00 sec)
mysql>
show databases;
+--------------------+
| Database |
+--------------------+
| dell |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql>
use test;
Database changed
mysql>
select database ();
+-------------+
| database () |
+-------------+
| test |
+-------------+
1 row in set (0.00 sec)
mysql>
mysql>
create table test_raj (
-> trans_id smallint,
-> trans varchar(5),
-> primary key (trans_id))
-> ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql>
show tables;
+----------------+
| Tables_in_test |
+----------------+
| test_raj |
+----------------+
1 row in set (0.00 sec)
mysql>
mysql>
INSERT INTO test_raj values(1,'SUGI');
ERROR 1142 (42000): INSERT command denied to user 'raj'@'localhost' for table 'test_raj'
mysql>
mysql>
use dell; <---- Login to other database and try to create table
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql>
select database ();
+-------------+
| database () |
+-------------+
| dell |
+-------------+
1 row in set (0.00 sec)
mysql>
create table test_raj (
-> trans_id smallint,
-> trans varchar(5),
-> primary key (trans_id))
-> ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql>
show tables;
+----------------+
| Tables_in_dell |
+----------------+
| test |
| test_raj |
+----------------+
2 rows in set (0.00 sec)
mysql>
create table test1 (
-> trans_id smallint,
-> trans varchar(5),
-> primary key (trans_id))
-> ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql>
show tables;
+----------------+
| Tables_in_dell |
+----------------+
| test |
| test1 |
| test_raj |
+----------------+
3 rows in set (0.00 sec)
mysql>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
To grant all privileges to a user account over a specific database, use the following command:
GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';
mysql>
create database orcl;
Query OK, 1 row affected (0.00 sec)
mysql>
create user lori@localhost identified by 'Lori123%';
Query OK, 0 rows affected (0.01 sec)
mysql>
grant create on orcl.* to lori@localhost;
Query OK, 0 rows affected (0.01 sec)
mysql>
[root@rac1 ~]#
mysql -u lori -h localhost -pLori123%
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 27
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 () |
+-----------------+
| lori@localhost |
+-----------------+
1 row in set (0.00 sec)
mysql>
mysql>
GRANT ALL ON orcl.* to lori@localhost;
Query OK, 0 rows affected (0.01 sec)
mysql>
[root@rac1 ~]#
mysql -u lori -h localhost -pLori123%
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 27
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 () |
+-----------------+
| lori@localhost |
+-----------------+
1 row in set (0.00 sec)
mysql>
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| orcl |
+--------------------+
2 rows in set (0.00 sec)
mysql>
use orcl;
Database changed
mysql>
create table test_orcl (
-> trans_id smallint,
-> trans varchar(5),
-> primary key (trans_id))
-> ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql>
show tables;
+----------------+
| Tables_in_orcl |
+----------------+
| test_orcl |
+----------------+
1 row in set (0.00 sec)
mysql>
SHOW GRANTS FOR CURRENT_USER();
+--------------------------------------------------------+
| Grants for lori@localhost |
+--------------------------------------------------------+
| GRANT USAGE ON *.* TO `lori`@`localhost` |
| GRANT ALL PRIVILEGES ON `orcl`.* TO `lori`@`localhost` |
+--------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>1234567891011121314151617181920212223242526272829
GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.table_name TO database_user@'localhost';
mysql>
SHOW GRANTS FOR lori@localhost;
+--------------------------------------------------------+
| Grants for lori@localhost |
+--------------------------------------------------------+
| GRANT USAGE ON *.* TO `lori`@`localhost` |
| GRANT ALL PRIVILEGES ON `orcl`.* TO `lori`@`localhost` |
+--------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mysql>
GRANT SELECT, INSERT, UPDATE, DELETE ON orcl.test_orcl TO 'lori'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql>
SHOW GRANTS FOR lori@localhost;
+----------------------------------------------------------------------------------+
| Grants for lori@localhost |
+----------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `lori`@`localhost` |
| GRANT ALL PRIVILEGES ON `orcl`.* TO `lori`@`localhost` |
|
GRANT SELECT, INSERT, UPDATE, DELETE ON `orcl`.`test_orcl` TO `lori`@`localhost`
|
+----------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
mysql>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
GRANT SELECT (`trans`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
GRANT SELECT (`trans_id`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
GRANT INSERT (`trans`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
GRANT INSERT (`trans`) ON orcl.`test_orcl` to 'may'@`localhost`;
mysql>
create user may@localhost identified by 'May1234%';
Query OK, 0 rows affected (0.01 sec)
mysql>
SELECT CONCAT('GRANT SELECT (`', COLUMN_NAME, '`), SHOW VIEW ON orcl.`', TABLE_NAME, '` to ''may''@`localhost`;')
-> FROM INFORMATION_SCHEMA.COLUMNS
-> WHERE TABLE_SCHEMA = 'orcl' AND TABLE_NAME = 'test_orcl';
+------------------------------------------------------------------------------------------------------------+
| CONCAT('GRANT SELECT (`', COLUMN_NAME, '`), SHOW VIEW ON orcl.`', TABLE_NAME, '` to ''may''@`localhost`;') |
+------------------------------------------------------------------------------------------------------------+
| GRANT SELECT (`trans`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`; |
| GRANT SELECT (`trans_id`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`; |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
GRANT SELECT (`trans`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
GRANT SELECT (`trans_id`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
mysql>
GRANT SELECT (`trans`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
Query OK, 0 rows affected (0.00 sec)
mysql>
GRANT SELECT (`trans_id`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
Query OK, 0 rows affected (0.00 sec)
mysql>
show grants for 'may'@`localhost`;
+------------------------------------------------------------------------------------------+
| Grants for may@localhost |
+------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `may`@`localhost` |
|
GRANT SELECT (`trans`, `trans_id`), SHOW VIEW ON `orcl`.`test_orcl` TO `may`@`localhost`
|
+------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mysql>
select trans from orcl.test_orcl;
+-------+
| trans |
+-------+
| SUGI |
| TEJU |
| RAJ |
| SOMU |
| JANA |
+-------+
5 rows in set (0.00 sec)
mysql>
mysql>
GRANT INSERT (`trans`), SHOW VIEW ON orcl.`test_orcl` to 'may'@`localhost`;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
SHOW GRANTS FOR 'may'@`localhost`;
+------------------------------------------------------------------------------------------------------------+
| Grants for may@localhost |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `may`@`localhost` |
|
GRANT SELECT (`trans`, `trans_id`), INSERT (`trans`), SHOW VIEW ON `orcl`.`test_orcl` TO `may`@`localhost`
|
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mysql>
GRANT INSERT (`trans`) ON orcl.`test_orcl` to 'may'@`localhost`;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
SHOW GRANTS FOR 'may'@`localhost`;
+------------------------------------------------------------------------------------------------------------+
| Grants for may@localhost |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `may`@`localhost` |
|
GRANT SELECT (`trans`, `trans_id`), INSERT (`trans`), SHOW VIEW ON `orcl`.`test_orcl` TO `may`@`localhost`
|
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
mysql>
use orcl;
Database changed
mysql>
mysql>
create table new_test (
-> trans_id smallint,
-> trans varchar(5),
-> primary key (trans_id))
-> ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql>
INSERT INTO new_test values(1,'SUGI');
Query OK, 1 row affected (0.01 sec)
mysql>
INSERT INTO new_test values(2,'TEJU');
Query OK, 1 row affected (0.00 sec)
mysql>
INSERT INTO new_test values(3,'RAJ');
Query OK, 1 row affected (0.00 sec)
mysql>
INSERT INTO new_test values(4,'SOMU');
Query OK, 1 row affected (0.01 sec)
mysql>
INSERT INTO new_test values(5,'JANA');
Query OK, 1 row affected (0.00 sec)
mysql>
COMMIT;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
create user bose@localhost identified by 'Bose_1234%';
Query OK, 0 rows affected (0.01 sec)
mysql>
grant select on orcl.* to 'bose'@'localhost'
WITH GRANT OPTION
;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
SHOW GRANTS FOR 'bose'@'localhost';
+------------------------------------------------------------------+
| Grants for bose@localhost |
+------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `bose`@`localhost` |
|
GRANT SELECT ON `orcl`.* TO `bose`@`localhost` WITH
GRANT OPTION
|
+------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>123456789101112131415161718192021222324252627282930313233343536373839404142434445
-- Global Level
REVOKE ALL ON *.* FROM 'database_user'@'localhost';
REVOKE SELECT,INSERT,UPDATE,DELETE ON *.* FROM 'database_user'@'localhost';
-- Database Level
REVOKE ALL PRIVILEGES ON database_name.* FROM 'database_user'@'localhost';
REVOKE SELECT,INSERT,UPDATE,DELETE ON database_name.* FROM 'database_user'@'localhost';
-- Table Level
REVOKE ALL PRIVILEGES ON database_name.table_name FROM 'database_user'@'localhost';
REVOKE SELECT,INSERT,UPDATE,DELETE ON database_name.table_name FROM 'database_user'@'localhost';
-- Column Level
REVOKE SELECT,INSERT (`trans`) ON orcl.`test_orcl` FROM 'may'@`localhost`;
REVOKE INSERT (`trans`) ON orcl.`test_orcl` FROM 'may'@`localhost`;
-- Revoke With Grant Option
mysql>
SHOW GRANTS FOR 'bose'@'localhost';
+------------------------------------------------------------------+
| Grants for bose@localhost |
+------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `bose`@`localhost` |
|
GRANT SELECT ON `orcl`.* TO `bose`@`localhost`
WITH GRANT OPTION
|
+------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mysql>
revoke grant option on orcl.* from bose@localhost;
Query OK, 0 rows affected (0.01 sec)
mysql>
SHOW GRANTS FOR 'bose'@'localhost';
+------------------------------------------------+
| Grants for bose@localhost |
+------------------------------------------------+
| GRANT USAGE ON *.* TO `bose`@`localhost` |
|
GRANT SELECT ON `orcl`.* TO `bose`@`localhost`
|
+------------------------------------------------+
2 rows in set (0.00 sec)
mysql>Please to add comments
No comments yet. Be the first to comment!