Search The Hostwinds Guides Knowledge Base

How to Change Your MySQL Password in Ubuntu 14.04

Share This Article [TheChamp-Sharing]

If you would like to reset the password for the root  user’s MySQL password, this guide will help you through that process. 

Reset MySQL Password

Stop the MySQL Server

You will want to have the correct version before continuing with this guide and are able to get it using this command:

mysql --version

Before continuing with the following, you will want to know that these steps are intended to be run by the root user by using su or sudo commands. Logging in as your root user is not recommended.

In order to change the MySQL password, you will need to first stop the MySQL service.

service mysql stop

If you know your password, you can change it using MySQL command line with the following for the root user in this example

mysql -u root -p

mysql> set password=password('new_password');
mysql> flush privileges;

Replace new_password above with your new password.

By running MySQL without loading any information about the user privileges, you can access the command line without using root privileges which would require a password.  You can do this by not allowing the database to load the grant tables.  You’ll also want to skip networking in order to avoid any security risks. 

It is important to note that using --skip-networking  below is very insecure and should only be run while resetting the root  user’s password if you have forgotten it.

Start the MySQL command prompt using this command: 

mysqld_safe --skip-grant-tables --skip-networking &

You’ll now be able to access the database without using the root password. 

mysql -u root

A MySQL command prompt will appear:

MySQL prompt
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql>

You can now change the password and will want to remain in the MySQL command prompt.

Changing The Password

You will first want to use the FLUSH PRIVILEGES command. 

mysql>  FLUSH PRIVILEGES;

Now we can actually change the root password and exit the MySQL command line by pressing CTRL + C to exit.

You can then use one of the below commands to change your password based on the version of MySQL you are using.

For MySQL 5.7.6 and newer use the following command in the MySQL command prompt.

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

For MySQL 5.7.5 and older use in the MySQL command prompt:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

Enter your new password above and replace new_password .

Once you have received message that the query was OK, you can Flush Privileges 

mysql> FLUSH PRIVILEGES;

Restart the MySQL Server

Now you will need to start the service. 

sudo service mysql start

You can now confirm that the password reset worked by running the following command:

mysql -u root -p