连接 Java-MySql:不允许公钥检索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50379839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:36:15  来源:igfitidea点击:

Connection Java-MySql : Public Key Retrieval is not allowed

javamysqlexceptionconnector

提问by danny

I try to connect MySql database with Java using connector 8.0.11. Everything seems to be ok but I have this exception:

我尝试使用连接器 8.0.11 将 MySql 数据库与 Java 连接。一切似乎都很好,但我有这个例外:

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

线程“main”中的异常 java.sql.SQLNonTransientConnectionException:不允许公钥检索

Stack Trace:

堆栈跟踪:

    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();
    }
}

采纳答案by jtomaszk

You should add client option to your mysql-connector allowPublicKeyRetrieval=trueto allow the client to automatically request the public key from the server. Note that AllowPublicKeyRetrieval=Truecould allow a malicious proxy to perform a MITM attack to get the plaintext password, so it is False by default and must be explicitly enabled.

您应该向 mysql-connector 添加客户端选项,allowPublicKeyRetrieval=true以允许客户端自动从服务器请求公钥。请注意,这AllowPublicKeyRetrieval=True可能允许恶意代理执行 MITM 攻击以获取明文密码,因此默认情况下为 False,必须显式启用。

https://mysql-net.github.io/MySqlConnector/connection-options/

https://mysql-net.github.io/MySqlConnector/connection-options/

you could also try adding useSSL=falsewhen you use it for testing/develop purposes

您也可以useSSL=false在将其用于测试/开发目的时尝试添加

example:

例子:

jdbc:mysql://localhost:3306/db?allowPublicKeyRetrieval=true&useSSL=false

回答by susan097

Use jdbc urlas :

使用 jdbc url如:

jdbc:mysql://localhost:3306/Database_dbName?allowPublicKeyRetrieval=true&useSSL=false;

PortNo: 3306can be different in your configuation

PortNo: 3306您的配置可能会有所不同

回答by shellhub

I solve this issue using below configuration on spring boot framework

我在 spring boot 框架上使用以下配置解决了这个问题

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

回答by Torsten Ojaperv

Alternatively to the suggested answers you could try and use mysql_native_passwordauthentication plugin instead of caching_sha2_passwordauthentication plugin.

除了建议的答案,您可以尝试使用mysql_native_password身份验证插件而不是caching_sha2_password身份验证插件。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password_here'; 

回答by surendrapanday

This solution worked for MacOS Sierra, and running MySQL version 8.0.11. Please make sure driver you have added in your build path - "add external jar" should match up with SQL version.

此解决方案适用于 MacOS Sierra,并运行 MySQL 8.0.11 版。请确保您在构建路径中添加了驱动程序 - “添加外部 jar”应与 SQL 版本匹配。

String url = "jdbc:mysql://localhost:3306/syscharacterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";

回答by juice

In my case it was user error. I was using the rootuser with an invalid password. I am not sure why I didn't get an auth error but instead received this cryptic message.

就我而言,这是用户错误。我使用的root用户密码无效。我不知道为什么我没有收到身份验证错误,而是收到了这条神秘的消息。

回答by risingStark

The above error in my case was actually due to the wrong username and password. Solving the issue: 1. Go to the line DriverManager.getConnection("jdbc:mysql://localhost:3306/?useSSL=false", "username", "password"); The fields username and password might be wrong. Enter the username and password which you use to start your mysql client. The username is generally root and password is the string which you enter when a screen similar to this appears Startup screen of mysql

在我的情况下,上述错误实际上是由于用户名和密码错误造成的。解决问题: 1. 转到 DriverManager.getConnection("jdbc:mysql://localhost:3306/?useSSL=false", "username", "password"); 字段用户名和密码可能是错误的。输入用于启动 mysql 客户端的用户名和密码。用户名一般是root,密码是你在mysql的启动画面出现类似画面时输入的字符串

Note: The portname 3306 might be different in your case.

注意:在您的情况下,端口名 3306 可能会有所不同。

回答by Cameron Hudson

I found this issue frustrating because I was able to interact with the database yesterday, but after coming back this morning, I started getting this error.

我发现这个问题令人沮丧,因为我昨天能够与数据库交互,但是今天早上回来后,我开始收到这个错误。

I tried adding the allowPublicKeyRetrieval=trueflag, but I kept getting the error.

我尝试添加allowPublicKeyRetrieval=true标志,但我一直收到错误消息。

What fixed it for me was doing Project->Cleanin Eclipse and Cleanon my Tomcat server. One (or both) of those fixed it.

为我修复它的是Project->Clean在 Eclipse 和Clean我的 Tomcat 服务器上做的。其中一个(或两个)修复了它。

I don't understand why, because I build my project using Maven, and have been restarting my server after each code change. Very irritating...

我不明白为什么,因为我使用 Maven 构建我的项目,并且在每次代码更改后都重新启动了我的服务器。很烦人...

回答by samit tiwary

Give connection URL as jdbc:mysql://localhost:3306/hb_student_tracker?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC

将连接 URL 指定为 jdbc:mysql://localhost:3306/hb_student_tracker?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC

回答by Rohtash Singh Lakra

If you are getting the following error while connecting the mysql (either local or mysql container running the mysql):

如果在连接 mysql(本地或运行 mysql 的 mysql 容器)时出现以下错误:

java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

java.sql.SQLNonTransientConnectionException:不允许公钥检索

Solution: Add the following line in your database service:

解决方案:在您的数据库服务中添加以下行:

command: --default-authentication-plugin=mysql_native_password