92

I'm trying to update the password for a database user using the command line, and it's not working for me. This is the code I'm using:

mysql> UPDATE user SET password=PASSWORD($w0rdf1sh) WHERE user='tate256';

Could someone tell me what's wrong with this code.

3
  • Well what steps are you following? Simply putting it in won't work. If I remember correctly its a process and that code.
    – Idris
    Apr 1, 2014 at 0:37
  • Ah. Do you remember the process of how to do it? Apr 1, 2014 at 0:39
  • This is a bit more detailed and should be more easier to understand innovativethought.net/2007/05/17/…
    – Idris
    Apr 1, 2014 at 0:41

9 Answers 9

121

In your code, try enclosing password inside single quote. Alternatively, as per the documentation of mysql, following should work -

SET PASSWORD FOR 'jeffrey'@'localhost' = PASSWORD('cleartext password');

FLUSH PRIVILEGES;

The last line is important or else your password change won't take effect unfortunately.

EDIT:

I ran a test in my local and it worked -

mysql>  set password for 'test' = PASSWORD('$w0rdf1sh');
Query OK, 0 rows affected (0.00 sec)

Mine is version 5. You can use following command to determine your version -

SHOW VARIABLES LIKE "%version%";
6
  • 4
    When I do that, it's returning syntax error near unexpected token (' Apr 1, 2014 at 0:42
  • 1
    What version of mysql are you using? I picked up the above syntax from version 5.0 documentation. Did you try the command that I posted?
    – hellboy
    Apr 1, 2014 at 0:44
  • I believe I'm using a 4.0 version and yes I tried it Apr 1, 2014 at 0:47
  • Command works well for me in MySQL Ver 14.14, ensure the password is wrapped in quotes and any quotes in the password are escaped.
    – Snaver
    Jun 24, 2014 at 18:26
  • 1
    Strangely, even after flushing, still doesn't work for me :-( I'm using Mysql 5.7.18-0ubuntu0.16.04.1 Jul 7, 2017 at 3:42
43

As of MySQL 5.7.6, use ALTER USER

Example:

ALTER USER 'username' IDENTIFIED BY 'password';

Because:

  • SET PASSWORD ... = PASSWORD('auth_string') syntax is deprecated as of MySQL 5.7.6 and will be removed in a future MySQL release.

  • SET PASSWORD ... = 'auth_string' syntax is not deprecated, but ALTER USER is now the preferred statement for assigning passwords.

10
  • 1
    is this true even for interacting with MySQL via PHP?
    – oldboy
    Aug 7, 2017 at 2:39
  • @Anthony yes iff your MySQL version is >= 5.7.6
    – Govind Rai
    Aug 7, 2017 at 14:06
  • i'm new to MySQL. what does ALTER USER ... IDENTIFIED BY ... do exactly? how is that different from UPDATE ... SET ... or INSERT INTO ...?
    – oldboy
    Aug 7, 2017 at 19:02
  • 1
    ALTER USER ... IDENTIFIED BY ... is a SQL statement that configures user permissions for your database. UPDATE and INSERT have different functionalities, which like the names suggest, update records and insert records into tables in your database.
    – Govind Rai
    Aug 7, 2017 at 20:58
  • 2
    gives error in mariaDb ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USER 'root' IDENTIFIED BY Apr 16, 2019 at 9:49
14

Note: you should login as root user

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your password');
6

this is the updated answer for WAMP v3.0.6

UPDATE mysql.user 
SET authentication_string=PASSWORD('MyNewPass') 
WHERE user='root';

FLUSH PRIVILEGES;
3

Before MySQL 5.7.6 this works from the command line:

mysql -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('$w0rdf1sh');"

I don't have a mysql install to test on but I think in your case it would be

mysql -e "UPDATE mysql.user SET Password=PASSWORD('$w0rdf1sh') WHERE User='tate256';"
2

In windows 10, just exit out of current login and run this on command line

--> mysqladmin -u root password “newpassword”

where instead of root could be any user.

2

As of MySQL 8.0.18 This works fine for me

mysql> SET PASSWORD FOR 'user'@'localhost' = 'userpassword';

2

This works for me. Got solution from MYSQL webpage

In MySQL run below queries:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'New_Password';
-1

Your login root should be /usr/local/directadmin/conf/mysql.conf. Then try following

UPDATE mysql.user SET password=PASSWORD('$w0rdf1sh') WHERE user='tate256' AND Host='10.10.2.30';
FLUSH PRIVILEGES;

Host is your mysql host.

1
  • 1
    Doesn't work. Returning syntax error near unexpected token (' Apr 1, 2014 at 0:50

Your Answer

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

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