GRANT assigns specific privileges to MariaDB users on databases, tables, or columns so they can read, write, or administer data.
GRANT adds privileges to a user account, enabling actions like SELECT, INSERT, UPDATE, DELETE, or ALL on chosen objects. It stores the rules in the mysql.user and mysql.db tables, taking effect after FLUSH PRIVILEGES or an automatic refresh.
Create the user first with CREATE USER, then grant SELECT on required schemas. Use a strong password and limit the host if possible.
CREATE USER 'reporter'@'%' IDENTIFIED BY 'S3cure!';
GRANT SELECT ON ecommerce.* TO 'reporter'@'%';
Grant INSERT on individual tables instead of the whole database. Combine with SELECT if the application must read what it writes.
GRANT INSERT, SELECT ON ecommerce.Orders TO 'loader'@'10.%';
Use GRANT ALL PRIVILEGES ON db.* to cover every object, including ones created later. Add WITH GRANT OPTION if the user should delegate rights.
GRANT ALL PRIVILEGES ON analytics.* TO 'dba'@'localhost' WITH GRANT OPTION;
Follow the principle of least privilege, grant at the lowest scope, avoid global grants unless necessary, and review rights regularly.
Use REVOKE with the same privilege list and scope. Follow with FLUSH PRIVILEGES or reconnect to force re-authentication.
REVOKE INSERT ON ecommerce.Orders FROM 'loader'@'10.%';
No. MariaDB updates the privilege tables instantly. The user must reconnect to see new rights.
Run SHOW GRANTS FOR 'user'@'host'; It returns SQL statements representing existing privileges.
Yes. Wrap them in a transaction-like block by disabling autocommit and re-enabling it after all GRANTs run. If one fails, manually ROLLBACK unwanted changes.