[How to] set a password for mysql root user

This is how to set a password for mysql root user.

Run the following command to set a password

`/usr/bin/mysqladmin -u root password ‘new-password’`

Once the password is applied, Test it by accessing mysql as root from localhost.

You will get something like this,
“`
root@JobNix:~# mysql


ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)“`

That means you have successfully applied the password for root user.

Now, create the following file so that you can access mysql from root user from the localhost without a password.

`vi /root/.my.cnf`
add the following to the file, And
“`
[client]


user=”root”


password=”new-password”“`

Then save it by :wq!

Secure the password file by running,

`chmod 600 /root/.my.cnf`

Now you can access mysql from root user without password.

“`
root@JobNix:~# mysql


Welcome to the MySQL monitor.  Commands end with ; or g.


Your MySQL connection id is 55


Server version: 5.5.32-0ubuntu0.12.04.1 (Ubuntu)“`

That means, You can access mysql from root without any password and all the other local users need a password to access mysql as root from the shell.

Comment if found helpful ![:)](https://www.jobnix.in/wp-includes/images/smilies/icon_smile.gif)

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top