我尝试使用连接器8.0.11将MySql数据库与Java连接。一切似乎都没问题,但我有这个例外:
线程“main”中的异常java.sql.SQLNonTransientConnectionException:不允许公钥检索
堆栈跟踪:
Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:444) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) at com.mysql.cj.jdbc.MysqlDataSource.getConnection(MysqlDataSource.java:438) at com.mysql.cj.jdbc.MysqlDataSource.getConnection(MysqlDataSource.java:146) at com.mysql.cj.jdbc.MysqlDataSource.getConnection(MysqlDataSource.java:119) at ConnectionManager.getConnection(ConnectionManager.java:28) at Main.main(Main.java:8)
Connector Manager:
public class ConnectionManager {
public static final String serverTimeZone = "UTC";
public static final String serverName = "localhost";
public static final String databaseName ="biblioteka";
public static final int portNumber = 3306;
public static final String user = "anyroot";
public static final String password = "anyroot";
public static Connection getConnection() throws SQLException {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUseSSL( false );
dataSource.setServerTimezone( serverTimeZone );
dataSource.setServerName( serverName );
dataSource.setDatabaseName( databaseName );
dataSource.setPortNumber( portNumber );
dataSource.setUser( user );
dataSource.setPassword( password );
return dataSource.getConnection();
}
}
您应该将客户机选项添加到mysql-connector allowPublicKeyRetrieval=true
https://mysql-net.github.io/MySqlConnector/connection-options/
也可以帮助添加useSSL=false
如果在连接mysql(运行mysql的本地或mysql容器)时收到以下错误:
java.sql.SQLNonTransientConnectionException:不允许公钥检索
解决方案:在数据库服务中添加以下行:
Clean
使用jdbc url
作为:
jdbc:mysql://localhost:3306/Database_dbName?allowPublicKeyRetrieval=true&useSSL=false;
qazxsw poi在您的配置中可能有所不同
除了建议的答案,您可以尝试使用mysql_native_password身份验证插件而不是caching_sha2_password身份验证插件。
PortNo: 3306
我在spring boot框架上使用以下配置解决了这个问题
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password_here';
在我的情况下,这是用户错误。我使用密码无效的spring.datasource.url=jdbc:mysql://localhost:3306/db-name?useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
用户。我不知道为什么我没有得到auth错误,而是收到了这个神秘的消息。
在我的情况下,上述错误实际上是由于错误的用户名和密码。解决问题:1。转到DriverManager.getConnection行(“jdbc:mysql:// localhost:3306 /?useSSL = false”,“username”,“password”);用户名和密码字段可能有误。输入用于启动mysql客户端的用户名和密码。用户名通常是root,密码是当与此类似的屏幕出现root
时输入的字符串
注意:在您的情况下,端口名称3306可能会有所不同。
此解决方案适用于MacOS Sierra,并运行MySQL 8.0.11版。请确保您已在构建路径中添加了驱动程序 - “添加外部jar”应与SQL版本匹配。
Startup screen of mysql
我发现这个问题令人沮丧,因为我昨天能够与数据库进行交互,但今天早上回来后,我开始收到此错误。
我尝试添加String url = "jdbc:mysql://localhost:3306/syscharacterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
标志,但我一直收到错误。
为我修复的是在Eclipse中执行allowPublicKeyRetrieval=true
和在Tomcat服务器上执行Project->Clean
。其中一个(或两个)修复它。
我不明白为什么,因为我使用Maven构建我的项目,并在每次代码更改后重新启动我的服务器。很烦人......
将连接URL指定为jdbc:mysql:// localhost:3306 / hb_student_tracker?allowPublicKeyRetrieval = true&useSSL = false&serverTimezone = UTC