MySQL从5.6升级到5.7后无法登录root用户

MySQL从5.6升级到5.7后无法登录root用户

将 MySQL 从 5.6 升级到 5.7 后,我无法以 root 身份登录:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

我在日志中发现以下内容:

2016-10-26T10:23:01.845088Z 0 [Warning] User entry 'root'@'localhost' has an empty plugin value. The user will be ignored and no one can login with this user anymore.
2016-10-26T10:23:01.845112Z 0 [Warning] User entry 'mysql.sys'@'localhost' has an empty plugin value. The user will be ignored and no one can login with this user anymore.
2016-10-26T10:23:01.845127Z 0 [Warning] User entry 'debian-sys-maint'@'localhost' has an empty plugin value. The user will be ignored and no one can login with this user anymore.
2016-10-26T10:23:01.845142Z 0 [Warning] User entry 'phpmyadmin'@'localhost' has an empty plugin value. The user will be ignored and no one can login with this user anymore.
2016-10-26T10:23:01.845155Z 0 [Warning] Some of the user accounts with SUPER privileges were disabled because of empty mysql.user.plugin value. If you are upgrading from MySQL 5.6 to MySQL 5.7 it means we were not able to substitute for empty plugin column. Probably because of pre 4.1 password hash. If your account is disabled you will need to:
2016-10-26T10:23:01.845183Z 0 [Warning] 1. Stop the server and restart it with --skip-grant-tables.
2016-10-26T10:23:01.845192Z 0 [Warning] 2. Run mysql_upgrade.
2016-10-26T10:23:01.845200Z 0 [Warning] 3. Restart the server with the parameters you normally use.
2016-10-26T10:23:01.845207Z 0 [Warning] For complete instructions on how to upgrade MySQL to a new version please see the 'Upgrading MySQL' section from the MySQL manual
2016-10-26T10:23:01.853461Z 0 [Note] Event Scheduler: Loaded 0 events
2016-10-26T10:23:01.853962Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.16-0ubuntu0.16.04.1'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)
2016-10-26T10:23:02.138961Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)

所以我按照建议做了:停止服务器:

> service mysql stop

从跳过授权表开始:

> sudo mysqld_safe --skip-grant-tables

当 mysqld_safe 使用 --skip-grant-tables 运行时,我可以以 root 身份登录,但只能在关闭它运行的终端之前(它不是作为守护进程运行,而是在关闭终端时停止)。

然后我按照建议运行 mysql_upgrade (在另一个终端中)

> sudo mysql_upgrade

这升级了我的桌子。

我关闭了运行 mysqld_safe 的终端,停止了 mysql 服务器(service mysql stop)。我必须杀死 mysql 的所有实例,否则我会收到以下错误的完整日志:

2016-10-26T10:40:54.073975Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2016-10-26T10:40:54.074060Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.

然后我再次启动MySQL:

> service mysql start

并得到与上面相同的结果(用户“root”@“localhost”的访问被拒绝(使用密码:NO))

我如何解决它?我已经为此苦苦挣扎了几个小时,非常感谢任何帮助!

答案1

当我将 Ubuntu 从 14.04 升级到 16.04 时,我遇到了同样的问题。我懒得做你的建议(导出数据库并重新开始),所以我最终这样做是为了解决问题!

停止 mysql,然后在没有授权的情况下启动

> sudo service stop mysql 
> sudo mkdir /var/run/mysqld # needed to launch mysql in ubuntu from command line...may not apply to other distros
> sudo chown mysql:mysql /var/run/mysqld # same reason as above
> sudo mysqld_safe --skip-grant-tables

然后,在另一个终端窗口中:

> mysql -uroot 
> select User, host, authentication_string,plugin from mysql.user; #see what users don't have a password (authentication_string and plugin are empty) 
> update mysql.user set authentication_string=PASSWORD('woot') where user='user';
> update mysql.user set plugin='mysql_native_password' where user='user'; 
> FLUSH PRIVILEGES;
> exit; 

最后,我只需要杀死临时 mysql (mysqld_safe) 实例并重新启动它......在第一个终端上

> ^\ #to tell the mysqld_safe process to quit itself 
> sudo service mysqld start 

答案2

问题是 mysql.user 插件值对于包括 debian-sys-maint 在内的所有用户来说都是空的,所以我什至无法重新配置该包。它应该通过运行 mysql_upgrade 来修复,但由于某种原因它没有。

所以我转储了所有数据库,完全删除了 MySQL 服务器,然后重新安装它并再次导入我的数据库。

转储所有数据库:

> mysqldump -u root -p --databases db1 db2 > alldb.sql

这对我来说相当愚蠢,但我使用 --all-databases 转储了所有数据库。它包括 mysql.user 表,其中所有用户都有空的“插件”值,因此我在第一次重新启动 mysql 服务后遇到了同样的问题。所以后来我只为我的数据库进行了转储。

转储将放置在您运行命令的文件夹中

删除 MySQL 及其设置:

> sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.7 mysql-client-core-5.7
> sudo rm -rf /etc/mysql /var/lib/mysql
> sudo apt-get autoremove
> sudo apt-get autoclean

安装MySQL:

> sudo apt-get install mysql-server

导入您的数据库:

> mysql -u root -p < alldb.sql

相关内容