首页 > 解决方案 > MySQL密码不会更新

问题描述

我尝试使用我在 Google 上找到的任何内容来重置我的 MySQL 密码,但是它系统地失败了。这是我尝试的三件事:

$ read -s password
$ echo ${#password}
10

$ echo "use mysql; update user set authentication_string=password('$password') where user='root';" | sudo mysql -uroot
$ echo "use mysql; select authentication_string from user where user='root';"  | sudo mysql -uroot
authentication_string
*B69D82770841AB22761D61C3C7DED4FBE78D99C7
$ mysql -uroot "-p$password"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

$ sudo mysqladmin -u root password "$password"
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.
$ mysql -uroot "-p$password"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

$ echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('$password');" | sudo mysql -uroot
$ mysql -uroot "-p$password"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

我也在更新和测试之间尝试了所有这三个echo "flush privileges" | sudo mysql -uroot,但仍然Access denied.

这是我的mysql版本

$ sudo mysql --version
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

为什么更新我的密码对这三种方式没有影响?

编辑

我也试过(因为我使用> = 5.7.6):

$ echo "ALTER USER 'root'@'localhost' IDENTIFIED BY '$password';"  | sudo mysql -uroot
$ echo "flush privileges; "  | sudo mysql -uroot
$ mysql -uroot "-p$password"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

编辑 2

我也尝试过,按照https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

$ echo "ALTER USER 'root'@'localhost' IDENTIFIED BY '$password';" > changepw.sql
$ chmod 777 changepw.sql 
$ sudo service mysql stop
$ sudo mysqld --init-file=`pwd`/changepw.sql &
$ sudo service mysql restart
$ mysql -uroot "-p$password"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

标签: mysqlchange-passwordubuntu-18.04

解决方案


显然,我需要确保mysql_native_password使用了该插件,然后才能ALTER USER正常工作。

$ read -s password
$ echo ${#password}

$ echo "use mysql; update user set plugin='mysql_native_password'; FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY '$password'; " | sudo mysql -uroot -p

我在这里找不到任何关于ALTER USER不工作的默认行为:

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

也没有提到插件,我也不知道这是否是 Ubunt 18 特有的。所以我怀疑这个解决方案是否足够,但对我来说它有效。


推荐阅读