我尝试使用连接器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这样做时,我必须去“连接设置”->“SSL”选项卡,然后:

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

这就是它的样子。

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

其他回答

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

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

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

首先,请确保您的数据库服务器已启动并运行。 我得到了同样的错误,在尝试了这里列出的所有答案后,我发现我的数据库服务器没有运行。

您可以从MySQL工作台或命令行中检查相同的使用

mysql -u USERNAME -p

这听起来很明显,但很多时候我们假设数据库服务器一直在运行,特别是当我们在本地机器上工作时,当我们重新启动/关闭机器时,数据库服务器将自动关闭。

在MySQL 8.0中,默认的身份验证插件由mysql_native_password更改为caching_sha2_password。 有关此更改的更多信息,请参阅https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password。

这意味着为了使用caching_sha2_password,连接必须执行以下操作之一:

使用安全连接(useSSL=true) 使用未加密的连接,支持使用RSA密钥对交换密码(useSSL=false和其他配置参数-见https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html)

你有几个选择:

ALTER the users to use the mysql_native_password plugin (like how it was doing historically and will also work with older clients / connections which don't support caching_sha2_password) useSSL=true useSSL=false and configure public key retrieval (this doesn't mean using allowPublicKeyRetrieval=true which would avoid the error - but defeats the objective of this extra security and is slow - it does mean using something like server-public-key-path to point to the client side copy of the public key)

这也可能发生由于错误的用户名或密码。 作为解决方案,我添加了allowPublicKeyRetrieval=true&useSSL=false部分,但我仍然得到了错误,然后我检查了密码,它是错误的。

我的问题是在pom.xml (spring boot)中。 我的pom.xml有两个不同数据库的依赖项。确保只保留MySQL依赖项,并删除任何其他数据库依赖项。