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

其他回答

更新useSSL=true在春季启动应用程序连接mysql;

jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=true&useLegacyDatetimeCode=false&serverTimezone=UTC

对于DBeaver用户:

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

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

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

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

例子:

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

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

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

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

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

什么修复它为我是做项目->清洁在Eclipse和清洁在我的Tomcat服务器。其中一个(或两个)解决了问题。

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