編碼的世界 / 優質文選 / 歷史

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


2022年5月22日
-   

遠程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
參考:
https://www.cnblogs.com/hongten/archive/2012/10/25/mysql.html

熱門文章