# Creating a user on MySQL

> Learn how to create a new user in MySQL with the CREATE USER command instead of relying on the powerful root account, then connect with the new credentials.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-01-06 | Topics: [Database](https://flaviocopes.com/tags/database/) | Canonical: https://flaviocopes.com/mysql-how-to-create-user/

Right after you install MySQL, you will have the `root` user available.

This is the user you might use to try and see if MySQL is working, but it should not be the user for other kinds of usages.

Why? Because it's **too powerful**.

With great power comes great responsibility (said Spider-Man's uncle). And in particular, great danger if you make mistakes.

Instead, you should create ad-hoc users that only have permissions to perform their job, and no more. Same for using Linux or any Unix system, for example: you don't want to be using the `root` user, but your own user account.

To create a new user, connect to MySQL using the `root` user:

```bash
mysql -u root -p
```

![MySQL terminal showing successful login as root user with welcome message and mysql prompt](https://flaviocopes.com/images/mysql-how-to-create-user/Screen_Shot_2019-12-18_at_19.07.11.png)

then use the command

```sql
CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';
```

For example, to create a user named `test_user` with password `test_password12A` run the command:

```sql
CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'test_password12A';
```

The command should return a line saying `Query OK, 0 rows affected (X.XX sec)`:

![MySQL terminal showing CREATE USER command executed successfully with Query OK message](https://flaviocopes.com/images/mysql-how-to-create-user/Screen_Shot_2019-12-18_at_19.09.49.png)

If you used an invalid password, the system would tell you something like `ERROR 1819 (HY000): Your password does not satisfy the current policy requirements`. In this case the error says that the password is not complex enough, because when I installed MySQL I told it to use a certain policy on passwords.

That's great! Now the user has been created, and we can connect using that user to MySQL. From the command line you can exit by typing `QUIT`, and typing:

```bash
mysql -u test_user -p
```

![MySQL terminal showing successful login as test_user with welcome message and mysql prompt](https://flaviocopes.com/images/mysql-how-to-create-user/Screen_Shot_2019-12-18_at_19.12.56.png)

Now if I try to create a database, I'd get an error saying `ERROR 1044 (42000): Access denied for user 'test_user'@'localhost' to database 'testing'`:

![MySQL terminal showing access denied error when test_user tries to create a database](https://flaviocopes.com/images/mysql-how-to-create-user/Screen_Shot_2019-12-18_at_19.13.32.png)

Why? Because the user **does not have the permission to create a new database**.

We'll see how to work with permissions in another tutorial.

It's worth mentioning the `@'localhost'` string we used to create the user. This tells MySQL that the user can only connect from localhost. Which is fine when testing things out and when any application that will connect to MySQL is running on the same computer that runs the DBMS.

When this is not true, you either need to manually enter the IP the user will connect from, or use the `%` wildcard:

```sql
CREATE USER 'test_user'@'%' IDENTIFIED BY 'test_password12A';
```

I want to close this tutorial by showing how to remove the user we created:

```sql
DROP USER 'test_user'@'localhost';
```
