Home > Web Development > MYSQL Tutorials > How to create a new user and grant permissions in MySQL.

How to create a new user and grant permissions in MySQL.

Diego Cortés
Diego Cortés
January 20, 2025
How to create a new user and grant permissions in MySQL.

Creating a new user in MySQL and granting them the appropriate permissions is a fundamental task for database management. This process allows for effective management of access to information, ensuring that only authorized users can perform certain actions. Below is a detailed procedure to carry out this task simply and effectively.

Step 1: Access MySQL

To begin, it is necessary to access the MySQL server. This can be done using the command line. Open your operating system's terminal and execute the following command:

mysql -u root -p

You will be prompted for the root user password. After entering it, you will have access to the MySQL console.

Step 2: Create a new user

Once inside the MySQL console, the next step is to create a new user. To do this, use the following command:

CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'secure_password';

Replace new_user with the name you want for the new user and secure_password with a password that ensures the security of the account.

Step 3: Grant permissions

After creating the user, it is essential to grant them the necessary permissions to perform their tasks. You can do this by using the following command:

GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'localhost' WITH GRANT OPTION;

This command grants all privileges to the new user, allowing them to access and modify any database on the server. If you wish to grant specific permissions, you can do so as follows:

GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'new_user'@'localhost';

With this command, the user will only be able to perform the actions of selecting, inserting, and updating in the specified database.

Step 4: Apply changes

It is important to remember that after making changes to the permissions, you need to apply updates for them to take effect. This is done by executing the following command:

FLUSH PRIVILEGES;

This updates the permissions on the MySQL server.

Step 5: Verify the creation of the user

To ensure that the new user has been created correctly and has the permissions you granted, you can execute the following command:

SHOW GRANTS FOR 'new_user'@'localhost';

This command will allow you to see all permissions assigned to the user in question.

Conclusion

Creating a new user and granting permissions in MySQL is a straightforward process, but essential for proper database management. By following the above steps, you can effectively manage access to your information.

If you want to continue learning about topics related to database administration and more, I invite you to read more articles like this on my blog.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 30.36 ms