MySQL 密码不会更新

MySQL password won't update

我尝试使用我在 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'

显然,我需要确保使用了 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 特有的东西。所以我怀疑这个解决方案是否足够,但对我来说它有效。