mysql出現ERROR1698(28000):Access denied for user root@localhost錯誤解決方法

個人操做系統是ubuntu18.04,如下是個人mysql版本:html

安裝完成後,登陸mysql的時候就出現了以下錯誤:mysql

由於安裝的過程當中沒讓設置密碼,可能密碼爲空,但不管如何都進不去mysql。sql

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

step1:在ubuntu的terminal(也即終端)上輸入sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf,進入到這個配置文件,而後在這個配置文件中的[mysqld]這一塊中加入skip-grant-tables這句話。ubuntu

 1 [mysqld]
 2 #
 3 # * Basic Settings
 4 #
 5 user              = mysql
 6 pid-file          = /var/run/mysqld/mysqld.pid
 7 socket           = /var/run/mysqld/mysqld.sock
 8 port              = 3306
 9 basedir          = /usr
10 datadir          = /var/lib/mysql
11 tmpdir          = /tmp
12 lc-messages-dir   = /usr/share/mysql
13 skip-external-locking
14 character-set-server=utf8
15 collation-server=utf8_general_ci
16 skip-grant-tables    <-- add here

做用:就是讓你能夠不用密碼登陸進去mysql。vim

保存:wq,退出。輸入:service mysql restart,從新啓動mysql。socket

step2:在終端上輸入mysql -u root -p,碰見輸入密碼的提示直接回車便可,進入mysql後,分別執行下面三句話:ui

1 use mysql;   而後敲回車
2 update user set authentication_string=password("你的密碼") where user="root";  而後敲回車
3 flush privileges;  而後敲回車

結果以下圖:this

而後輸入quit,退出mysql。spa

step3:從新進入到mysqld.cnf文件中去把剛開始加的skip-grant-tables這條語句給註釋掉。

 1 [mysqld]
 2 #
 3 # * Basic Settings
 4 #
 5 user              = mysql
 6 pid-file          = /var/run/mysqld/mysqld.pid
 7 socket           = /var/run/mysqld/mysqld.sock
 8 port              = 3306
 9 basedir          = /usr
10 datadir          = /var/lib/mysql
11 tmpdir          = /tmp
12 lc-messages-dir   = /usr/share/mysql
13 skip-external-locking
14 character-set-server=utf8
15 collation-server=utf8_general_ci
16 # skip-grant-tables    <-- add # here

再返回終端輸入mysql -u root -p,應該就能夠進入數據庫了。

step4:若是此時仍是報出錯誤,那麼就須要返回step3中,把註釋掉的那條語句從新生效(就是刪除#符號),從新進入mysql中,先選擇一個數據庫(use mysql;),而後輸入select user,plugin from user;,看下圖:

從圖中能夠看到在執行了select user,plugin from user;後,錯誤緣由是由於plugin root的字段是auth_socket,那咱們改掉它爲下面的mysql_native_password就好了。輸入:

1 update user set authentication_string=password("ln122920"),plugin='mysql_native_password' where user='root';

而後回車執行如下,再輸入select user,plugin from user;回車,咱們能看到root用戶的字段改爲功了。

最後quit退出。返回執行step3。

那麼這個問題就徹底解決了。

參考連接:http://www.cnblogs.com/py1612919884/p/9327015.html

===========================================更新=================================================

拓展一下:

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

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,而後再執行上面修改密碼的語句。

相關文章
相關標籤/搜索