无法使用 UPDATE 在 MySQL 上更改用户密码

Can't change user password on MySQL using UPDATE

在 MySql 5.7.17 下,提到的指令不起作用,我总是得不到任何反馈或以下错误消息:

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 'UPDATE mysql.user SET password=password("elephant7") where user="root"' at line ...

我在命令行上尝试了以下 UPDATE

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

我真的不明白我的错误了。我也试过没有 ;.

不建议直接在mysql.usertable上使用UPDATE这种方式修改密码。您应该使用 SET PASSWORD 代替:

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

More information on MySQL: Assigning Account Passwords

您的 UPDATE 命令可能不起作用,因为 password 列被 authentication_string on MySQL 5.7.6.

取代

The authentication_string column in the mysql.user table now stores credential information for all accounts. The password column, previously used to store password hash values for accounts authenticated with the mysql_native_password and mysql_old_password plugins, is removed.

如果您直接更改授权 tables,您还必须使用 FLUSH PRIVILEGES 语句重新加载 tables:

If you modify the grant tables directly using statements such as INSERT, UPDATE, or DELETE (which is not recommended), the changes have no effect on privilege checking until you either tell the server to reload the tables or restart it. Thus, if you change the grant tables directly but forget to reload them, the changes have no effect until you restart the server. This may leave you wondering why your changes seem to make no difference!

To tell the server to reload the grant tables, perform a flush-privileges operation. This can be done by issuing a FLUSH PRIVILEGES statement.

source: When Privilege Changes Take Effect

因此,您的 UPDATE 命令直接更改授权 table 上的密码必须如下所示,使用正确的列和 FLUSH PRIVILEGES 语句:

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

使用此方法更改密码需要刷新权限才能生效。

flush privileges;