Create User in MySQL

In this post, we will learn to create user in MySQL server and also assign a role to specific database.

Make sure you have already logged in to your MySQL server with root credentials.

Create User

Syntax:

CREATE USER 'newUser'@'serverAddress' IDENTIFIED BY 'newPassword';

Let’s create a user called `myUser` with the password `myPass123`

Example:

CREATE USER 'myUser'@'localhost' IDENTIFIED BY 'myPass123';

Now, the user is created in the database.

Grant Permission

To give the specific permission to the user, we can follow the following syntax:

GRANT type_of_permission ON database_name.table_name TO ‘username’@'localhost’;

Let’s take a following scenario.

We have a database myDb and the user myUser should manage the database myDb then we can use following command:

GRANT ALL ON myDb.* TO ‘myUser’@'localhost’;

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments