229

I recently installed MySQL and it seems I have to reset the password after install. It won't let me do anything else.

Now I already reset the password the usual way:

update user set password = password('XXX') where user = root;

(BTW: took me ages to work out that MySQL for some bizarre reason has renamed the field 'password' to 'authentication_string'. I am quite upset about changes like that.)

Unfortunately it seems I need to change the password a different way that is unknown to me. Maybe someone here has already come across that problem?

23 Answers 23

574

If this is NOT your first time setting up the password, try this method:

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

And if you get the following error, there is a high chance that you have never set your password before:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 

To set up your password for the first time:

mysql> SET PASSWORD = PASSWORD('your_new_password');
Query OK, 0 rows affected, 1 warning (0.01 sec)

Reference: https://dev.mysql.com/doc/refman/5.6/en/alter-user.html

12
  • 11
    Thanks! Very confusing error message from MySQL though!
    – Ed .
    Commented Jan 21, 2016 at 21:06
  • 1
    This worked for me with mysql-5.7.13-osx10.11-x86_64
    – GiriB
    Commented Jul 21, 2016 at 15:24
  • 1
    After the 'mysql> SET PASSWORD = PASSWORD('your_new_password');', I am getting the following error: 'ERROR 29 (HY000): File './mysql/user.MYD' not found (Errcode: 2 - No such file or directory)'. Can someone help me ?
    – Ray Kim
    Commented Jan 4, 2017 at 4:09
  • 4
    You'll note that contrarily to what MySQL suggests, you do not use the "ALTER USER" statement, but just "SET PASSWORD"... Commented Aug 14, 2017 at 3:36
  • 2
    This command is currently giving 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 'PASSWORD
    – Nathan
    Commented Sep 12, 2023 at 17:58
188

If you started mysql using mysql -u root -p

Try ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Source: http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

9
  • 1
    Thank you! This worked for me! I am using mysql-5.7.11-osx10.10-x86_64 .
    – LeArNr
    Commented Feb 27, 2016 at 12:42
  • This worked; Thanks! It deeply saddens me that even though MySQL is such a widely used program, there is no one-stop documentation to install and get it running fast.
    – Shatu
    Commented Mar 11, 2017 at 17:51
  • 11
    After much frustration, a variant of this worked on mint 18 mysql 5.7.18: ALTER USER 'root'@'localhost' indentified WITH mysql_native_password BY 'password';
    – Steve
    Commented Jul 6, 2017 at 15:31
  • That definitely worked on a new mysql install on Oracle Linux 7. Thanks. Commented Sep 14, 2017 at 14:06
  • 1
    @Steve thank you. For others, his command also works on MySQL 8. Commented Sep 14, 2022 at 7:17
35

I have same problem on Mac

First, log in mysql with sandbox mode

mysql -u <user> -p --connect-expired-password

Then, set password

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

Query OK, 0 rows affected, 1 warning (0.01 sec)

It works for me ~

via: https://dba.stackexchange.com/questions/118852/your-paswssword-has-expired-after-restart-mysql-when-updated-mysql-5-7-8-rcde

2
  • 1
    Seems quite complicated keeping in mind that you have to do this before even using the database at all. Commented Aug 20, 2016 at 1:12
  • in the warning it wants you to use SET PASSWORD FOR 'root'@'localhost' = 'XXXX'; Commented Jan 3, 2017 at 8:21
34

If you use MySQL 5.7.6 and later:

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

If you use MySQL 5.7.5 and earlier:

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

MySQL Documentation

2
  • 2
    I'm using MySQL 5.7.9 and the strange thing is SET PASSWORD = PASSWORD('my_pass') worked for me. There are comments of people with more recent versions mentioning that this statement worked for them as well. Despite this, I upvoted your answer since you found this particular reference in MySQL docs which no one mentioned earlier.
    – Armfoot
    Commented Jun 6, 2017 at 15:08
  • 1
    MySQL 8 SET PASSWORD threw and error. ALTER USER worked. Commented Dec 13, 2019 at 2:33
17

On MySQL 5.7.x you need to switch to native password to be able to change it, like:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';
0
13

UPDATE user SET authentication_string=PASSWORD("MyPassWord") 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 '("MyPassWord") WHERE User='root'' at line 1

Resolved with

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

Reference from below site

https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html

1
12

Run these:

$ cd /usr/local/mysql/bin
$ ./mysqladmin -u root password 'password'

Then run

./mysql -u root

It should log in. Now run FLUSH privileges;

Then exit the MySQL console and try logging in. If that doesn't work run these:

$ mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET authentication_string=PASSWORD("XXXXXXX") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit

Change xxxxxx to ur new password. Then try logging in again.

Update. See this http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

It should solve your problem.

If you are on oracle try this

ALTER USER username IDENTIFIED BY password
5
  • No, sorry this too doesn't work. At least not anymore. The 'password' column has been renamed by Oracle, btw. It's now called authentication_string. But I cannot execute this command, because I first have to change the password using the ALTER USER command. This is incredible frustrating, because that is not how you traditionally would update the password. ALTER should be reserved for tables, functions and stuff, not altering entries in a table. That's the Update statement. (sorry to rant)
    – dengar81
    Commented Nov 1, 2015 at 22:20
  • @Shivan Paw: Two warnings I would expect: mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. - I would consider that the password is updated. And indeed I can log on with the new password. However, I still cannot run any queries, as I need to update the password with the ALTER USER command :(.
    – dengar81
    Commented Nov 1, 2015 at 22:25
  • @Shivan Paw: Try what? I already tried using the commands you suggested. Resetting the password using the ./mysqladmin command (two warnings) and then proceeding to run the update on mysql.user... The second command fails with the message "You must reset your password using ALTER USER [...]"
    – dengar81
    Commented Nov 1, 2015 at 23:10
  • This is the only solution that worked for me. if you have an expired password in the latest version of mysql it won't let you ALTER_USER or SET_PASSWORD. Commented Dec 1, 2015 at 16:17
  • this mysqladmin works for me after I killed the PID file Commented Nov 25, 2021 at 0:40
9

Maybe try that ?

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

or

SET PASSWORD FOR 'root'@'%' = PASSWORD('XXX');

Depending on which access you use.

(and not sure you should change yourself field names...)

https://dev.mysql.com/doc/refman/5.0/en/set-password.html

1
  • No sorry, this does not work: ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    – dengar81
    Commented Nov 1, 2015 at 21:43
7

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

Use this line...

6

On Ver 14.14 Distrib 5.7.19, for macos10.12 (x86_64), I logged in as: mysql -uroot -p then typed in the generated password by MySQL when you install it. Then..

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

Example:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Ab1234'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye $ mysql -uroot -p

And you can type in 'Ab1234'

5

Mysql 5.7.24 get root first login

step 1: get password from log

 grep root@localhost /var/log/mysqld.log
    Output
        2019-01-17T09:58:34.459520Z 1 [Note] A temporary password is generated for root@localhost: wHkJHUxeR4)w

step 2: login with him to mysql

mysql -uroot -p'wHkJHUxeR4)w'

step 3: you put new root password

SET PASSWORD = PASSWORD('xxxxx');

you get ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

how fix it?

run this SET GLOBAL validate_password_policy=LOW;

Try Again SET PASSWORD = PASSWORD('xxxxx');

0
3

This worked for me:

ALTER USER USER() IDENTIFIED BY 'auth_string';

I found it here: http://dev.mysql.com/doc/refman/5.7/en/alter-user.html#alter-user-current

1
  • ERROR 29 (HY000): File './mysql/user.MYD' not found (Errcode: 13 - Permission denied)
    – Ben
    Commented Dec 13, 2016 at 10:33
3

Here is the way works for me.

mysql> show databases ;

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> uninstall plugin validate_password;

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> alter user 'root'@'localhost' identified by 'root';

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.03 sec)

2

In order to chage user's password:

  • Firstly, login by root $sudo mysql

  • Change user's password

    ALTER USER 'user'@'localhost' IDENTIFIED BY 'newPassword';

1
  • 2
    miss spell on "INDENTIFIED" --> " IDENTIFIED" Commented Jul 6, 2022 at 13:05
1

When you use SET PASSWORD = PASSWORD('your_new_password'); it may crash for it

(ERROR 1819 (HY000): Your password does not satisfy the current policy requirements)

.you can use SET GLOBAL validate_password_policy=LOW; to slove it.

1

in 5.7 version. 'password' field has been deleted. 'authentication_string' replace it

use mysql;
update user set authentication_string=password('123456') where user='root';
flush privileges;
0

I also got the same problem in mac OS X 10.10.4(Yosemite).SET PASSWORD work for me.Alter password for mysql- mysql> SET PASSWORD = PASSWORD('your_password'); Query OK, 0 rows affected, 1 warning (0.01 sec)

set your Mysql environment path variable in .bash_profile and add the below line
export PATH=$PATH:/usr/local/mysql/bin, after that, run the following command :source .bash_profile

0

Had the problem after I installed mysql workbench and mysql community server. Got generated password during the server installation. Double clicking on the exisiting local instance in workbench triggered a dialog where I could set the new password.

0
0

mysql> SET PASSWORD = PASSWORD('your_new_password'); That works for me.

0

remember versions 5.7.23 and up - the user table doesn't has column password instead authentication string so below works while resetting password for a user.

update user set authentication_string=password('root') where user='root';
0

In mysql 5.7.x(replication) this issue can happen because the on the slave user either the password is unmatched or the root user does not exists on master.

For example(in my case):

I created an instance, altered password for the root and before replicating the slave, I dropped the root user. When I made the slave instance this alert appeared on the query: (because I altered the password on master before dropping the root user, and slave was reading from the first log which was actually an alter root user statement)

show slave status\G

So there are 2 solutions that can be applied here:

  1. you can create root user on master host and slave host with same password
  2. you can run the query on master hostreset master;

and reconnect slave to master.

0

i had same problem. access denied..

so, my solution is,

go to

mysql> USE mysql;
mysql> SELECT User, Host, plugin FROM mysql.user;

you can see plug in column name

i changed plugin from unix_socket -> to mysql_native_password

so, type this

mysql > use mysql;
mysql> update user set plugin='mysql_native_password' where user='root';
mysql> flush privileges;

done. my version so 10.1.48 MariaDB ubuntu 18

-1

The command CREATE USER 'test'@'localhost' IDENTIFIED WITH mysql_native_password BY '123'; fixes the problem by creating the user with the specified password.

1
  • How does CREATE USER solve the problem using ALTER USER?
    – Chris
    Commented Mar 31 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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