On Unix, use the following procedure to reset the password for the MySQL ‘root’@’localhost’ account. To change the password for a root account with a different host name part, modify the instructions to use that host name.
The instructions assume that you will start the MySQL server from the Unix login account that you normally use for running it. For example, if you run the server using the mysql login account, you should log in as mysql before using the instructions. Alternatively, you can log in as root, but in this case you must start mysqld with the –user=mysql option. If you start the server as root without using –user=mysql, the server may create root-owned files in the data directory, such as log files, and these may cause permission-related problems for future server startups. If that happens, you will need to either change the ownership of the files to mysql or remove them.
Log on to your system as the Unix user that the MySQL server runs as (for example, mysql).
Step1: Stop the mysql service
(Ubuntu and Debian) Run the following command:
$ sudo /etc/init.d/mysql stop
(CentOS, Fedora, and Red Hat Enterprise Linux) Run the following command:
$ sudo /etc/init.d/mysqld stop
Step2: Start MySQL without a password
Run the following command. The ampersand (&) at the end of the command is required.
$ sudo mysqld_safe --skip-grant-tables &
Step3: Connect to MySQL
$ mysql -uroot
Step4: Set a new MySQL root password
mysql> UPDATE mysql.user -> SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N' -> WHERE User = 'root' AND Host = 'localhost';
Step5: Flush Privileges
mysql> FLUSH PRIVILEGES; mysql> quit;