MySQL: How to Create a New User and Grant Permissions – Windows/Ubuntu

Are you setting up a MySQL database for your application or project and need to grant access to other users? Managing user accounts and permissions in MySQL is crucial for maintaining security and controlling access to your database. In this guide, we’ll walk you through the process of creating a new user and granting permissions in MySQL.

Video Guide: How to Create a New User and Grant Permissions in MYSQL – Windows/Ubuntu/Linux

Step 1: Logging in to MySQL

To begin, you’ll need to log in to your MySQL server as a user with administrative privileges. You can do this using the mysql command-line tool. For example:

mysql -u root -p

Enter the password for the root user when prompted.

Step 2: Creating a New User

Once you’re logged in, you can create a new MySQL user using the CREATE USER command. Replace new_username with the username you want to create and password with the desired password. For instance:

CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password';

This command creates a user named new_username that can only connect from the localhost.

Step 3: Granting Permissions

After creating the user, you need to grant appropriate permissions to this user. You can grant permissions using the GRANT statement. Replace database_name with the name of the database to which you want to grant access, and privileges with the specific privileges you want to grant (e.g., SELECT, INSERT, UPDATE, DELETE, etc.). Here’s an example:

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

If you want to grant all privileges, you can use ALL PRIVILEGES. For example:

GRANT ALL PRIVILEGES ON database_name.* TO 'new_username'@'localhost';

Step 4: Flushing Privileges

After granting privileges, you need to flush the privileges to apply the changes:

FLUSH PRIVILEGES;

Step 5: Exiting MySQL

Once you have completed the above steps, you can exit MySQL by typing:

EXIT;

Managing user accounts and permissions is an essential aspect of database administration in MySQL. By following these steps, you can create new users and grant them appropriate access to your MySQL databases, ensuring secure and controlled access to your data.

Check out our New Blog on – How to Connect MySQL in Visual Studio Code or How to Reset MySQL Root Password on Windows