1 User Management & Permissions
TopThe principle of least privilege: every user should have only the permissions they need and nothing more. A web app shouldn't connect with the root account. An intern shouldn't have DELETE access to production tables.
Creating Users
-- Create a user for the web application
CREATE USER 'webapp'@'%'
IDENTIFIED BY 'Str0ng_P@ssw0rd!';
-- Create a read-only analytics user
CREATE USER 'analyst'@'10.0.0.%'
IDENTIFIED BY 'An@lytics2025!';Granting Permissions
-- Web app: read/write data, but can't change structure
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'webapp'@'%';
-- Analyst: read-only access
GRANT SELECT ON mydb.* TO 'analyst'@'10.0.0.%';
-- View specific columns only (use a view for column-level control)
CREATE VIEW safe_customers AS
SELECT id, name, city FROM customers; -- no email, no phone
GRANT SELECT ON mydb.safe_customers TO 'intern'@'%';
-- Remove permissions
REVOKE DELETE ON mydb.* FROM 'webapp'@'%';
-- Apply changes
FLUSH PRIVILEGES;| Permission | Allows | Give To |
|---|---|---|
SELECT | Read data | Almost everyone |
INSERT | Add rows | App users, data entry |
UPDATE | Modify rows | App users (careful) |
DELETE | Remove rows | Admins only (usually) |
CREATE, ALTER, DROP | Change schema | DBAs only |
ALL PRIVILEGES | Everything | Never in production |
Key Takeaways
- Create separate users for each application/role — never share the root account
- GRANT only the minimum permissions needed (least privilege)
- Use views for column-level access control
- REVOKE permissions when roles change; audit regularly