java.sql.SQLException: Access denied for user 'root'@'localhost'

原由:ERROR 1698 (28000): Access denied for user 'root'@'localhost'

環境: ubuntu 18.04 + Eclipse +mysql 5.7

1. 學習 sping boot 到了 javax.persistence.Entity 出錯!而後倒回去學習 JPA 
2. 先是遇到了 SSL 錯誤 ,簡單加上 url=jdbc:mysql://127.0.0.1:3306/framework?characterEncoding=utf8&useSSL=true ,或者 url=jdbc:mysql://127.0.0.1:3306/framework?characterEncoding=utf8&useSSL=false
3. 以前學習 java 和 jsp 的時候,都跳過去了 java 鏈接 mysql 部分,如今回頭去補上
4. 遇到錯誤: ERROR 1698 (28000): Access denied for user 'root'@'localhost' 
5. 從 jsp 退回到 java ,怎麼調整 url 和 property 都無論用,退回到 mysql
6. 由於現安裝了 mysql 5.7 ,每次啓動 mysql 時習慣性的加上了 sudo,沒有遇到錯誤,可是,如今在 eclipse 裏面,顯然是不可能去 sudo的
sudo mysql -uroot -p wzh20190228 正確!
mysql -uroot -p wzh20190228 也是錯誤!
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
7. 因此問題回到了 mysql 的 root

參考

http://www.javashuo.com/article/p-qjhgwbqx-u.html https://blog.csdn.net/a1027805380/article/details/70990821 http://www.javashuo.com/article/p-fwufxqhz-eh.htmlhtml

一、找到系統自動產生的客戶端帳戶

sudo cat /etc/mysql/debian.cnf
內容以下
	# Automatically generated for Debian scripts. DO NOT TOUCH!
	[client]
	host     = localhost
	user     = debian-sys-maint
	password = Ev6UOKtD58F3kQys
	socket   = /var/run/mysqld/mysqld.sock
	[mysql_upgrade]
	host     = localhost
	user     = debian-sys-maint
	password = Ev6UOKtD58F3kQys
	socket   = /var/run/mysqld/mysqld.sock

二、使用這個帳戶登陸 mysql

1. 無需中止 mysql 服務
2. mysql -u debian-sys-maint -p
	輸入以上系統產生的密碼 (個人是 Ev6UOKtD58F3kQys)
3. 切換到 mysql
	mysql>use mysql;
		...
		Database changed

4. mysql> show tables;
		...
		user

5. mysql> select user, plugin from mysql.user;
	+------------------+-----------------------+
	| user             | plugin                |
	+------------------+-----------------------+
	| root             | auth_socket           |
	| mysql.session    | mysql_native_password |
	| mysql.sys        | mysql_native_password |
	| debian-sys-maint | mysql_native_password |
	| root             | mysql_native_password |
	+------------------+-----------------------+
	5 rows in set (0.01 sec)

6. 重設 root 密碼 和認證方式 (這一步是關鍵)
	update mysql.user set authentication_string=PASSWORD('Wwzzhh[@123](https://my.oschina.net/u/9520)'),plugin='mysql_native_password' where user='root';
返回結果:
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2  Changed: 1  Warnings: 1

** 密碼要符合規定!我老是想簡單爲 12345678,反正是學習用
	ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
	
7. mysql> flush privileges;
8. quit
9. mysql -u root -p
 
** 以前學習的都是進入安全模式以後,重設 root 密碼,老是無論用!
** 感謝 https://www.cnblogs.com/leolztang/p/5094930.html 做者!
** 問題的關鍵不在 root 密碼 ,由於 sudo mysql -uroot -p wzh20190228 是能夠登陸的
** 問題關鍵在於 root 帳戶的認證方式:auth_socket

三、回到 java 程序

在這裏插入圖片描述

四、回到 jsp 程序

後續: JPA 、Spring boot 待處理

相關文章
相關標籤/搜索