TechBuzzPro

Create MySQL User Account & Grant Privileges

· 4 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 mysql command-line client, MySQL Workbench, or phpMyAdmin.
  • Basic familiarity with SQL syntax and the database name you want to grant access to.

Step-by-Step

  1. 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.
  2. Create the new user
    The basic syntax is:
    CREATE USER 'username'@'host' IDENTIFIED BY 'password';
    Replace username, host (use localhost for local-only access, or '%' to allow any host), and password with a strong password. Example:
    CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPass!2024';
  3. Grant privileges
    Use the GRANT statement 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 in myapp_db, but not to other databases.
  4. Apply the changes
    Run FLUSH PRIVILEGES; to reload the grant tables and make the changes take effect immediately.
  5. 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
  6. Revoke privileges (if needed)
    If you need to remove some privileges later, use REVOKE. For example, to revoke DELETE privilege:
    REVOKE DELETE ON database_name.* FROM 'username'@'host';
    Then run FLUSH 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 ALL on *.*: 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 USER with GRANT: CREATE USER only creates the account; you must separately grant privileges.

Tips

  • Grant only necessary privileges: Instead of ALL PRIVILEGES, you can grant specific ones like SELECT, INSERT, UPDATE to 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 GRANTS for 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., localhost vs %).
  • "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.