Assigning Privileges
Now we'll learn to assign different types of privileges to control what users can do.
Basic Privilege Management
Let's grant some basic privileges to our HR user:
-- Grant SELECT and INSERT privileges on company_db.employees table
GRANT SELECT, INSERT ON company_db.employees TO 'hr_user'@'localhost';
-- Verify the privileges
SHOW GRANTS FOR 'hr_user'@'localhost';
The GRANT command gives specific permissions (SELECT, INSERT) on specific database objects to users. The SHOW GRANTS command displays current privileges.
Expected output:
+----------------------------------------------------------------------------------------------------------------+
| Grants for hr_user@localhost |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `hr_user`@`localhost` IDENTIFIED BY PASSWORD '*27DB7FC6D35D1F5E6A4C37473D9018DD46DC2944' |
| GRANT SELECT, INSERT ON `company_db`.`employees` TO `hr_user`@`localhost` |
+----------------------------------------------------------------------------------------------------------------+
Different Privilege Levels
MariaDB supports privileges at different levels of granularity:
-- Global privileges (all databases)
GRANT CREATE USER ON *.* TO 'admin'@'localhost';
-- Database level privileges
GRANT ALL PRIVILEGES ON company_db.* TO 'hr_user'@'localhost';
-- Table level privileges
GRANT SELECT, UPDATE ON company_db.employees TO 'intern'@'localhost';
-- Column level privileges
GRANT SELECT (name, department), UPDATE (department)
ON company_db.employees TO 'intern'@'localhost';
The *.*
syntax refers to all databases and tables, while database.*
refers to all tables in a specific database. Column-level privileges allow fine-grained access control.
Revoking Privileges
To remove privileges:
-- Revoke specific privileges
REVOKE INSERT ON company_db.employees FROM 'hr_user'@'localhost';
-- Revoke ALL privileges
REVOKE ALL PRIVILEGES ON company_db.employees FROM 'intern'@'localhost';
Always verify privilege changes after revoking them using SHOW GRANTS.
MariaDB [(none)]> SHOW GRANTS FOR 'hr_user'@'localhost';
+----------------------------------------------------------------------------------------------------------------+
| Grants for hr_user@localhost |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `hr_user`@`localhost` IDENTIFIED BY PASSWORD '*27DB7FC6D35D1F5E6A4C37473D9018DD46DC2944' |
| GRANT ALL PRIVILEGES ON `company_db`.* TO `hr_user`@`localhost` |
| GRANT SELECT ON `company_db`.`employees` TO `hr_user`@`localhost` |
+----------------------------------------------------------------------------------------------------------------+
MariaDB [(none)]> SHOW GRANTS FOR 'intern'@'localhost';
+---------------------------------------------------------------------------------------------------------------+
| Grants for intern@localhost |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `intern`@`localhost` IDENTIFIED BY PASSWORD '*A381F474A9DDCAA5BC97DAB83A69431B1C8D8902' |
+---------------------------------------------------------------------------------------------------------------+