You can change the MySQL root password either with the mysqladmin command or through an SQL query.
Change MySQL root password with mysqladmin
If your configuration doesn't require password for login to MySQL, you can set it with the following command:
#mysqladmin -u root password your_password
To update the root password you can use the following command:
#mysqladmin -u root -p'old_password' password 'new_password'
Note: Keep in mind that there should not be a blank space between the argument -p and the password - 'old_password'. Otherwise it will throw an error similar to this one: mysqladmin: Unknown command: 'old_password'
Change MySQL password for other users
With the same command you can change the password for any user.
#mysqladmin -u rutix -p'old_password' password 'new_password'
Change MySQL root password through an SQL query
You will need to login as root to MySQL in order to access the user table where all users are stored.
#mysql -u root -p
mysql> USE mysql; mysql> UPDATE user SET password = PASSWORD("new_password") WHERE User = 'root'; mysql> FLUSH PRIVILEGES;
Be careful with the update query, because if there are multiple users with the same username as root, it will change the password to all of them. For example, i have two root users, one that can login to MySQL from localhost and one that can login from any public ip address. So you might want to specify the 'Host':
mysql> USE mysql; mysql> UPDATE user SET password = PASSWORD("new_password") WHERE User = 'root' AND Host = 'localhost'; mysql> FLUSH PRIVILEGES;
See also: Recover MySQL root password