MySQL Users, Grants, and Privileges Cheatsheet

create users, alter passwords, grant privileges, show grants, revoke access, and manage MySQL accounts safely.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## user management
Create a user
CREATE USER 'app_user'@'%' IDENTIFIED BY 'replace-with-strong-password';

# Create an account identified by user name and host.

Change a user's password
ALTER USER 'app_user'@'%' IDENTIFIED BY 'new-strong-password';

# Rotate credentials for an existing account.

Drop a user
DROP USER IF EXISTS 'old_user'@'%';

# Remove an account from the server.

Show the current and authenticated user
SELECT USER(), CURRENT_USER();

# Inspect who MySQL believes you are.

List users from mysql.user
SELECT user, host FROM mysql.user ORDER BY user, host;

# Display existing accounts.

## grants and roles
Grant privileges on one database
GRANT SELECT, INSERT, UPDATE, DELETE ON app_db.* TO 'app_user'@'%';

# Allow an app account to use one schema.

Grant all privileges on a database
GRANT ALL PRIVILEGES ON app_db.* TO 'admin_user'@'%';

# Give broad schema-level rights.

Show grants for a user
SHOW GRANTS FOR 'app_user'@'%';

# Inspect the effective privileges granted to an account.

Revoke privileges
REVOKE DELETE ON app_db.* FROM 'app_user'@'%';

# Remove previously granted rights.

Reload grant tables
FLUSH PRIVILEGES;

# Refresh privilege data after direct system-table changes.

Recommended next

No recommendations yet.