我尝试使用连接器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)
 

下面是我的Connection 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();
    }
}

当前回答

给出连接URL jdbc: mysql: / / localhost: 3306 / hb_student_tracker吗?allowPublicKeyRetrieval = true&useSSL = false&serverTimezone = UTC

其他回答

当从DBeaver这样做时,我必须去“连接设置”->“SSL”选项卡,然后:

取消选中“验证服务器证书” 检查“允许公钥检索”

这就是它的样子。

注意,这只适用于本地开发。

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

请参阅MySQL .NET连接字符串选项

您也可以尝试添加useSSL=false当您使用它进行测试/开发的目的

例子:

jdbc:mysql://localhost:3306/db?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.url=jdbc:mysql://localhost:3306/database?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false

您可以将这一行插入到应用程序中。属性文件,这意味着,

spring.datasource.url=jdbc:mysql://localhost:3306/ This one uses mysql as the database service. I think this can changed by using relavent name and the port of your database name. database?createDatabaseIfNotExist=true = use the database named database if you haven't already make a database like that, make a new one. allowPublicKeyRetrieval=true = to allow the client to automatically request the public key from the server. (This part might be additional) useSSL=false = This will disable SSL and also suppress the SSL errors

此外,要警惕同一文件中的spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect属性。

最后检查是否在pom.xml文件的dependencies中添加了以下依赖项。

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
</dependency>

在对现有应用程序进行dockerization时,我也遇到过这样的问题。解决方案是在JDBC连接字符串中添加MySQL的allowPublicKeyRetrieval连接选项,值为true。 如果这是不工作,尝试添加useSSL选项为false以及。

结果字符串看起来像这样:

jdbc:mysql://<database server ip>:3306/databaseName?allowPublicKeyRetrieval=true&useSSL=false

当我遇到根帐户“不允许公开密钥检索”的问题时,我更新了这个参数。