233

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?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
asm234
  • 2,607
  • 4
  • 18
  • 17
  • If you don't need the data you can "reset" the password by removing mysql in its entirety: https://stackoverflow.com/questions/10853004/removing-mysql-5-5-completely/11869352#11869352 then installing it again (it will prompt you for the "new" root password) FWIW :) – rogerdpack Jan 12 '18 at 22:33
  • Do you know the MySQL root password? – Franck Dernoncourt Jan 22 '18 at 22:29
  • 3
    in mysql-server-5.7 these methods dont work. use 'sudo' without password as mentioned in https://askubuntu.com/a/848504 – Pragalathan M May 13 '18 at 14:37
  • Tip: sort by "oldest" and then look at the newest answers. The most upvoted answers don't work in newer versions of MySQL. – Flimm Jan 04 '21 at 09:41

37 Answers37

300

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. (In some cases, if /var/run/mysqld doesn't exist, you have to create it at first: sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld
  3. Start the mysqld configuration: sudo mysqld --skip-grant-tables &
  4. Login to MySQL as root: mysql -u root mysql
  5. Replace YOURNEWPASSWORD with your new password:

For MySQL < 8.0

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

If your MySQL uses new auth plugin, you will need to use: update user set plugin="mysql_native_password" where User='root'; before flushing privileges.

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

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
FLUSH PRIVILEGES;

Last step:

As noted in comments by @lambart, you might need to kill the temporary password-less mysql process that you started, i.e. sudo killall -9 mysqld and then start normal daemon: sudo service mysql start

References:

  1. Set / Change / Reset the MySQL root password on Ubuntu Linux
  2. How to Reset the Root Password (v5.6)
  3. How to Reset the Root Password (v8.0)
T.Todua
  • 53,146
  • 19
  • 236
  • 237
Mark
  • 8,046
  • 15
  • 48
  • 78
  • 27
    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 08 '13 at 01:39
  • 9
    The method in this answer is only needed for resetting a MySQL root password that you don't know [[source](http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html)]. If you know it, the `SET PASSWORD` MySQL instruction is for you. – tanius Mar 06 '14 at 17:51
  • Can it not be done only by logging in phpMyAdmin as a root user and then executing these SQLs? `UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';` `FLUSH PRIVILEGES;` – itsazzad Feb 22 '15 at 05:42
  • 61
    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 – Sushivam Nov 02 '16 at 11:24
  • 2
    Doesn't work on 5.7. Look at the Anvesh's answer instead. – alexpirine Jan 20 '17 at 19:27
  • 12
    in some cases, you also need to run `mkdir /var/run/mysqld` and `chown mysql: /var/run/mysqld` between steps 1 and 2 – Neville Nazerane Feb 02 '18 at 02:38
  • Probably this just will works on MySQL 5.7.6 and later. To reset in earlier versions of Mysql: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass'); – alvaropaco Feb 07 '18 at 13:48
  • Try to verify your SA credentials. – Mark Jun 22 '18 at 06:57
  • 15
    step 3 did not work for me with Ubuntu 18.04. I just logged in using **sudo mysql** – itsols Oct 24 '18 at 08:02
  • 1
    All these solutions failed in Ubuntu 18.04 and MySQL 5.7.25 I think because they missed `UPDATE user SET plugin="mysql_native_password" WHERE User='root';` after setting the password. Follow details here... https://linuxconfig.org/how-to-reset-root-mysql-password-on-ubuntu-18-04-bionic-beaver-linux – god_is_love Jan 31 '19 at 15:37
  • 1
    Correct answer but before 3rd step run: sudo service mysql start – Soyab Badi Mar 27 '19 at 14:04
  • Soyab Badi> does not help "Access denied for user 'root'@'localhost' (using password: NO)" – Jiří Doubravský Apr 02 '19 at 11:32
  • @JiříDoubravský what step? – Mark Apr 11 '19 at 06:44
  • Note that in some versions both the "Password" column and the "authentication_string" columns exist. Updating them both does the trick. – Daan Apr 24 '19 at 07:43
  • If `sudo mysqld --skip-grant-tables` doesn't work, check the error logs: `tail -f /var/log/mysql/error.log` (it's either you didn't stop the existing process or didn't create `/var/run/mysqld` directory). – kenorb May 10 '19 at 22:47
  • 1
    Step 3: How are you supposed to connect to MySQL which is stopped in Step 1? – Atul Oct 11 '19 at 08:40
  • check comment @Atul.:) – Mark Oct 17 '19 at 01:21
  • This answer is dated 2013. I fixed today with `sudo mysql` (read Amir's answer) – Augusto Jan 29 '20 at 09:49
  • I also had to change `plugin` to: `mysql_native_password` – Jack Mar 11 '20 at 14:46
  • 3
    For anybody struggling to get these statements to execute. The PASSWORD() function was deprecated in more recent versions of mysql, so you will need to do something like this, for example SHA1: `UPDATE mysql.user SET authentication_string = SHA1('MYSECRETPASSWORD') WHERE User = 'root'; FLUSH PRIVILEGES;` – Carl Du Plessis Mar 19 '20 at 12:49
  • 1
    This solution doesn't work, because you don't have rights for mysql.service. The answer below skip-grant-tables worked for me. This shouldn't be the correct answer. – Hakikat41 Oct 23 '20 at 14:27
  • step 4. ```mysql -u root mysql``` needed ```sudo``` – Nomikos Strigkos Jan 14 '21 at 12:25
  • @Sushivam I've updated answer, so now it has correct step numbers. – T.Todua Nov 14 '22 at 11:38
  • additionally, it will show `The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement` in MySQL 8, so you need to flush privileges at first. – T.Todua Nov 14 '22 at 11:47
  • Works on MySQL 8.0.32 running on Ubuntu 22. – LeperAffinity666 Apr 09 '23 at 00:20
176

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. use mysql

  6. select * from mysql.user where user = 'root'; - Look at the top to determine whether the password column is called password or authentication_string

  7. UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost'; - Use the proper password column from above

  8. FLUSH PRIVILEGES;

  9. exit

  10. sudo vim /etc/mysql/my.cnf

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

  12. sudo service mysql restart

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

Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
narko
  • 3,645
  • 1
  • 28
  • 33
  • 11
    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 05:15
  • 10
    I had to run UPDATE mysql.user SET authentication_string=PASSWORD('newpwd') WHERE User='root'; instead for line 5. – mbuc91 May 11 '17 at 05:21
  • 2
    The only solution that worked for me. I have Ubuntu 17.04 and mysql 5.7 – Mohamed Nov 20 '17 at 01:52
  • In point 6 you have to add `user` should be this way `select * from mysql.user where user = 'root';` – Mario May 17 '18 at 02:50
  • 1
    Sad this does not work for me mysql 5.7.22 ubuntu 18.04 – Mario May 17 '18 at 02:55
  • This is the only solution that worked for me. Ubuntu 18.04, mysql Ver 14.14 Distrib 5.7.24 – viion Dec 13 '18 at 11:17
  • Works here in Ubuntu 18.04 too – gies0r Jul 29 '19 at 22:38
  • 1
    Everything was working as expected. But when trying to login it's saying Enter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost' – ThunderStorm Nov 05 '19 at 10:49
  • 2
    The function `PASSWORD` doesn't exist in newer versions of MySQL. – Flimm Jan 04 '21 at 09:30
101

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

user12345
  • 2,876
  • 2
  • 24
  • 25
  • 3
    Works even when you lost the original MySQL root password – nice. – tanius May 02 '14 at 12:29
  • 4
    Works with `Ver 14.14 Distrib 5.5.38, for debian-linux-gnu` – justinpage Sep 14 '14 at 01:56
  • Worked with mysql v 5.0 as well... `sudo dpkg-reconfigure mysql-server-5.0` – dsghi Feb 24 '15 at 03:19
  • 4
    sudo dpkg-reconfigure mysql-server-5.7 not working for Ubuntu 18.04 for reset root password. Any other alternate? – Jagdeep Singh May 21 '18 at 06:25
  • Ubuntu: `sudo dpkg-reconfigure mysql-server-5.7; sudo mysql_secure_installation` – kenorb May 10 '19 at 21:42
  • @user12345 If I have data in databases, Can I use this `sudo dpkg-reconfigure mysql-server-5.7` command ? it will just reconfigure root or data will be lost? – Nikhil Radadiya Oct 14 '20 at 11:59
  • @NikhilRadadiya No guarantee, however `sudo dpkg-reconfigure mysql-server-5.7` has never caused me any data loss or changed data in my own databases. [I would not expect any data loss](https://unix.stackexchange.com/questions/299366/reconfigure-mysql-question). However make a server snapshot if you can. You can [backup MySQL files with caveats](https://www.linode.com/docs/databases/mysql/create-physical-backups-of-your-mariadb-or-mysql-databases/). Be sure to [understand the risks](https://stackoverflow.com/questions/2482491/is-copying-var-lib-mysql-a-good-alterntive-to-mysqldump). – user12345 Oct 14 '20 at 22:42
  • Newer versions of MySQL don't have the `PASSWORD` function. – Flimm Jan 04 '21 at 09:34
89

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='localhost';
EXIT;

Turn off MySQL.

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

Start the MySQL service normally.

sudo service mysql start
Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
fabriciofreitag
  • 2,843
  • 22
  • 26
  • The other ways did not work for me but this one (thanks!), or you can also go here http://rricketts.com/reset-root-password-mysql-5-7-ubuntu-16-04-lts/ – Dung Dec 07 '17 at 17:25
  • 11
    Very useful ! In my specific case (Ubuntu 18.04 LTS), I had to replace the host '%' to 'localhost' – FredericK May 07 '18 at 13:16
  • This didn't work in ubuntu 18.04. But it worked with this little change described here https://stackoverflow.com/a/53385753/1591143 – Aleksandar Popovic Jan 19 '19 at 16:16
  • Newer versions of MySQL do not support the `PASSWORD` function. – Flimm Jan 04 '21 at 09:34
79

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;
Anvesh
  • 7,103
  • 3
  • 45
  • 43
  • Indeed. Also, it is closer to the [official documentation](http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html). In case you run into this: "ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded”, take a look at http://stackoverflow.com/questions/37879448/mysql-fails-on-mysql-error-1524-hy000-plugin-auth-socket-is-not-loaded – Wtower Aug 22 '16 at 09: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 02:16
  • 1
    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 01:55
  • 1
    authenticated _string helped – Faiyaz Md Abdul Nov 24 '17 at 17:49
79

At first run this command:

sudo mysql

and then you should check which authentication method of your MySQL user accounts use.So run this command

SELECT user,authentication_string,plugin,host FROM mysql.user;

now you can see something like this already :

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

in the table that is in the above , you can see that all of your mysql users accounts status & if you have set a password for root account before you see mysql_native_password in plugin column instead auth_socket. All in all for change your root password you should run :

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Be sure to change password to a strong password of your choosing. Then for reload your server to put your new changes into effect run this;

FLUSH PRIVILEGES;

So again check the authentication methods which has employed by your mysql , by this command:

SELECT user,authentication_string,plugin,host FROM mysql.user;

and now the output is :

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

as you can see in the grant table your root account has mysql_native_password . now you can exit MYSQL shell

exit;

That's it.just you should restart mysql by sudo service mysql restart. Now you can login to mysql as a root account with your password easily.

Masoud
  • 1,099
  • 1
  • 10
  • 22
47

I faced problems with ubuntu 18.04 and mysql 5.7, this is the solution

Try restart mysql-server before execution the comands

sudo service mysql restart

MYSQL-SERVER >= 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET authentication_string=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;

MYSQL-SERVER < 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET password=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;
Jerfeson Guerreiro
  • 745
  • 1
  • 10
  • 19
  • 1
    Thanks, I'm also running Ubuntu 18.04 here and the `MYSQL-SERVER >= 5.7` version was the only thing that actually solved the problem – Miguelgraz Jul 04 '18 at 12:41
  • 1
    This is the one that worked for in Ubuntu 18.04 mysql installation. `MYSQL-SERVER >= 5.7` version to be precise. – Sathish Manohar Feb 16 '19 at 07:56
  • UPDATE user SET password=PASSWORD('YOUR_PASSWORD') WHERE User='root'; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('YOUR_PASSWORD') WHERE User='root'' at line 1 – asdjfiasd Sep 14 '20 at 15:30
  • Thanks,I'm running Ubuntu 18.04 and the MYSQL-SERVER = 5.7, it works for me – kl w May 21 '21 at 13:59
36

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');

Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
faisalbhagat
  • 2,142
  • 24
  • 27
15
  1. Stop MySQL sudo service mysql stop

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

  3. Give MySQL user permission to write to the service directory. sudo chown mysql: /var/run/mysqld

  4. Start MySQL manually, without permission checks or networking. sudo mysqld_safe --skip-grant-tables --skip-networking &

5.Log in without a password. mysql -uroot mysql

6.Update the password for the root user.

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

  1. Turn off MySQL. sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

  2. Start the MySQL service normally. sudo service mysql start

  • 1
    Hi, this answer works for me, but I had to use another command: ``UPDATE mysql.user SET authentication_string=PASSWORD('solutionclub3@*^G'), plugin='mysql_native_password' WHERE User='root';`` without ``AND Host='%'``. Thanks! – David Corral May 22 '18 at 17:51
  • This also worked for me! – frost kazuma May 04 '22 at 03:16
14

In my case this option helped : https://stackoverflow.com/a/49610152/13760371
Thank you, Rahul.

except for the following moment, when I try entered command:

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

the console issued a warning:

1681 'password' is deprecated and will be removed in a future release

cured with this command:

UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';

MySQL version 5.7.X

My variant:

1. > sudo service mysql stop
2. > sudo mkdir /var/run/mysqld
3. > sudo chown mysql: /var/run/mysqld
4. > sudo mysqld_safe --skip-grant-tables --skip-networking &
5. > mysql -uroot mysql
6. > UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
7. > \q;
8. > sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
9. > sudo service mysql start
ktscript
  • 309
  • 4
  • 11
13

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.

user2206324
  • 356
  • 2
  • 8
  • 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 03:46
  • sudo dpkg-reconfigure mysql-server-5.7 on ubuntu 16.04 – Nick Barrett Feb 28 '17 at 06:42
  • 2
    I ran `sudo dpkg-reconfigure mysql-server-5.7` on Ubuntu 16.04, it didn't prompt to enter a new password. – Franck Dernoncourt Jan 22 '18 at 22:32
  • If it says `mysql-server is broken or not fully installed`, one can use `sudo apt purge mysql*` and `sudo apt install mysql-server` – Pavel Oct 22 '18 at 07:30
  • @user2206324 If I have data in databases, Can I use this sudo dpkg-reconfigure mysql-server-5.5 command ? it will just reconfigure root or data will be lost? – Nikhil Radadiya Oct 14 '20 at 12:01
13

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';
EXIT;

Turn off MySQL.

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

Start the MySQL service normally.

sudo service mysql start
Imran Zahoor
  • 2,521
  • 1
  • 28
  • 38
woodpecker
  • 211
  • 4
  • 8
12

This solution belongs to the previous version of MySQL. By logging in to MySQL using socket authentication, you can do it.

sudo mysql -u root

Then the following command could be run.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Details are available here .

Huseyin
  • 1,499
  • 2
  • 25
  • 39
  • 2
    I couldn't login as **`sudo mysql -u root`** but I was able to do it as **`sudo mysql`** and run the `ALTER USER` command. – itsols Oct 24 '18 at 07:59
  • I was getting this error: "Your password does not satisfy the current policy requirements". I was able to correct this here: https://stackoverflow.com/a/43094873/9073437 . After setting a modified password policy, everything worked fine. – Grandtour May 12 '20 at 13:35
  • ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement – asdjfiasd Sep 14 '20 at 15:31
10

This is the solution for me. I work at Ubuntu 18.04: https://stackoverflow.com/a/46076838/2400373

But is important this change in the last step:

UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost'; 
juanitourquiza
  • 2,097
  • 1
  • 30
  • 52
  • Gosh! Thanks a lot! This is the only way that was working in my case. Forget about that stupid secure installation script as it kept my freshly installed 5.7 wide open to users without ever asking for a password ... if Linux/Ubuntu is for safety-aware users why is it this hard to protect a crucial application like MySQL server? – Thomas Urban Mar 21 '19 at 15:34
  • 2
    Newer versions of MySQL don't support the `PASSWORD` function. – Flimm Jan 04 '21 at 09:35
8

For Ubuntu 18.04 and mysql version 14.14 Distrib 5.7.22 follow the below step to reset the mysql password.

Step 1

sudo systemctl stop mysql

Step 2

sudo systemctl edit mysql

This command will open a new file in the nano editor, which you'll use to edit MySQL's service overrides. These change the default service parameters for MySQL. This file will be empty, so add the following content:

[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid --skip-grant-tables --skip-networking

Step 3

sudo systemctl daemon-reload
sudo systemctl start mysql

Step 4

sudo mysql -u root

Step 5

FLUSH PRIVILEGES;

Step 6

UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHEREuser = 'root';

Step 7

UPDATE mysql.user SET plugin ='mysql_native_password' WHERE user = 'root';

Step 8

sudo systemctl revert mysql

and finally

sudo systemctl restart mysql

Now enjoy

Masum Billah
  • 2,209
  • 21
  • 20
7

Echoing rogerdpack's comment: if you don't know the MySQL root password and you don't care about MySQL data/settings, you can reinstall it and reset the root's password as follows:

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo rm -rf /var/lib/mysql
sudo apt-get install -y mysql-server mysql-client 

During the installation, you can choose the root's password:

enter image description here

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
5

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.

Peter Mutisya
  • 350
  • 4
  • 7
  • ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement – asdjfiasd Sep 14 '20 at 15:27
3

You don't need all this. Simply log in:

mysql -u root -p

Then change the current user's password as the mysql> prompt:

mysql> set password=password('the_new_password');
mysql> flush privileges;
mprivat
  • 21,582
  • 4
  • 54
  • 64
3

Most of the answers on this topic are outdated; two major changes have occurred in MySQL up until the writing of this answer:

1- the 'Password' field in the user table has been replaced by 'authentication_string' column.

2- the 'Password' encryption function : PASSWORD("of some text") is deprecated.

Please refer to this link for further information:dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html

Elie Asmar
  • 2,995
  • 4
  • 17
  • 30
3

1.Open nano / vim to create a file with the following content and Save file as ~/mysql-pwd

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEWPASSWORD';
  1. Stop mysql sudo systemctl stop mysql
  2. Run sudo mysqld -init-file=~/mysql-pwd
  3. Restart mysql sudo systemctl start mysql
  4. Now login mysql -u root -p. password will be your NEWPASSWORD
Nayeem Azad
  • 657
  • 5
  • 20
3

The steps below worked for me. I'm using MySQL 8.* on Ubuntu

  1. Stop MySQL service and check status to confirm the service stopped
sudo systemctl stop mysql
sudo systemctl status mysql
  1. Edit the systemd config file so you can access MySQL without permission check

    sudo systemctl edit mysql

  2. Copy and paste the following 3 lines

[Service]

ExecStart=

ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking

After pasting the lines CTRL+0 to save and then CTRL+X to exit

  1. Reload mysql service and start it (starts the service with --skip-grant-table)
sudo systemctl daemon-reload
sudo systemctl start mysql

5.Now connect to MySQL server without password

sudo mysql -u root

6.Load the grant tables by running

mysql> FLUSH PRIVILEGES;
  1. Reset the root password

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPasswordHere';

  1. Close the mysql connection

mysql> exit

  1. Revert the modification done on the mysql systemd file

sudo systemctl revert mysql

  1. Reload the mysql daemon for changes to take place.

sudo systemctl daemon-reload

  1. Lastly restart the MySQL service

sudo systemctl restart mysql

Now connect to mysql with the new password set in step 7

You can visit this link reset root password for mysql 8 for more details.

Sam Kihika
  • 101
  • 1
  • 6
2

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.

Community
  • 1
  • 1
tanius
  • 14,003
  • 3
  • 51
  • 63
2

You can use this command:

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

after that please use flush:

FLUSH PRIVILEGES;
kenorb
  • 155,785
  • 88
  • 678
  • 743
Rahul Karande
  • 259
  • 2
  • 9
2

To update the "root" Mysql user password you must have in mind that you will need of super user permissions for that. If you have super user privilegies, try the following commands:

MySQL 5.7.6 and later

sudo su
service mysql stop
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
\q;
exit
mysql -u root -p MyNewPass

MySQL 5.7.5 and earlier

sudo su
service mysql stop
mysql -u root
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
\q;
exit
mysql -u root -p MyNewPass
alvaropaco
  • 1,573
  • 18
  • 29
2

As mysql documentation on the password() function says:

This function was removed in MySQL 8.0.11.

This invalidates pretty much all existing answers for mysql v8.0.11 and newer.

Per mysql documentation the new generic way to reset the root password is as follows:

The preceding sections provide password-resetting instructions specifically for Windows and Unix and Unix-like systems. Alternatively, on any platform, you can reset the password using the mysql client (but this approach is less secure):

Stop the MySQL server if necessary, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges, and disables account-management statements such as ALTER USER and SET PASSWORD. Because this is insecure, if the server is started with the --skip-grant-tables option, it enables --skip-networking automatically to prevent remote connections.

Connect to the MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables:

shell> mysql

In the mysql client, tell the server to reload the grant tables so that account-management statements work:

mysql> FLUSH PRIVILEGES;

Then change the 'root'@'localhost' account password. Replace the password with the password that you want to use. To change the password for a root account with a different host name part, modify the instructions to use that host name.

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

You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally (without the --skip-grant-tables and --skip-networking options).

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • These instructions did not work for me with mysqld version 8.0.19 on Ubuntu 19.10. The mysqld socket is not created when I use the --skip-grant-tables option. If you are in the same situation, see my answer here https://stackoverflow.com/a/60386378/12559612 – Dominic108 Feb 25 '20 at 01:58
  • Thanks for your answer, a combination with an old answer works for me: `sudo service mysql stop sudo mkdir /var/run/mysqld sudo chown mysql: /var/run/mysqld sudo mysqld_safe --skip-grant-tables --skip-networking &` In another console: `mysql -uroot mysql ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword'; systemctl restart mysql` – Talu Dec 23 '20 at 23:22
2

If you know the 'root' users password, log in to mysql with that credentials. Then execute the following query to update the password.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_passowrd';
Udara Seneviratne
  • 2,303
  • 1
  • 33
  • 49
1

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.

joseph
  • 940
  • 10
  • 19
1

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)
Mohd Arshil
  • 195
  • 1
  • 1
  • 9
1

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

sudo dpkg-reconfigure mysql-server-5.6
MSS
  • 3,520
  • 24
  • 29
  • I tried but its not working.. I am getting this bellow error.. could you please help? This installation of MySQL is already upgraded to 5.7.21, use --force if you still need to run mysql_upgrade – Vijaysinh Parmar Mar 23 '18 at 07:30
1

I had to go this route on Ubuntu 16.04.1 LTS. It is somewhat of a mix of some of the other answers above - but none of them helped. I spent an hour or more trying all other suggestions from MySql website to everything on SO, I finally got it working with:

Note: while it showed Enter password for user root, I didnt have the original password so I just entered the same password to be used as the new password.

Note: there was no /var/log/mysqld.log only /var/log/mysql/error.log

Also note this did not work for me:
sudo dpkg-reconfigure mysql-server-5.7

Nor did:
sudo dpkg-reconfigure --force mysql-server-5.5

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

Then:

  1. kill the current mysqld pid
  2. run mysqld with sudo /usr/sbin/mysqld &
  3. run /usr/bin/mysql_secure_installation

    Output from mysql_secure_installation

    root@myServer:~# /usr/bin/mysql_secure_installation

    Securing the MySQL server deployment.

    Enter password for user root:

    VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?

    Press y|Y for Yes, any other key for No: no Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

    New password:

    Re-enter new password: By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.

    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success.

    Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.

    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success.

    By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

    • Dropping test database... Success.

    • Removing privileges on test database... Success.

    Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success.

    All done!

JGlass
  • 1,427
  • 2
  • 12
  • 26
1

You can try these some steps to reset mysql 5.7 root password :

Stop Mysql Service 1st

sudo /etc/init.d/mysql stop 

Login as root without password sudo mysqld_safe --skip-grant-tables &

After login mysql terminal you should need execute commands more:

use mysql;




UPDATE mysql.user SET authentication_string=PASSWORD('solutionclub3@*^G'), plugin='mysql_native_password' WHERE User='root';


flush privileges;


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

After you restart your mysql server If you still facing error you must visit : Reset MySQL 5.7 root password Ubuntu 16.04

Amitesh Kumar
  • 3,051
  • 1
  • 26
  • 42
Inderpal Singh
  • 101
  • 1
  • 2
1

Change the MySQL root password. In Simpler way

All these commands should be run as root.

Login through MySQL command line tool using your old password:

Step-1

mysql -uroot -p"your_old_password"

Then run below command:

Step-2

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

Method-2 (First login using your old password using above command)

Run this command, which sets a password for the current user:

SET PASSWORD = PASSWORD('your_new_password');

Above command is for the current user. If you want to change the password for other user, you can put the user name instead of "root".

PyDevSRS
  • 1,715
  • 16
  • 17
1

I haven't seen the official steps recommended by the MySQL 8.0 guide, which were the only ones that worked for me. Here's a summary of those steps.

  1. Stop the MySQL server if it is running. Look in /var/lib/mysql/, /var/run/mysqld/, or /usr/local/mysql/data/ to find the pid file with the server's process ID. Generally the file begins with either mysqld or your system's host name and ends with .pid. Replace mysql-data-directory and host_name that you just found, in the following command:

    $ sudo kill `sudo cat /mysql-data-directory/host_name.pid`
    
  2. This command will create a text file in /tmp/mysql-init with the SQL statement and makes the mysql user the owner. Replace in the command MyNewPass with your own password.

    $ echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';" > /tmp/mysql-init && sudo chown mysql /tmp/mysql-init
    
  3. Start the MySQL server by running the following command on the command line. After this the password is updated and you can close the server again with CTRL+C.

    $ sudo mysqld --user=mysql --init-file=/tmp/mysql-init &
    
  4. Remove the temporary file with your password:

    $ sudo rm /tmp/mysql-init
    
Daan
  • 7,685
  • 5
  • 43
  • 52
0

To reset or change the password enter sudo dpkg-reconfigure mysql-server-X.X (X.X is mysql version you have installed i.e. 5.6, 5.7) and then you will prompt a screen where you have to set the new password and then in next step confirm the password and just wait for a moment. That's it.

Gaurav Paliwal
  • 1,556
  • 16
  • 27
0

Setting MySQL Password

  1. stop MySQL database service

sudo /etc/init.d/mysql stop

  1. create a new mysqld directory

sudo mkdir /var/run/mysqld/

  1. give mysql user access

sudo chown mysql /var/run/mysqld/

  1. start MySQL in safe mode...

sudo mysqld_safe --skip-grant-tables &

  1. logon to the database server without a password

sudo mysql -u root

  1. use the default mysql database

use mysql;

  1. change the root password

update user set authentication_string=PASSWORD("New_Passwore_Here") where User='root';

  1. Save the changes

    flush privileges; exit;

  2. stop MySQL safe_mode and start MySQL default service

sudo /etc/init.d/mysql stop

sudo /etc/init.d/mysql start

  1. log back onto MySQL database using the root new password

sudo mysql -u root -p

*** Source : https://websiteforstudents.com/resetting-mysql-root-password-on-ubuntu-16-04-17-10-and-18-04-lts/

Viraj
  • 638
  • 1
  • 8
  • 10
0

For those who find password function doesn't work for mysql 8.0 and creating the file and doing the init are very difficult. What I did is drop the root user in the user table in the safe mode as many mentioned above. And create the root user again by referring to https://askubuntu.com/questions/766334/cant-login-as-mysql-user-root-from-normal-user-account-in-ubuntu-16-04/784347#784347, Then do

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password(this is the plugin which specified in the user table) BY 'password';
FLUSH PRIVILEGES;

Hope someone can find this when above doesn't work. Try this way.

Youshikyou
  • 365
  • 1
  • 8
-1

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.

Rock
  • 17
  • 2
    The question is about `ubuntu` OS not windows. What do you mean by _DELETE SYSTEM 32 for windows_? – EhsanT Jan 14 '17 at 02:13