ERROR 1698 (28000): Access denied for user 'root'@'localhost' (後面沒有多餘的東西) -------解決方法

這個問題很多安裝配置教程裏都沒有提到,找了好久好久。。。
轉載一下。

轉載:https://www.linuxidc.com/Linux/2019-08/159900.htm


測試的Linux操作系統是Ubuntu 18.04 LTS,MySQL版本如下:

linuxidc@linuxidc:~/www.linuxidc.com$ mysql --version
mysql Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using EditLine wrapper

在這裏插入圖片描述
安裝完成後,登錄MySQL時出現如下錯誤:

linuxidc@linuxidc:~/www.linuxidc.com$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user ‘root’@‘localhost’

在這裏插入圖片描述

因爲安裝的過程中沒讓設置密碼,可能密碼爲空,但無論如何都進不去MySQL。

那麼該怎麼做呢,接下來就將這個解決方法總結記錄一下。

第1步:
在Ubuntu終端輸入如下命令

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

進入到mysqld.cnf配置文件,然後在這個配置文件中的[mysqld]這一塊中加入skip-grant-tables這句話。

[mysqld]

# * Basic Settings

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
skip-grant-tables

在這裏插入圖片描述

作用:就是讓你可以不用密碼登錄進去MySQL。

保存:wq,退出。

輸入如下命令:

linuxidc@linuxidc:~/www.linuxidc.com$ service mysql restart

重新啓動MySQL。如下圖:

在這裏插入圖片描述

第2步:
在Ubuntu終端上輸入

linuxidc@linuxidc:~/www.linuxidc.com$ mysql -u root -p

遇見輸入密碼的提示直接回車即可,進入MySQL後,分別執行下面三句話:

linuxidc@linuxidc:~/www.linuxidc.com$ use mysql; #然後回車

linuxidc@linuxidc:~/www.linuxidc.com$ update user set authentication_string=password(“linuxidc”) where user=“root”; #然後回

說明:本例的密碼是linuxidc

linuxidc@linuxidc:~/www.linuxidc.com$ flush privileges; #然後回車

結果如下圖:

在這裏插入圖片描述

然後輸入quit,退出MySQL

第3步:
重新進入到mysqld.cnf文件中去把剛開始加的skip-grant-tables這條語句給註釋掉。如下圖:

在這裏插入圖片描述
再返回終端輸入mysql -u root -p,應該就可以進入數據庫了。

第4步:
如果此時還是報出錯誤如下

linuxidc@linuxidc:~/www.linuxidc.com$ mysql -u root -p
Enter password:
ERROR 1524 (HY000): Plugin ‘auth_socket’ is not loaded

在這裏插入圖片描述

那麼就需要返回第3步中,把註釋掉的那條語句重新生效(就是刪除#符號),重新進入mysql中,先選擇一個數據庫(use mysql;),然後輸入select user,plugin from user;,看下圖:

在這裏插入圖片描述

從圖中可以看到在執行了select user,plugin from user;後,錯誤原因是因爲plugin root的字段是auth_socket,那我們改掉它爲下面的mysql_native_password就行了。輸入:

update user set authentication_string=password(“linuxidc”),plugin=‘mysql_native_password’ where user=‘root’;

然後回車執行以下,再輸入select user,plugin from user;回車,我們能看到root用戶的字段改成功了。如下圖:

在這裏插入圖片描述

最後quit退出。返回執行第3步。

那麼這個問題就完全解決了。

拓展更新:
在MySQL 8版本中,上面更新代碼的語句似乎有所變化,那個句法會被告知是錯誤的,這裏我貼一下沒有語法錯誤的:

ALTER user ‘root’@‘localhost’ IDENTIFIED BY ‘newpassward’; //newpassward 新密碼

將這句話對應到上面第二步即可。

如果執行本語句出現The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen這個錯誤,解決如下:

先flush privileges,然後再執行上面修改密碼的語句。


剛好下面的錯誤我也遇到了。完美。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章