Explain Codes LogoExplain Codes Logo

Mysql - How to grant read only permissions to a user?

sql
grant-permissions
mysql-privileges
database-security
Anton ShumikhinbyAnton Shumikhin·Dec 21, 2024
TLDR

For a swift swim in the sea of MySQL, use this lifeboat:

GRANT SELECT ON db.* TO 'user'@'host' IDENTIFIED BY 'pass';

Just replace db with your database name, user with the username, host the user's host (e.g., localhost), and 'pass' with their password. Voila! The user can now only read from all tables in db, maintaining data integrity in its purest form.

Extended read-only permissions

While SELECT is like your morning espresso - the main substance behind read-only permissions, sometimes, it needs companions like a nice Danish, namely SHOW VIEW, PROCESS, or REPLICATION CLIENT. These come handy when your user wants to view the content of database, monitor database processes, or perform replication tasks.

Verifying the granted privileges

Granting privileges is like making a margarita. You need to verify that you mixed the right ingredients. Replace the tasting part with a SHOW GRANTS command. This ensures your margarita...sorry, grant privileges taste as expected.

SHOW GRANTS FOR 'user'@'host'; --Viewer discretion advised! :)

This check acts like a mirror reflecting any extra privilege monsters disguised amongst the benign privileges.

Security 101: Keep privileges on a leash

As Spiderman's Uncle Ben once said, "With great power, comes great responsibility". It's true for MySQL as well! Don't use the sweeping ALL PRIVILEGES for a user who should drive on the read-only lane. It might unknowingly arm them to meddle with your data. Abide by the Minimum Privilege Principle.

Diving deeper: Hostname in grant permissions

Cinderella wouldn't have had a prince if the shoe didn't fit exactly on her foot. Likewise, specifying the host in GRANT statement is crucial to give the permissions for a slipper-perfect fit. To go on a generic date, use % as a wildcard:

GRANT SELECT ON db.* TO 'user'@'%'; --be warned, user can read data wearing pajamas from a holiday in Hawaii!

This gives read-only access to all, but for charm, and some security, specify the one host or an IP address.

Need for speed: Stored procedures

A bit more advanced, but worth it: summon the stored procedures to the rescue. Bundle the GRANT statement inside a stored procedure and run it like a marathon under the baton of DEFINER user. They turn into a knight-in-shining-armor, granting temporary privileges when duty calls.

Compatibility complexities & granular approach

Does your MySQL version play nice with your GRANT syntax? Keep an eye on that. Spray on a granular approach when you want to grant read-only access to specific tables or columns within a database, because sometimes, a general disinfectant just doesn't cut the mustard!

Creating secure users

Always pair IDENTIFIED BY 'password' with your GRANT clause. Trust me, you don't want your data armor having rust holes. So, use a strong password, and sleep tight!

Flushing and updating privileges

Once you sprinkle the permissions dust, clean-up using FLUSH PRIVILEGES;. This tidies up the permission assignment and ensures your changes take effect pronto!