MySQL — Users
Create User
CREATE USER 'john'@'localhost' IDENTIFIED BY 'password123';
Grant Privileges
-- All privileges on a database
GRANT ALL PRIVILEGES ON mydb.* TO 'john'@'localhost';
-- Specific privileges
GRANT SELECT, INSERT ON mydb.customers TO 'john'@'localhost';
-- Read-only access
GRANT SELECT ON mydb.* TO 'reader'@'%';
View Privileges
SHOW GRANTS FOR 'john'@'localhost';
Revoke Privileges
REVOKE INSERT ON mydb.* FROM 'john'@'localhost';
Privilege Types
| Privilege | Description |
|---|---|
| SELECT | Read data |
| INSERT | Add rows |
| UPDATE | Modify rows |
| DELETE | Remove rows |
| CREATE | Create databases/tables |
| DROP | Drop databases/tables |
| ALL | Everything |
Drop User
DROP USER 'john'@'localhost';
Change Password
ALTER USER 'john'@'localhost' IDENTIFIED BY 'new_password';
Password Expiration
ALTER USER 'john'@'localhost' PASSWORD EXPIRE;
Public Key Authentication
CREATE USER 'secure_user'@'%'
IDENTIFIED WITH caching_sha2_password
BY 'strong_password';
Host Patterns
| Pattern | Matches |
|---|---|
localhost | Same machine only |
% | Any host |
192.168.1.% | Subnet range |
%.example.com | Domain pattern |
Mini Practice
Write SQL code that:
- Creates a new user
- Grants SELECT and INSERT privileges
- Shows the user's grants
- Revokes a privilege and drops the user
Up Next
Continue with Backup — protecting your data with backups.
Related Topics
Frequently Asked Questions about Users
What is Users in MySQL?
Users is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Users?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Users.
Why is Users important in MySQL?
Users is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.