1. 程式人生 > 資料庫 >解決mysql登入錯誤:'Access denied for user 'root'@'localhost'

解決mysql登入錯誤:'Access denied for user 'root'@'localhost'

首先是不知道怎麼忽然mysql用命令列,workbench都登入不了,都提示'Access denied for user 'root'@'localhost'。
資料庫解除安裝重灌了幾次都不行。好像感覺資料清理不乾淨。解決的過程遇到的坑,這裡記錄分享下。

有效的操作記錄下:

1、首先是跳過許可權登入mysql,檢視user表,

停止mysql服務~$ sudo service mysql stop

以安全模式啟動MySQL~$ sudo mysqld_safe --skip-grant-tables &

注意:

可能提示mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exist

解決方法:(筆者嘗試發現必須加sudo)

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

再次執行上面安全模式啟動mysql ~$ sudo mysqld_safe --skip-grant-tables &

這次提示語句顯示了,mysqld_safe Staring mysqld deamon with database from /var/lib/mysql

這次登入可以不用密碼:mysql -u root 回車就登入了

sql語句查詢mysql.user表的情況,如下:

加圖片(以後補)

還有人提議如下這麼做,沒嘗試

Open & Edit /etc/my.cnf or /etc/mysql/my.cnf,depending on your distro.
Add skip-grant-tables under [mysqld]

2、發現user的plugin為socket_plugin,改為mysql_native_password

修改root的plugin許可權:

update mysql.user set authentication_string=PASSWORD('newPwd'),plugin='mysql_native_password' where user='root';
flush privileges;
quit;

(注意這裡修改一定要正確,免得跟筆者一樣,粗心的把plugin修改的內容少些了一個字母,又有下面的折騰)

3、user表還有其他使用者,root不能登入,用其他的使用者登入也行啊。查詢了發現一下有效操作:

在mysql的安裝目錄,一般在 /etc/mysql裡面有個debain.cnf檔案,裡面有user,password,用這個用了登入,密碼最後複製,然後就又可以修改user表root的plugin,操作同上。

重啟mysql服務,sudo service mysql restart;

用root使用者就可以登入。

參考部落格:

MySQL ERROR 1698 (28000) 錯誤 https://www.jb51.net/article/117566.htm

mysql 檢視當前使用的配置檔案my.cnf的方法 https://www.jb51.net/article/110395.htm

linux -- Ubuntu檢視修改mysql的登入名和密碼、安裝phpmyadmin https://www.jb51.net/article/174925.htm

總結

以上所述是小編給大家介紹的解決mysql登入錯誤:''Access denied for user ''root''@''localhost'',希望對大家有所幫助!