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

当前回答

对于DBeaver用户:

右键单击连接,选择“编辑连接” 在“连接设置”界面(主界面)点击“编辑驱动程序设置” 点击“连接属性”(在最近的版本中,它被命名为“驱动程序属性”) 右键单击“用户属性”区域,选择“添加新属性” 添加两个属性:"useSSL"和"allowPublicKeyRetrieval" 通过双击“value”列,将其值设置为“false”和“true”

其他回答

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

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

另一种方法,在DBeaver上。

您可以编辑数据库的连接,在连接设置中进入SSL选项卡。这里有一个“允许公钥检索”的复选框,将其标记为真。那问题就解决了。

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>

打开DBeaver->编辑连接->查找驱动程序属性->allowPublicKeyRetrieval=true和useSSl=true

我在spring引导框架上使用下面的配置解决了这个问题

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