User Rights Management
Creating MySQL Users
Creating and managing user rights is a fundamental aspect of MySQL security. Here's how to create and manage users:
## Login to MySQL as root
mysql -u root -p
## Create a new user
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
User Privilege Assignment
Granting Specific Privileges
graph TD
A[Privilege Granting] --> B[Database Level]
A --> C[Table Level]
A --> D[Column Level]
Privilege Grant Examples
## Grant all privileges on a specific database
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';
## Grant specific privileges
GRANT SELECT, INSERT ON database_name.table_name TO 'newuser'@'localhost';
## Grant specific column privileges
GRANT SELECT(column1, column2) ON database_name.table_name TO 'newuser'@'localhost';
Privilege Management Matrix
Operation |
Command |
Description |
Grant Privileges |
GRANT |
Assign specific rights to a user |
Revoke Privileges |
REVOKE |
Remove previously granted privileges |
Show User Privileges |
SHOW GRANTS |
Display current user permissions |
User Authentication Methods
graph TD
A[MySQL Authentication] --> B[Native Authentication]
A --> C[SHA256 Authentication]
A --> D[LDAP Authentication]
Advanced User Management
Creating Users with Specific Host Access
## Create user with specific host access
CREATE USER 'developer'@'192.168.1.%' IDENTIFIED BY 'secure_password';
## Grant limited database access
GRANT SELECT, INSERT ON project_db.* TO 'developer'@'192.168.1.%';
Password Management
## Change user password
ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';
## Set password expiration
ALTER USER 'username'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;
Security Best Practices
- Use strong, unique passwords
- Limit global privileges
- Use specific host restrictions
- Regularly audit user rights
LabEx Tip
LabEx provides a safe environment to practice complex user rights management scenarios without risking production systems.
Revoking Privileges
## Revoke specific privileges
REVOKE INSERT ON database_name.table_name FROM 'username'@'localhost';
## Revoke all privileges
REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'localhost';