Skip to content

Security and Access Control: Users, Roles, GRANT and REVOKE

Beginning of the lesson

Security and Access Control

Users, roles, GRANT and REVOKE, and the least-privilege discipline that protects production data

Big Idea: A database engine decides two things about every connection: who are you (authentication) and what are you allowed to do (authorization). Authentication is a login (a user account plus a secret). Authorization is a set of privileges - typed permissions such as SELECT, INSERT or DROP, scoped to a server, a database, a table or even a single column. GRANT hands privileges out, REVOKE takes them back, and roles bundle privileges so you manage them once and reuse them across many accounts.
By the end you will be able to:
  • create login accounts with CREATE USER and understand the 'user'@'host' identity,
  • read the privilege hierarchy - global, database, table, column and routine scope,
  • grant and revoke exactly the privileges an account needs with GRANT / REVOKE,
  • build reusable ROLE objects in MySQL 8 and assign them as default roles,
  • apply least privilege so an application - or an SQL-injection payload - can only ever touch what it was designed to touch.

1. Authentication vs authorization

These two words are often blurred, but the engine keeps them strictly separate. Authentication answers "is this really that account?" - it is the password check at connect time. Authorization answers "may this account run this statement on this object?" - it is checked on every query, against the privileges stored in the server's grant tables.

In MySQL an account is not just a name - it is a name plus a host pattern: 'user'@'host'. The same username connecting from a different host can be a different account with different rights. This lets you say "the application may connect only from the app servers, never from a laptop".

-- A user that may connect only from one private subnet
CREATE USER 'app_web'@'10.0.%' IDENTIFIED BY 'S0me-Strong-Secret';

-- A user that may connect from anywhere (%). Convenient, but a wider attack surface.
CREATE USER 'reporting'@'%' IDENTIFIED BY 'An0ther-Secret';

-- Force a password change and expire it every 90 days
ALTER USER 'app_web'@'10.0.%' PASSWORD EXPIRE INTERVAL 90 DAY;

Creating a user grants nothing except the ability to log in (and see information_schema). A brand-new account cannot read a single business row until you GRANT it something. That "deny by default" starting point is the foundation everything else builds on.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers