Skip to content
Share on
Updated

Introduction

More often than not, when working with a database you will need to be able to create and delete users. Whether working in a team that has multiple members needing access to the database or creating and deleting test users to simulate database privileges, knowing how to add and remove database users is important.

In this short guide, we will cover the basics of creating and deleting database users in MySQL. These commands will have you ready to get started on granting database access and establish a foundation for future role management.

How do you create MySQL users?

Creating users in MySQL is key for collaborating with others and for testing access rights and privileges. It is important to note that in order to create users you must login with a user with the privileges described in the prerequisites.

Basic syntax

The basic syntax for creating a new user is relatively simple. You use the CREATE USER command, specify the user and host for the new account, and set a password for it:

CREATE USER '<user>'@'<host>' IDENTIFIED BY '<password>';

This creates a new user account and assigns a password for the account at the same time.

How do you create a user with a password?

The IDENTIFIED BY clause in the example above configures authentication for the new account by setting its password. The clause is optional, but you should always include it (or configure another authentication method, as shown below). An account created without it has no password, so anyone who connects from a matching host can log in as that user without one.

The host part of the account name controls where the user can connect from. Use localhost for a user that connects from the database server itself, or the specific host name or IP address that the user connects from. The % wildcard matches any host, so only use it for accounts that really need to connect from anywhere.

To assign or change a password after the account exists, use the ALTER USER command, as described in our guide to managing users and authentication.

How do you give a new user access to a database?

A new account can log in, but it can't read or change the data in your databases until you grant it privileges. Grant only the privileges the account needs, on only the databases it needs. For example, to create an account for an application that reads and writes the data in an app_db database from the same machine:

CREATE USER 'app_user'@'localhost' IDENTIFIED BY '<password>';
GRANT SELECT, INSERT, UPDATE, DELETE ON app_db.* TO 'app_user'@'localhost';

Running GRANT requires the GRANT OPTION privilege as well as the privileges you are granting, which an administrative account like root has.

app_user can now query and modify the rows in app_db's tables, but it can't create or drop tables, and it can't access any other database:

mysql> USE other_db;
ERROR 1044 (42000): Access denied for user 'app_user'@'localhost' to database 'other_db'
mysql> CREATE TABLE app_db.extra (id INT);
ERROR 1142 (42000): CREATE command denied to user 'app_user'@'localhost' for table 'extra'

You can learn more in our guide to managing privileges in MySQL.

How do you create a user with Unix socket authentication?

While password authentication is the most common authentication method for most users, it's not the only option. MySQL provides many different internal and external authentication mechanisms that you can configure your user accounts to use. As an example, we'll configure a new account using Unix socket authentication.

Unix socket authentication can be used in Linux or Unix-like environments so that an account on the operating system is given access to the same account name within MySQL without further authentication. In this configuration, the MySQL administrator knows that user accounts on the operating system are tightly controlled.

So if there is a mary user on the operating system, they will be able to login to the 'mary'@'localhost' account within MySQL if Unix socket authentication is the defined authentication mechanism. Let's configure this now.

Socket authentication requires the auth_socket plugin, so first load the plugin by typing:

INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';

Next, create a user account that matches a user account you have on your operating system. For this example, we'll use the mary account we discussed above. If you don't use a name that matches one of your operating system names, you won't be able to authenticate using this user.

To create the user with socket authentication, we need to use the IDENTIFIED WITH clause (different than the IDENTIFIED BY clause used earlier) to specify the authentication plugin to use:

CREATE USER 'mary'@'localhost' IDENTIFIED WITH auth_socket;

Now, you should be able to authenticate to the 'mary'@'localhost' MySQL user from the mary user on your operating system. When logged in as mary, connect to the database without providing any username or password:

mysql

You should be signed in automatically through the Unix socket authentication you configured.

How do you delete MySQL users?

Keeping user accounts around that no longer serve a purpose is a security risk. You can remove accounts easily with the DROP USER command.

The basic syntax looks like this:

DROP USER '<user>'@'<host>';

So to delete the 'mary'@'localhost' user, you would type:

DROP USER 'mary'@'localhost';

If you try to delete a user that does not exist, you will receive an error:

ERROR 1396 (HY000): Operation DROP USER failed for 'mary'@'localhost'

To avoid this, you can add the IF EXISTS clause before the account name. If the user exists, it will be deleted. If it does not, only a warning will be given:

DROP USER IF EXISTS 'mary'@'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)

Conclusion

In this quick guide, we covered the basics of creating and deleting users for MySQL databases as well as some additional options.

Once your users are created, we have additional informative articles diving deeper in user role management in MySQL as well as user authentication.

A hosted Postgres database for your next project, with connection pooling built in and a free plan. Explore Prisma Postgres →

About the author
Alex Emerich

Alex Emerich

Alex is your typical bird watching, hip-hop loving bookworm that also enjoys writing about databases. He currently lives in Berlin, where he can be seen walking through the city aimlessly like Leopold Bloom.