Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Create New User in MySQL: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. Understanding MySQL Users and Hosts
  2. Prerequisites for Creating a User
  3. Step-by-Step: Creating a New User
  4. Granting Privileges: What You Need to Know
  5. Common Mistakes and How to Avoid Them
  6. Creating a User with Remote Access
  7. Deleting or Modifying Users
  8. Frequently Asked Questions

To create a new user in MySQL, use the CREATE USER statement followed by the username and host, then grant privileges with GRANT. For example: CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; creates the user, and GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost'; gives full access.

Understanding MySQL Users and Hosts

In MySQL, a user is identified not just by a username but also by the host from which they connect. This is why you often see entries like 'username'@'localhost' or 'username'@'%'. The host part determines which client machines are allowed to connect using that account. localhost means only connections from the same machine where MySQL runs, while % is a wildcard that allows any host. For example, a web server connecting to a database server would use the web server's IP address or hostname, not localhost.

Prerequisites for Creating a User

  • You need administrative access to MySQL, typically as the root user or a user with the CREATE USER privilege.
  • Access to a MySQL client (command line, MySQL Workbench, or a programming language's MySQL library).
  • Decide on a strong password and the specific privileges the new user should have.

Step-by-Step: Creating a New User

  1. Log into MySQL as an administrator: mysql -u root -p (you'll be prompted for the root password).
  2. Create the user with a password: CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPass!123';
  3. Grant the necessary privileges. For example, to give all privileges on a specific database: GRANT ALL PRIVILEGES ON myappdb.* TO 'appuser'@'localhost';
  4. Apply the privilege changes: FLUSH PRIVILEGES; (not always required after GRANT, but harmless).
  5. Verify the user exists: SELECT user, host FROM mysql.user;

Granting Privileges: What You Need to Know

Privileges control what a user can do. Common ones include SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, and ALL PRIVILEGES. The scope of a privilege is defined by the database and table pattern. For example, GRANT SELECT, INSERT ON mydb.* TO 'user'@'host'; allows reading and adding data to any table in mydb, but not modifying or deleting. To grant privileges on a specific table: GRANT UPDATE ON mydb.orders TO 'user'@'host';. You can also grant global privileges using *.* as the scope, which is powerful and should be used sparingly.

Common Mistakes and How to Avoid Them

  • Forgetting the host part: CREATE USER 'john'@'localhost' is different from CREATE USER 'john'@'%'. If you try to connect from a different host, you'll get an access denied error.
  • Using a weak password: Always use a strong, unique password. MySQL 8.0 has a default password validation plugin that enforces complexity.
  • Not granting the correct privileges: Users often get too many privileges (security risk) or too few (application errors). Follow the principle of least privilege.
  • Forgetting to flush privileges: In most cases, GRANT and CREATE USER take effect immediately, but if you modify the grant tables directly (not recommended), you need to FLUSH PRIVILEGES.

Creating a User with Remote Access

To allow a user to connect from another machine, specify the remote host or use a wildcard. For example, to allow a user to connect from any IP address: CREATE USER 'remoteuser'@'%' IDENTIFIED BY 'password';. However, using % is insecure because it allows connections from anywhere. A better practice is to specify the exact IP or subnet: CREATE USER 'remoteuser'@'192.168.1.%' IDENTIFIED BY 'password'; (allows connections from the 192.168.1.0/24 network). Note that the MySQL server must also be configured to listen on the appropriate network interface and not be blocked by a firewall.

Deleting or Modifying Users

To change a user's password: ALTER USER 'user'@'host' IDENTIFIED BY 'newpassword';. To revoke privileges: REVOKE ALL PRIVILEGES ON mydb.* FROM 'user'@'host';. To delete a user: DROP USER 'user'@'host';. Always be careful when dropping users, as you may lose access to data if the user owns objects.

Frequently Asked Questions

What is the difference between 'user'@'localhost' and 'user'@'%'?

localhost restricts connections to the same machine where MySQL runs. % allows connections from any host. If you are running the application on the same server as MySQL, use localhost for security. For remote connections, specify the IP address or subnet instead of % whenever possible.

How do I see all users in MySQL?

Run the query: SELECT user, host FROM mysql.user;. This shows all usernames and their allowed hosts. You can also see more details by selecting additional columns like plugin or password_expired.

Why do I get 'Access denied' when trying to connect as the new user?

This usually happens because the host part doesn't match your connection source, or the password is incorrect. Double-check the host you're connecting from and the exact username@host combination you created. Also ensure the user has at least the USAGE privilege (automatically granted) and any database-specific privileges needed.

Can I create a user without a password?

Yes, you can omit the IDENTIFIED BY clause, which creates a user with an empty password. However, this is extremely insecure and not recommended for any real environment. Always set a strong password.

What is the default authentication plugin in MySQL 8.0?

MySQL 8.0 uses caching_sha2_password as the default authentication plugin. Older clients may not support it, so you might need to specify IDENTIFIED WITH mysql_native_password when creating the user if you're using an older client library.

How do I grant a user access to only one database?

Use a scoped GRANT: GRANT ALL PRIVILEGES ON mydb.* TO 'user'@'host';. Replace mydb with the database name. This grants all privileges on all tables within that database, but not on other databases.