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

2022-08-31 05:00:08

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

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)
 

这是我的连接管理器类:

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

答案 1

您应该将客户端选项添加到 mysql 连接器,以允许客户端自动从服务器请求公钥。请注意,可能允许恶意代理执行MITM攻击以获取纯文本密码,因此默认情况下它是False,必须显式启用。allowPublicKeyRetrieval=trueallowPublicKeyRetrieval=True

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

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

例:

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

答案 2

对于 DBeaver 用户:

  1. 右键单击您的连接,选择“编辑连接”

  2. 在“连接设置”屏幕(主屏幕)上,单击“编辑驱动程序设置”

  3. 单击“连接属性”,(在最新版本中,它命名为“驱动程序属性”")

  4. 右键单击“用户属性”区域,然后选择“添加新属性”

  5. 添加两个属性:“useSSL”和“allowPublicKeyRetrieval”

  6. 通过双击“值”列将其值设置为“false”和“true”