Sunday, April 26, 2015

How to Change Root Password in MySQL

Following are the methods to use on both Windows and Unix Environment including Ubuntu, Debian, Redhat, CentOS, Fedora, Arch Linux, SUSE etc.

Set MySQL Root Password Using mysqladmin

This command ‘mysqladmin’ works whether password is not currently assigned for the root user account. So we will set the password without giving current password.
[root@linuxpathfinder /]# mysqladmin -u root password 'newpassword'
 [Note: Here, first attemp to set password for root.]
Change MySQL User (non-root) Password Using mysqladmin
mysqladmin command is also used to change user’s (non-root) password.
[root@linuxpathfinder /]# mysqladmin -u asrk -pcurrentpassword password 'newpassword'
Change MySQL User’s Password Using UPDATE SQL Command
You can also use the SQL Update command to change the user’s password as shown below.
mysql> UPDATE user SET password=PASSWORD('newpassword') WHERE user='asrk';
 Query OK, 2 rows affected (0.01 sec)
 Rows matched: 2  Changed: 2  Warnings: 0

Change MySQL Root Password Using mysqladmin

MySQL root password can be changed using mysqladmin command. It should not space between -p and currentpassword as shown below.
[root@linuxpathfinder /]# mysqladmin -u root -pcurrentpassword password 'newpassword'
When you have made a change with new password then make sure can you login with new password, as shown below.
[root@linuxpathfinder /]# mysql -u root -pnewpassword

 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 8
 Server version: 5.1.25-rc-community MySQL Community Server (GPL)
 mysql>
Logged in successfully.

Change MySQL Root Password From MySQL UPDATE Command

You will use the SQL Update command to change the password as shown below. First of all, you need to login to MySQL root account using old password.
[root@linuxpathfinder /]# mysql -u root -poldpassword

 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 8
 Server version: 5.1.25-rc-community MySQL Community Server (GPL)
 mysql>
Change root Password with UPDATE Command
mysql> UPDATE user SET password=PASSWORD('newpassword') WHERE user='root';
 Query OK, 1 row affected (0.00 sec)
 Rows matched: 1  Changed: 1  Warnings: 0
Verify the new MySQL root password
When you will make a change then make sure can you login with new password, as shown below.
[root@linuxpathfinder /]# mysql -u root -pnewpassword

 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 8
 Server version: 5.1.25-rc-community MySQL Community Server (GPL)
 mysql>

No comments:

Post a Comment