Table of Contents
Introduction
To create a user in MySQL there is a command called CREATE USER.
So, if you’ve installed MySQL and now you don’t know how to create a new user, don’t worry, you’ve come to the right place!
Don’t worry, this post will clear all your doubts!
In fact, with this tutorial we will see how easy it is the creation of a new user in MySQL and how to set the password.
How to create a user in MySQL
Before starting to see how to create a new user there are a couple of premises to make.
Of course, MySQL must be installed on your computer.
If not, I recommend downloading it here and following the instructions.
If you are using Ubuntu or other linux distros instead, follow this tutorial.
Once MySQL has been successfully installed, open a terminal and type the following command to enter the MySQL prompt:
sudo mysql
At this point we can proceed with the tutorial.
How to create a user in MySQL without password
Creating a user in MySQL is very simple.
In this section we will see how to create a user without any password.
This means that once this user has been created, you will be able to log in without having to specify any password.
Note that this option is not highly recommended, as all users should have a password for security reasons.
The command to create a new user in MySQL is as follows:
CREATE USER '<my_user>'
Obviously <my_user>
is just a placeholder, replace with the name of the new user you want to create.
Once the command has been launched, you can verify that the new user has been created with the following query:
SELECT user FROM mysql. user;
If you see your new user in the list, you can use it to log into the MySQL prompt.
mysql -u <my_user>
How to create a user in MySQL with password
In the previous section we saw how to create a user without setting any particular password.
Now let’s see how you can create a user with a password.
I always suggest you to use this route because for security reasons it is much better to have a password set for each user.
The command to create a user by also setting a password is the following:
CREATE USER '<my_user>' IDENTIFIED BY '<my_password>';
As usual, remember to substitute the placeholders with wanted data.
Again you can verify that the user was created with the following query:
SELECT user FROM mysql. user;
If you see your new user in the list, you can use it to log into the MySQL prompt.
mysql -u <my_user>
How to grant privileges to a new user
Now that you have created a new user you will also want to grant him permissions.
Well, MySQL also provides you with a command to change the permissions of individual users.
We are talking about the MySQL GRANT command.
With this command you can change the permissions of a user for a single table or for all. More importantly, you can grant a single permission or all of them.
All in all, this is a really powerful command.
The syntax of the command is very simple and I report it to you below:
GRANT <permission> ON <database>.<table> TO '<username>'@'<environment>';
If you have further doubts, do not miss this post in which an in-depth study is made on how to grant privileges to a user in MySQL.
Conclusion
Here we are at the end of this post where we have seen how to create a new user in MySQL.
I hope you were able to create new users from the commando line by following the instructions in this post.
As always feel free to contact me or leave a comment below if something is not clear to you.
If everything is clear to you, move on to the next article!