解決MySQL不需要密碼就能登錄問題 mysql出現ERROR 1698 (28000): Access denied for user 'root'@'localhost'錯誤解決方法


解決MySQL不需要密碼就能登錄問題

 

因為執行了一個更改數據庫root用戶密碼的命令,當我更改完后,發現用我新密碼和舊密碼都能登陸,於是感覺沒有輸密碼,直接回車就能登錄,而我在配置中也沒有進行免密碼登陸的操作,最后,執行了一條命令解決update user set plugin = "mysql_native_password";

修改密碼及解決無密碼登陸問題都在下面命令中:

 
> use mysql;

> update user set authentication_string=password("你的密碼") where user='root';  #(無password字段的版本,也就是版本<=5.7的)
> update user set password=password('你的密碼') where user='root'; #(有password字段的版本,版本>5.7的)

> update user set plugin="mysql_native_password"; 

> flush privileges;

> exit;

 




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

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



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

保存:wq,退出。輸入:service mysql restart,重新啟動mysql

 

 

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

 

  1. 1 use mysql; 然后敲回車
  2.  
    2 update user set authentication_string=password("你的密碼") where user="root"; 然后敲回車
  3.  
    3 flush privileges; 然后敲回車
然后輸入quit,退出mysql。


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

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

step4:如果此時還是報出錯誤,那么就需要返回step3中,把注釋掉的那條語句重新生效(就是刪除#符號),重新進入mysql中,先選擇一個數據庫(use mysql;),然后輸入select user,plugin from user;,看下圖:

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

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

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

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



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

我的操作系統是ubuntu18.04,以下是我的mysql版本:

安裝完成后,登錄mysql的時候就出現了如下錯誤:

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

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

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

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

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

保存:wq,退出。輸入:service mysql restart,重新啟動mysql。

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

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

結果如下圖:

然后輸入quit,退出mysql。

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

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM