Advanced MySQL Lesson 11 of 13
MySQL Users, Privileges and Security
Create MySQL user accounts, grant only the privileges an app needs, revoke access, change passwords, and follow basic database security practices.
Using the all-powerful root account for everything is like giving every student the principal's master key. MySQL lets you create separate users, each with only the privileges they need.
Creating a user
CREATE USER 'school_app'@'localhost' IDENTIFIED BY 'Str0ng-Pa55word!2026';
'school_app'is the username.'localhost'means this user can only connect from the same machine (the web server).'%'would allow any host; avoid it unless needed.- Use a long, random password, and keep it out of public code repositories.
Granting privileges
-- The app can read and change data in the school database, nothing more
GRANT SELECT, INSERT, UPDATE, DELETE ON school.* TO 'school_app'@'localhost';
-- A report user that can only read two tables
CREATE USER 'reports'@'localhost' IDENTIFIED BY 'An0ther-Str0ng-One!';
GRANT SELECT ON school.students TO 'reports'@'localhost';
GRANT SELECT ON school.grades TO 'reports'@'localhost';
| Privilege | Allows |
|---|---|
SELECT | Reading data |
INSERT, UPDATE, DELETE | Changing data |
CREATE, ALTER, DROP | Changing table structure |
EXECUTE | Running stored procedures |
ALL PRIVILEGES | Everything on the given scope |
school.* means every table in the school database; *.* means every database (avoid for app users).
Checking and removing access
SHOW GRANTS FOR 'school_app'@'localhost';
REVOKE DELETE ON school.* FROM 'school_app'@'localhost';
ALTER USER 'school_app'@'localhost' IDENTIFIED BY 'N3w-Pa55word!';
DROP USER 'reports'@'localhost';
Output
+-------------------------------------------------------------------------------+ | Grants for school_app@localhost | +-------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO `school_app`@`localhost` IDENTIFIED BY PASSWORD '*...' | | GRANT SELECT, INSERT, UPDATE, DELETE ON `school`.* TO `school_app`@`localhost`| +-------------------------------------------------------------------------------+
USAGE means "can log in, no special privileges".
The principle of least privilege
Give each user the minimum access needed. If the website is hacked through a bug, an attacker using school_app can't drop tables or read other databases, which limits the damage.
Security checklist
- Set a strong root password, and never use root in your app's code.
- Create one user per app with only the needed privileges.
- Use prepared statements in your code to prevent SQL injection (see the PHP course).
- Store passwords of your app's users as hashes (
password_hashin PHP), never plain text. - Don't expose port 3306 to the internet; let only your web server connect.
- Back up regularly (next lesson) and keep MySQL updated.
- Never commit database passwords to GitHub; use configuration files outside the web folder or environment variables.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04