MySQL: MySQL User Management and Permission Control

Last updated: 2026-08-26

Access control is the foundation of database security—different users can only access the data they are authorized to access.

This lesson covers user and permission management.

100%
graph TB
    A[MySQL Permission Levels] --> B[Global Permissions<br/>*.*]
    A --> C[Database Permissions<br/>mydb.*]
    A --> D[Table Permissions<br/>mydb.users]
    A --> E[Column Permissions<br/>mydb.users col]
    B --> B1[ALL/CREATE/RELOAD...]
    C --> C1[SELECT/INSERT/UPDATE/DELETE]
    D --> D1[SELECT/ALTER/INDEX...]
    E --> E1[SELECT col1, col2]

1. What You'll Learn



2. A True Story

(1) Pain Point: A Mistake with the Root Account Leads to a Major Disaster

The development team had always been connecting to the production database using the root account—it was convenient, but then one day, a developer accidentally executed DROP TABLE users, and the data for 3 million users vanished in an instant. It took four hours to restore the backup, during which time the service was completely down, resulting in direct losses of over one million.

(2) Solutions for the Permissions System

Apply the principle of least privilege: Grant each role only the necessary permissions. Developers are granted read-only access plus read/write access to development repositories; operations staff have management permissions for specific repositories; and root access is restricted to DBAs using the bastion host.

Dimension Always use root Principle of Least Privilege
Risk of Accidental Operations Extremely High (Can DROP any table) Extremely Low (Restricted permissions)
Disaster Recovery Difficult (Wide-ranging impact) Easy (Limited impact)
Audit Trail Cannot Identify Operator Trace by User
Security Level


3. User Management

▶ Example: Creating a User

SQL
-- Create a Local User
CREATE USER 'dev_user'@'localhost' IDENTIFIED BY 'DevPass123!';

-- Create a user who is allowed to connect remotely
CREATE USER 'remote_user'@'%' IDENTIFIED BY 'RemotePass123!';

-- Create a user allowed to connect from a specific IP
CREATE USER 'app_user'@'192.168.1.%' IDENTIFIED BY 'AppPass123!';
▶ Try it Yourself

Output:

TEXT 📖 Display only
Query OK, 0 rows affected

Query OK, 0 rows affected

--------+----------
user    | host     
--------+----------
value_1 | localhost
value_2 | localhost
value_3 | localhost
--------+----------
3 rows in set

▶ Example: Modifying and Deleting Users

SQL
-- Change Password
ALTER USER 'dev_user'@'localhost' IDENTIFIED BY 'NewPass123!';

-- Delete User
DROP USER 'dev_user'@'localhost';

-- View All Users
SELECT user, host FROM mysql.user;
▶ Try it Yourself

Output:

TEXT 📖 Display only
Output displayed


4. GRANT Authorization

▶ Example: Permissions Management

Output:

TEXT 📖 Display only
Query OK, 0 rows affected

Query OK, 0 rows affected

Query OK, 0 rows affected

Query OK, 0 rows affected

Query OK, 0 rows affected
SQL
-- Grant All Privileges (Administrator)
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';

-- Grant Permissions to a Specific Database
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'dev_user'@'localhost';

-- Grant Permissions on a Specific Table
GRANT SELECT ON mydb.users TO 'readonly'@'localhost';

-- Grant Permissions on Specific Columns
GRANT SELECT (username, email) ON mydb.users TO 'limited'@'localhost';

-- Refresh Permissions
FLUSH PRIVILEGES;

Output:

TEXT 📖 Display only
Output displayed


5. Permission Levels

Level Syntax Description
Global *.* All tables in all databases
Database mydb.* All tables in a specific database
Table mydb.users Specific Table
Column mydb.users(email) Specific Column


6. REVOKE: Revoke Permissions

SQL
-- Revoke All Permissions
REVOKE ALL PRIVILEGES ON *.* FROM 'dev_user'@'localhost';

-- Revoke Specific Permissions
REVOKE INSERT, DELETE ON mydb.* FROM 'dev_user'@'localhost';

FLUSH PRIVILEGES;


7. View Permissions

SQL
-- View Current User Permissions
SHOW GRANTS;

-- View a Specific User's Permissions
SHOW GRANTS FOR 'dev_user'@'localhost';


8. User Management (MySQL 8.0+)

SQL
-- Create a role
CREATE ROLE 'app_read', 'app_write';

-- Grant privileges to roles
GRANT SELECT ON mydb.* TO 'app_read';
GRANT INSERT, UPDATE, DELETE ON mydb.* TO 'app_write';

-- Assign Roles to Users
GRANT 'app_read', 'app_write' TO 'dev_user'@'localhost';

-- Activate role
SET DEFAULT ROLE ALL TO 'dev_user'@'localhost';

❓ FAQ

Q What is the difference between 'user'@'localhost' and 'user'@'%'?
A "localhost" allows only local connections, while "%" allows connections from any host.
Q Is a FLUSH PRIVILEGES statement required after GRANT?
A It is not required when using GRANT directly. It is only required when modifying the mysql.user table directly.
Q What should I do if I forget my root password?
A Boot the system bypassing the permission verification, change the password, and then reboot.
Q What is WITH GRANT OPTION?
A It allows a user to grant their own privileges to others. Granting this privilege should be done with caution to prevent the proliferation of privileges.
Q What is the difference between a role and a user?
A A role is a set of permissions; it cannot be used to log in directly. After a user logs in, they use the SET ROLE command to activate a role.

📖 Summary


📝 Exercises

  1. Basic Question (Difficulty: ⭐): Create a read-only user who can only query the mydb database.

  2. Advanced Exercise (Difficulty: ⭐⭐): Create a role and assign it to a user.

  3. Challenge Question (Difficulty: ⭐⭐⭐): Design a permissions scheme where developers have read and write access, operations staff have read-only access, and administrators have full access.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏