MySql數據庫拒絕訪問解決辦法

遠程mysql_java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect

操作步驟也是很簡單的:
1 進入你的數據庫
$ /usr/local/mysql/bin/mysql -u root -p按Ener,輸入你的密碼
2 成功進入之後依次操作
mysql> user mysql; (分號不要忘記),按Ener
mysql> use mysql; (分號不要忘記),按Ener

mysql> select host from user;(分號不要忘記),按Ener
mysql> update user set host ='%' where user ='root';

mysql> exit; 退出MySql模式。
3 進入計算機的服務界面,重新啓動mysql服務就搞定啦。
擴展:
1 成功連接後,出現如下警告:
Mon Apr 04 15:43:00 CST 2016 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
這是警告不是錯誤,以後使用是不影響的。大概的意思就是說建立ssl連接,但是服務器沒有身份認證,這種方式不推薦使用。
解決辦法:
  原來的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "letmein");
  現在的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false","root", "letmein");
            // 連接URL爲 jdbc:mysql//服務器地址/數據庫名 ,後面的2個參數分別是登陸用戶名和密碼
如果是使用Spring框架則需要把application.propeties配置文件的mysql路徑改爲如下:
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&AutoReconnect=true&characterEncoding=utf-8&useSSL=false
參考:
發佈了161 篇原創文章 · 獲贊 116 · 訪問量 63萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章