How to create user in MySql

Home / How to create user in MySql

To create a new user in mysql is very easy , you just have to follow the simple steps :

First login to mysql using root, for login as root in mysql you have to use the following command

 mysql -u root -p

-u is represent the user that is root and -p is used for password ,after typing the above command press enter ,it will ask for password enter the password and you will successfully login to mysql.

Now you can easily see how many user are present in your mysql by the following command

 select user , host from mysql.user; 

Now it’s time to create a new user just type the following command

Syntax :    CREATE USER ‘newuser’@’localhost’ IDENTIFIED BY ‘password’ ;

Note : in above syntax use the name of user that you want to give at the place of ‘newuser’ and use localhost at the place of ‘localhost’ if your database present on localhost but if your database is present on another server and you want to access that database then use the server ip at place of ‘localhost’.

Example:

CREATE USER ‘javadream’@’localhost’ IDENTIFIED BY ‘12345@’ ;     //For localhost

CREATE USER ‘javadream’@’172.168.0.1’ IDENTIFIED BY ‘12345@’ ;  // If Database Present on another server then use ip of the server.

Now you have to give the privileges to javadream user to access the database,if you want to give the complete access of all the databases then use the following command

GRANT ALL  PRIVILEGES ON  *.*  TO  ‘javadream’@’localhost’  IDENTIFIED BY  ‘12345@’;

if you want to give the specific database privileges then use the following command

GRANT  ALL  PRIVILEGES  ON  userdemo.*  TO  ‘javadream’@’localhost’ IDENTIFIED  BY  ‘12345@’;

In above command userdemo is the name of database

Now just flush the privileges using following command

flush PRIVILEGES 

Now you are done ,you just have succesfully created the new user ,to use this user just login by that user name like following command

mysql  -u javadream -p

after this press enter and type password 12345@

And now you also can see how many users are present in mysql using following command

select  user,host  from  mysql.user;

Help Others, Please Share

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *