MySQL: How to reset or change the MySQL root password?
The official and easy way to reset the root password on an ubuntu server...
If you are on 16.04, 14.04, 12.04:
sudo dpkg-reconfigure mysql-server-5.5
If you are on 10.04:
sudo dpkg-reconfigure mysql-server-5.1
If you are not sure which mysql-server version is installed you can try:
dpkg --get-selections | grep mysql-server
Updated notes for mysql-server-5.7
Note that if you are using mysql-server-5.7 you can not use the easier dpkg-reconfigure method shown above.
If you know the password, login and run this:
UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE USER='root';
FLUSH PRIVILEGES;
Alternatively, you can use the following:
sudo mysql_secure_installation
This will ask you a series of questions about securing your installation (highly recommended), including if you want to provide a new root password.
If you do NOT know the root password, refer to this Ubuntu-centric write up on the process.
See for more info:
https://help.ubuntu.com/16.04/serverguide/mysql.html https://help.ubuntu.com/14.04/serverguide/mysql.html
The only method that worked for me is the one described here (I am running ubuntu 14.04). For the sake of clarity, these are the steps I followed:
sudo vim /etc/mysql/my.cnf
Add the following lines at the end:
[mysqld] skip-grant-tables
sudo service mysql restart
mysql -u root
use mysql
select * from mysql.user where user = 'root';
- Look at the top to determine whether the password column is called password or authentication_stringUPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost';
- Use the proper password column from aboveFLUSH PRIVILEGES;
exit
sudo vim /etc/mysql/my.cnf
Remove the lines added in step 2 if you want to keep your security standards.
sudo service mysql restart
For reference : https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
Set / change / reset the MySQL root password on Ubuntu Linux. Enter the following lines in your terminal.
- Stop the MySQL Server:
sudo /etc/init.d/mysql stop
- Start the
mysqld
configuration:sudo mysqld --skip-grant-tables &
In some cases, you've to create the /var/run/mysqld
first:
sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld
- Login to MySQL as root:
mysql -u root mysql
- Replace
YOURNEWPASSWORD
with your new password:
For MySQL < 8.0
UPDATE
mysql.user
SET
Password = PASSWORD('YOURNEWPASSWORD')
WHERE
User = 'root';
FLUSH PRIVILEGES;
exit;
Note: on some versions, if
password
column doesn't exist, you may want to try:UPDATE user SET authentication_string=password('YOURNEWPASSWORD') WHERE user='root';
Note: This method is not regarded as the most secure way of resetting the password, however, it works.
For MySQL >= 8.0
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
References:
- Set / Change / Reset the MySQL root password on Ubuntu Linux
- How to Reset the Root Password (v5.6)
- How to Reset the Root Password (v8.0)