Create user on MySQL

A quick step-by-step guide on how to create users on MySQL

After installing MySQL, you will haverootAvailable to users.

This is the user you might use to try to see if MySQL is working, but it shouldn't be a user for other purposes.

why? because it isToo strong.

Great power brings great responsibility (says Spider-Man's uncle). Especially if you make a mistake, it will bring great danger.

Instead, you should create temporary users, which only have permissions to perform their work and no other permissions. Same as using Linux or any Unix system, for example: you don’t want to userootUser, but you have your own user account.

To create a new user, userootuser:

mysql -u root -p

Then use the command

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

For example, create a file namedtest_userWith passwordtest_password12ARun the command:

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

The command should return one lineQuery OK, 0 rows affected (X.XX sec):

If you use an invalid password, the system will tell you the followingERROR 1819 (HY000): Your password does not satisfy the current policy requirements. In this case, the error indicates that the password is not complex enough because when MySQL was installed, I told it to use a certain strategy for passwords.

That's great! Now that the user has been created, we can use that user to connect to MySQL. In the command line, you can exit by typingQUITAnd enter:

mysql -u test_user -p

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

why? Because the userNo permission to create a new database.

We will learn how to use permissions in another tutorial.

It is worth mentioning that@'localhost'The string we use to create the user. This tells MySQL users to connect only from the local host. This is fine when testing, and any applications that will connect to MySQL are running on the same computer where the DBMS is running.

If it is incorrect, you need to manually enter the IP that the user will connect to, or use%Wildcard:

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

I want to end this tutorial by showing how to delete the user we created:

DROP USER 'test_user'@'localhost';

More database tutorials: