如果没有设置系统 root 密码,则无法更新 mysql

如果没有设置系统 root 密码,则无法更新 mysql

我正在尝试更新系统,但 mysql 5.7 失败。似乎由于身份验证问题无法连接。

以下是完整的控制台输出:

Reading package lists... Done
Building dependency tree
Reading state information... Done
mysql-server-5.7 is already the newest version (5.7.23-0ubuntu0.16.04.1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Setting up mysql-server-5.7 (5.7.23-0ubuntu0.16.04.1) ...
Renaming removed key_buffer and myisam-recover options (if present)
mysql_upgrade: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) while connecting to the MySQL server
Upgrade process encountered error and will not continue.
mysql_upgrade failed with exit status 11
dpkg: error processing package mysql-server-5.7 (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-server-5.7; however:
  Package mysql-server-5.7 is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 mysql-server-5.7
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

重要提示:

 mysql_upgrade: Got error: 1045: Access denied for user 'root'@'localhost'

我可以使用“mysql -u root -p”命令顺利登录。我不使用密码登录服务器,因为如果需要的话,我会使用 ssh 密钥。

为什么这个问题现在会出现?如何解决?我也已经有一段时间没有更新系统了,但这是唯一有问题的软件包。

答案1

您可以尝试通过 Unix Socket 启用无密码登录 mySQL,该功能通常默认启用,但也许您已经更改了它。

登录到mysqlroot shell 并运行以下命令:

grant usage on *.* to 'root'@'localhost' identified via unix_socket;

如果这不起作用,请尝试添加~/.my.cnf包含以下内容的文件:

[client]
user=root
password=your_password

使其仅所有者(和 root)可读:

chmod 600 ~/.my.cnf

然后重试。

相关内容