Saturday 3 September 2011

How to reset mysql root password

You can reset the MySQL root password using the following command if know the current root password

root@amaziah [~]#mysqladmin -u root -p 'current password' password 'new password'

If you don't know the  root password then, follow the below steps

First stop MySQL server,

root@amaziah [~]#service mysql stop
                    OR
root@amaziah [~]#/etc/init.d/mysql stop


Then edit the following file,

root@amaziah [~]#vi /etc/rc.d/init.d/mysql

edit the line, 

$bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
To
$bindir/mysqld_safe --skip-grant-tables --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &

Then start the MySQL server,

 root@amaziah [~]#/etc/init.d/mysql start

Once the MySQL server is started, you can login to MySQL as root user without password

root@amaziah [~]#mysql -u root mysql

Then enter the following command to set new password for root,

mysql>UPDATE user SET password=PASSWORD ('new password') WHERE user='root';
mysql>flush privileges ;
mysql>quit

Now you have to stop the MySQL server and edit /etc/rc.d/init.d/mysql and change it to as it was before. ie. change the line 

$bindir/mysqld_safe --skip-grant-tables --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
To
$bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &

Once this has been done, start the MySQL server.

root@amaziah [~]#/etc/init.d/mysql start

Done. Now you can login to MySQL using the new password that you have set.  





No comments:

Post a Comment