How to create a new user and grant permissions in MySQL
Below we show you a detailed manual on how to create a new user and grant permissions in MySQL:
Step 1: Log in to MySQL
To create a new user and grant permissions in MySQL, you must log in to your MySQL server. You can do this in several ways, but a common way is to open a terminal or command line and type the following:
mysql -u root -p
This will log into MySQL with the root user. It will ask you to enter your root password before continuing.
Step 2: Create a new user
Once you have logged into MySQL, you can create a new user using the following command:
´CREATE ´USER 'username'@'localhost' IDENTIFIED BY 'password';
In this command, you must replace username with the username you wish to create and password with the password you wish to assign to that user.
Step 3: Grant permissions to the new user
Once you have created the user, you must grant them permissions so they can interact with the databases. You can do this using the following command:
GRANT permission ON database_name.* TO 'username'@'localhost';
In this command, permission represents the type of permission you wish to grant, such as SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, etc. database_name is the name of the database in which you wish to grant permissions, and username is the username you wish to grant permissions to.
You can grant multiple permissions by separating them with commas. For example, if you wish to grant SELECT and INSERT permissions on the my_database database to the my_user user, you can use the following command:
GRANT SELECT, INSERT ON my_database.* TO 'my_user'@'localhost';
Step 4: Update permissions
Once you have granted permissions to the new user, you must update the permissions in MySQL for them to take effect. You can do this using the following command:
FLUSH PRIVILEGES;
This command will update the permissions in MySQL so that the newly created user can interact with the databases.
Step 5: Exit MySQL
Once you have created a new user and granted permissions, you can exit MySQL using the following command:
exit
This will close the MySQL session and return to the terminal or command line.
success Done! Now you have created a new user and granted them permissions to interact with the databases in MySQL.