Create MySQL User Account & Grant Privileges
In this article
This guide walks you through creating a new MySQL user account and granting it privileges on a database. You'll learn the exact SQL commands, how to apply them, and how to avoid common permission pitfalls. Before starting, ensure you have access to the MySQL server with an account that has administrative rights (typically root), and know the server's hostname (often localhost).
What You'll Need
- Access to a MySQL server (local or remote) with credentials for an admin user (e.g.,
root). - MySQL client tool: the
mysqlcommand-line client, MySQL Workbench, or phpMyAdmin. - Basic familiarity with SQL syntax and the database name you want to grant access to.
Step-by-Step
- Connect to MySQL as an admin
Open your terminal or client and connect to the MySQL server. For the command line, use:mysql -u root -p
Enter the root password when prompted. If you're using a remote server, add-h hostname. - Create the new user
The basic syntax is:CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Replaceusername,host(uselocalhostfor local-only access, or'%'to allow any host), andpasswordwith a strong password. Example:CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPass!2024'; - Grant privileges
Use theGRANTstatement to give the user permissions on a database. The most common grant is all privileges on a specific database:GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
For example:GRANT ALL PRIVILEGES ON myapp_db.* TO 'appuser'@'localhost';
This grants full access to all tables inmyapp_db, but not to other databases. - Apply the changes
RunFLUSH PRIVILEGES;to reload the grant tables and make the changes take effect immediately. - Verify the user and privileges
Check that the user exists and what privileges they have:SHOW GRANTS FOR 'username'@'host';
You can also test logging in as the new user:mysql -u appuser -p - Revoke privileges (if needed)
If you need to remove some privileges later, useREVOKE. For example, to revoke DELETE privilege:REVOKE DELETE ON database_name.* FROM 'username'@'host';
Then runFLUSH PRIVILEGES;again.
Common Mistakes
- Forgetting the host part:
'username'@'localhost'is different from'username'@'%'. If you connect from a remote IP, the host must match or be%. - Using
GRANT ALLon*.*: This grants global privileges over all databases, which is a security risk. Always limit to a specific database when possible. - Not running
FLUSH PRIVILEGES: In most cases, changes take effect immediately, but running it ensures the grant tables are reloaded. - Using weak passwords: MySQL enforces password policies in newer versions; use a strong password to avoid errors.
- Confusing
CREATE USERwithGRANT:CREATE USERonly creates the account; you must separately grant privileges.
Tips
- Grant only necessary privileges: Instead of
ALL PRIVILEGES, you can grant specific ones likeSELECT, INSERT, UPDATEto minimize risk. - Use a dedicated database: Create a separate database for each application and grant the user access only to that database.
- Document your users: Keep track of which user has which privileges, especially in shared environments.
- Use
SHOW GRANTSfor auditing: Regularly review user privileges to ensure they still match current needs. - Consider using roles (MySQL 8.0+): Roles can simplify privilege management by grouping permissions and assigning them to multiple users.
Troubleshooting
- "Access denied" when connecting: Double-check the username, host, and password. Ensure the user was created with the correct host (e.g.,
localhostvs%). - "ERROR 1396 (HY000): Operation CREATE USER failed": The user already exists. Check with
SELECT User, Host FROM mysql.user;and either drop the existing user or use a different name. - "ERROR 1133 (42000): Can't find any matching row in the user table": Occurs when the host part doesn't match. Use
'username'@'%'if you're unsure of the connecting host. - Privileges not taking effect: Run
FLUSH PRIVILEGES;and ensure you're connecting as the new user, not as root. - Remote connections failing: Check that MySQL is configured to listen on the network (not just localhost) and that firewalls allow the port (default 3306).
- Password policy errors: If you get an error about password strength, use a longer password with mixed case, numbers, and special characters.
Frequently Asked Questions
Can I create a user and grant privileges in one command?
No, MySQL does not have a single command for both. You must run CREATE USER first, then GRANT. However, you can run both statements in a single script or transaction.
What does '%' mean in the host part?
The % is a wildcard that matches any host. So 'user'@'%' allows the user to connect from any IP address. For local-only access, use localhost.
How do I grant privileges on all databases?
Use GRANT ALL PRIVILEGES ON *.* TO 'user'@'host';. This gives the user full access to all databases on the server. Use this sparingly and only for admin users.
What's the difference between GRANT ALL and GRANT ALL PRIVILEGES?
They are equivalent. ALL is a shorthand for ALL PRIVILEGES. Both grant all available privileges for the specified scope.
How do I delete a MySQL user?
Use DROP USER 'username'@'host';. This removes the user and all their privileges. You may need to revoke privileges first if they exist on other databases.
Can I change a user's password later?
Yes, use ALTER USER 'username'@'host' IDENTIFIED BY 'newpassword';. Then run FLUSH PRIVILEGES; if needed.