Thursday, April 5, 2012

MySQL- Lost / forgotten root password reset on Linux


Step 1: Stop MySQL daemon if it is currently running

Locate the .pid file that contains the server's process ID

Common locations are /var/lib/mysql/, /var/run/mysqld/ and /usr/local/mysql/data/.

Generally, the file name has an extension of .pid and begins with either mysqld or your system's host name.

or 

Check running process

# ps -ef | grep mysql     

Kills the daemon, if it is running.

# pkill mysqld  

or

# kill -9 /path/to/pid

Step 2: Run MySQL safe daemon with skipping grant tables

 # mysqld_safe --skip-grant-tables &

Step 3: Login to MySQL as root with no password

# mysql -u root mysql

Step 4:  Run UPDATE query to reset the root password

mysql> UPDATE user SET password=PASSWORD("NewPassword") WHERE user="root";

mysql> FLUSH PRIVILEGES;

Step 5:  Stop MySQL safe daemon

Note: This time only run kill (pkill) “mysqld_safe” instead of “mysqld”
               
# pkill mysqld_safe

Step 6:  Start MySQL daemon

# service mysqld start

Step 7: Root password is reset and ready to use

 # mysql -u root -p mysql