How do I change the MySQL root password and username in ubuntu server? Do I need to stop the mysql service before setting any changes?

I have a phpmyadmin setup as well, will phpmyadmin get updated automatically?

share|improve this question

15 Answers 15

up vote 90 down vote accepted

Set / change / reset the MySQL root password on Ubuntu Linux. Enter the following lines in your terminal.

  1. Stop the MySQL Server: sudo /etc/init.d/mysql stop
  2. Start the mysqld configuration: sudo mysqld --skip-grant-tables &
  3. Login to MySQL as root: mysql -u root mysql
  4. Replace YOURNEWPASSWORD with your new password:

    UPDATE
      mysql.user
    SET
      Password = PASSWORD('YOURNEWPASSWORD')
    WHERE
      User = 'root';
    FLUSH PRIVILEGES;
    exit;
    

Note: This method is not regarded as the most secure way of resetting the password, however, it works.

References:

  1. Set / Change / Reset the MySQL root password on Ubuntu Linux
  2. How to Reset the Root Password
share|improve this answer
3  
Thanks. what is the secure way then? – asm234 May 15 '13 at 17:36
12  
Of course, after this point you'll need to kill the temporary, password-less server process that you started in step 2. maybe use sudo killall -9 mysqld? and then sudo service mysql start to restart the normal daemon... – Lambart Dec 8 '13 at 1:39
5  
The method in this answer is only needed for resetting a MySQL root password that you don't know [source]. If you know it, the SET PASSWORD MySQL instruction is for you. – tanius Mar 6 '14 at 17:51
5  
It doesn't work with me! – moderns Jul 14 '14 at 17:44
6  
i followed till 3rd step and i get Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) when i login thru root – Sachin S Nov 2 '16 at 11:24

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

share|improve this answer
3  
Works even when you lost the original MySQL root password – nice. – tanius May 2 '14 at 12:29
3  
Works with Ver 14.14 Distrib 5.5.38, for debian-linux-gnu – KLVTZ Sep 14 '14 at 1:56
    
This worked for me on 5.5.40-0ubuntu0.14.04.1 – ken koehler Jan 15 '15 at 19:14
1  
Works flawless on my ubuntu 14.04, thanks – codemode May 11 '15 at 18:34
13  
Doesn't seem to work with 5.7 on newer ubuntu – fuzzyTew Dec 26 '16 at 2:15

I am sharing the step by step final solution to reset a MySQL password in Linux Ubuntu.

Reference taken from blog (dbrnd.com)

Step 1: Stop MySQL Service.

sudo stop mysql

Step 2: Kill all running mysqld.

sudo killall -9 mysqld

Step 3: Starting mysqld in Safe mode.

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

Step 4: Start mysql client

mysql -u root

Step 5: After successful login, please execute this command to change any password.

FLUSH PRIVILEGES;

Step 6: You can update mysql root password .

UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';

Note: On MySQL 5.7, column Password is called authentication_string.

Step 7: Please execute this command.

FLUSH PRIVILEGES;
share|improve this answer
2  
This actually worked for me, not the accepted answer, give it a try! – Santiago Martí Olbrich Oct 20 '15 at 14:49
    
Indeed. Also, it is closer to the official documentation. In case you run into this: "ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded”, take a look at stackoverflow.com/questions/37879448/… – Wtower Aug 22 '16 at 9:36
    
I also had to change mysql.user.plugin from 'auth_socket' to 'mysql_native_password' for my root user with host of 'localhost'. – Brandon May 17 '17 at 2:16
    
I had to use "sudo /etc/init.d/mysql stop" to stop the service and "UPDATE mysql.user SET authentication_string=PASSWORD('newpwd') WHERE User='root';" to update the password – L Bahr Aug 15 '17 at 1:55
    
authenticated _string helped – Faiyaz Md Abdul Nov 24 '17 at 17:49

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:

  1. sudo vim /etc/mysql/my.cnf
  2. Add the following lines at the end:

    [mysqld]
    
    skip-grant-tables
  3. sudo service mysql restart

  4. mysql -u root

  5. UPDATE mysql.user set password = PASSWORD('your_new_password') where user = 'root' and host = 'localhost';

OR in some systems, eg: 5.7.18 you may want to do

UPDATE mysql.user SET authentication_string = PASSWORD('password'), password_expired = 'N' WHERE User = 'root' AND Host = 'localhost'; FLUSH PRIVILEGES;

For reference : https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

  1. exit

  2. Remove the lines added in step 2 if you want to keep your security standards.

share|improve this answer
4  
Finally, this worked for me! I'm running MySQL 5.7.18 on Ubuntu 16.04. I tried dpkg-reconfigure, Anvesh's answer, Christian Mark's answer, and even the instructions on MySQL's help page. – mbuc91 May 11 '17 at 5:15
4  
I had to run UPDATE mysql.user SET authentication_string=PASSWORD('newpwd') WHERE User='root'; instead for line 5. – mbuc91 May 11 '17 at 5:21
    
The only solution that worked for me. I have Ubuntu 17.04 and mysql 5.7 – Mohamed Nov 20 '17 at 1:52

Change the MySQL root password.

This method exposes the password to the command-line history, these commands should be run as root.

  1. Login through mysql command line tool:

    mysql -uroot -poldpassword
    
  2. Run this command:

    SET PASSWORD FOR root@'localhost' = PASSWORD('newpassword');
    

or

  1. Run this command, which sets a password for the current user ('root' for this case) :

    SET PASSWORD = PASSWORD('newpassword');

share|improve this answer

If you would like to change the MySQL root password, in a terminal enter:

sudo dpkg-reconfigure mysql-server-5.5

The MySQL daemon will be stopped, and you will be prompted to enter a new password.

share|improve this answer
1  
This is a much better way to do it. I was unable to run the previous commands because of some mysql error... – Andy Jan 28 '16 at 3:46
    
sudo dpkg-reconfigure mysql-server-5.7 on ubuntu 16.04 – Nick Barrett Feb 28 '17 at 6:42

What worked for me (Ubuntu 16.04, mysql 5.7):

Stop MySQL

sudo service mysql stop

Make MySQL service directory.

sudo mkdir /var/run/mysqld

Give MySQL user permission to write to the service directory.

sudo chown mysql: /var/run/mysqld

Start MySQL manually, without permission checks or networking.

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

On another console, log in without a password.

mysql -uroot mysql

Then:

UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';
EXIT;

Turn off MySQL.

sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

Start the MySQL service normally.

sudo service mysql start
share|improve this answer
    
this is what working, thanks for posting. – Dheeraj Thedijje Nov 8 '17 at 9:03
    
Thanks alot man! – MD Shahrouq Dec 5 '17 at 11:06
    
The other ways did not work for me but this one (thanks!), or you can also go here rricketts.com/reset-root-password-mysql-5-7-ubuntu-16-04-lts – Dung Dec 7 '17 at 17:25

This works like charm I did it for Ubuntu 16.04. Full credit to below link as I got it from there. [https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts][1]

Stop MySQL

sudo service mysql stop

Make MySQL service directory. sudo mkdir /var/run/mysqld

Give MySQL user permission to write to the service directory.

sudo chown mysql: /var/run/mysqld

Start MySQL manually, without permission checks or networking.

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

Log in without a password.

mysql -uroot mysql

Update the password for the root user. make sure at atleast root account gets updated by the below query. make some selection and check the existing values if you like

UPDATE mysql.user SET 
authentication_string=PASSWORD('YOURNEWPASSWORD'), 
plugin='mysql_native_password' WHERE User='root' AND Host='%';
EXIT;

Turn off MySQL.

sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

Start the MySQL service normally.

sudo service mysql start
share|improve this answer

When you use MySQL's PASSWORD() on the system where you want to change the password, it can cause the password turn up in a MySQL log in cleartext [source]. Keeping them, their backups etc. as secure as the password sounds like nightmare to me, so I rather like to do it as follows:

  1. On your local machine, run this with your password:

     mysql -u someuser -p < <(echo "SELECT PASSWORD('mypass');")
    

    Note the space in front to prevent it from turning up in the bash history (for other distros than Ubuntu, this might work differently – source).

  2. On your server machine, execute the following command to change its MySQL root password (replace myhash with your password's hash as printed by the first command):

    mysql -u root -p < <(echo "SET PASSWORD FOR root@localhost = 'myhash';")
    
  3. Optionally, let's be a bit paranoid: On your local machine, clear your terminal screen with clear and purge your virtual terminal scrollback, to hide the cleartext password appearing in the command above.

share|improve this answer

If you know your current password, you don't have to stop mysql server. Open the ubuntu terminal. Login to mysql using:

mysql - username -p

Then type your password. This will take you into the mysql console. Inside the console, type:

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

Then flush privileges using:

> flush privileges;

Then you are all done.

share|improve this answer

Instead of resetting the password there is a work around on the local machine if you have setup phpmyadmin to connect without giving the password or username. Check this out by starting mysql, apache etc. I have xampp installed in my local machine. So starting the xampp will start all the necessary services. Now going to http://localhost/phpmyadmin shows me all the databases. This confirms that you have saved the username and passsword in the config file of phpmyadmin which can be found in the phpmyadmin install location. If you have xampp installed the phpmyadmin folder can be found in the root folder of xampp installation. Search for the word password in the config.inc.php file. There you will find the password and username.

share|improve this answer

You can easily change the mysql password if deployed on xampp through provided phpadmin gui.

phpMyAdmin -> User Accounts -> Edit Privileges (Select the intended user) -> Change Password (Tab)
share|improve this answer

You can use this command :

UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';

after that pleas use flush FLUSH PRIVILEGES;

share|improve this answer

for mysql 5.6 this command works and you can set password through the wizard:

sudo dpkg-reconfigure mysql-server-5.6
share|improve this answer

when changing/resetting the MySQL password the following commands listed above did not help. I found that going into the terminal and using these commands is pointless. instead use the command sudo stop everything. DELETE SYSTEM 32 for windows if that helps.

share|improve this answer
    
The question is about ubuntu OS not windows. What do you mean by DELETE SYSTEM 32 for windows? – EhsanT Jan 14 '17 at 2:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.