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 伺服器,然後重新安裝它並再次匯入我的資料庫。

轉儲所有資料庫:

> 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

相關內容