Chapter 18: Users, Privileges & Security

This is the chapter that turns your MySQL database from a “free-for-all playground” into a secure, professional system! In real life, you never want everyone to have full access — imagine if every student could delete all enrollment records or change fees!

Today we’ll learn how to:

  • Create new users
  • Grant (give) and Revoke (take away) specific permissions
  • Check what privileges a user has with SHOW GRANTS
  • Change passwords safely

Let’s pretend we’re running a real coaching institute database and we need different types of users:

  • admin → full control
  • teacher → can read and update student marks/notes
  • receptionist → can only INSERT new students and read basic info
  • student → can only see their own details

1. CREATE USER – Adding New Users

Syntax:

SQL

host can be:

  • ‘localhost’ → only from the same machine
  • ‘%’ → from anywhere (dangerous — use only for trusted remote access!)
  • ‘192.168.1.%’ → from a specific IP range

Best practice: Always use strong passwords (at least 12 characters, mix of letters/numbers/symbols).

Examples:

SQL

Tip: After creating users, always flush privileges (though usually automatic):

SQL

2. GRANT / REVOKE Privileges – Giving & Taking Permissions

GRANT gives permissions. REVOKE takes them away.

Common privileges:

Privilege What it allows
ALL PRIVILEGES Everything (dangerous!)
SELECT Read data (SELECT queries)
INSERT Add new rows
UPDATE Change existing rows
DELETE Remove rows
CREATE Create tables/databases
DROP Delete tables/databases
EXECUTE Run stored procedures/functions
GRANT OPTION Can give privileges to others

Syntax:

SQL

Example 1 – Give full access to admin

SQL

Example 2 – Give teacher SELECT + UPDATE on students table

SQL

Example 3 – Give receptionist only INSERT and SELECT on students

SQL

Example 4 – Give student only SELECT on their own data (using views) First create a view for student’s own data:

SQL

Then:

SQL

Now student can only see their own info:

SQL

Revoke example – Take away UPDATE from teacher

SQL

3. SHOW GRANTS – Check What Privileges a User Has

Syntax:

SQL

Example:

SQL

Sample output:

text

For current user:

SQL

4. Changing Passwords – Safely Update Credentials

Syntax:

SQL

Example – Change admin password

SQL

Important tips for passwords:

  • Never use simple passwords like ‘123456’ or ‘admin’
  • Use password policies (MySQL 8.0+ supports validate_password plugin)
  • Change default root password immediately after installation!
  • For root:
SQL

5. Bonus: Best Security Practices (Real-World Tips)

  1. Never use root for applications — create dedicated users with minimal privileges
  2. Use ‘%’ host only when really needed — prefer specific IPs or localhost
  3. Enable validate_password plugin (for strong passwords)
SQL
  1. Use SSL for remote connections
  2. Regularly audit privileges with SHOW GRANTS
  3. Backup before major privilege changes

That’s it for Chapter 18! 🎉 You now know how to secure your database like a real DBA — only the right people can see or change the right things!

Homework for today (do it right now – very important!) Run these commands and paste the output of the final SHOW GRANTS:

SQL

Paste the two SHOW GRANTS outputs — I’ll check them!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *