mysql - 无法使用 UPDATE 更改 MySQL 上的用户密码

标签 mysql change-password

在 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.user 表上使用 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.

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

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 命令必须如下所示,使用正确的列和 FLUSH PRIVILEGES声明:

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

关于mysql - 无法使用 UPDATE 更改 MySQL 上的用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218479/

相关文章:

mysql - 使用列表连接表

mysql - 从数据库中删除多个值

Azure AD B2C 密码更改自定义策略,用户每次都需要登录

spring - 创建帐户、忘记密码和更改密码

php - 有条件地运行 php 代码

php - 输入用户凭据时无法重定向登录页面

ssl - 在 Payara/Glassfish 服务器上更改主密码

php - Magento 停留在 "It' 秒的时间来更改您的密码。”

php - 在 MySql 中订购多个表

python - 使用 Python (sftp) 更改远程 Linux 系统的密码