</>
Skip to content
MySQL lessons (47/48)

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

PrivilegeDescription
SELECTRead data
INSERTAdd rows
UPDATEModify rows
DELETERemove rows
CREATECreate databases/tables
DROPDrop databases/tables
ALLEverything

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

PatternMatches
localhostSame machine only
%Any host
192.168.1.%Subnet range
%.example.comDomain pattern

Mini Practice

Write SQL code that:

  1. Creates a new user
  2. Grants SELECT and INSERT privileges
  3. Shows the user's grants
  4. 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.