Stop service:
~ $ /etc/init.d/mysql stop
Add the next parameters to he my.cnf file config:
skip-grant-tables skip-networking
The first parameter disables grants on mysql so we can access without password to any database. If mysql was compiled with the option –disable-grant-options (not usually), setting this parameter will not work.
The second one disable network connection, by this way we can connect to mysql only from localhost.
Start mysql:
~ $ /etc/init.d/mysql start
Connect to mysql without password:
~ $ mysql -u root
Update the pasword:
mysql> use mysql; mysql> UPDATE user SET Password=PASSWORD('new_pass') WHERE User='root'; mysql> FLUSH PRIVILEGES; mysql> exit
And restart mysql:
~ $ /etc/init.d/mysql restart