Thursday, December 15, 2005

How to restore mysql admin/root password?

To day I'm trying to use CMS (Wordpress) in my server. But when i was extracting then i realize that i didn't remember the mySQL password!
Huh... !! How can i use this Wordpress without database?
This problem make me headache. I'm trying to guess the password but it failed. How fool i am, how can i forget the password !!
But i already found solution for my problem. I could use mysql in safe mode to change the admin/root password.

The steps are:
1. First stop your mysql
There are many ways to do this:
  • In Fedora Core : service mysqld stop or /etc/init.d/mysqld stop.
  • In other linux distribution (You can use it in Fedora too): killall mysqld
2. Run this command in your shell:
[root@myhost root]#mysqld_safe --skip-grant-tables --autoclose
This command will make mysql run in safe mode. So it will not prompt any password when you type mysql

3. Follow this step to change your mysql password
[root@myhost root]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1 to server version: 3.23.41

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql> USE mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> UPDATE user
-> SET password=password("newpassword")
-> WHERE user="root";
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit;


*Remember to change newpassword with your password.

4. Then restart mysql
service mysqld restart (directly restart) or killall mysqld (to stop) then start mysql

5. All done, if you can enter with your new password
[root@myhost root]#mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 4.1.15

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

No comments: