问题
使用mybatis plus逆向生成实体类CodeGenerator远程连接mysql 8.0时报错
Exception in thread "main" java.lang.RuntimeException: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
at com.baomidou.mybatisplus.generator.config.DataSourceConfig.getConn(DataSourceConfig.java:170)
at com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder.<init>(ConfigBuilder.java:110)
at com.baomidou.mybatisplus.generator.AutoGenerator.execute(AutoGenerator.java:96)
at com.dev.docker.config.CodeGenerator.main(CodeGenerator.java:125)
Caused by: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at com.baomidou.mybatisplus.generator.config.DataSourceConfig.getConn(DataSourceConfig.java:168)
... 3 more
原因
官网:https://mysqlconnector.net/connection-options/
如果用户账号使用sha256_password认证,传输时必须保护密码; TLS 是首选机制,但如果它不可用,则将使用 RSA 公钥加密。 要指定服务器的 RSA 公钥,请使用 ServerRSAPublicKeyFile 连接字符串设置,或设置 AllowPublicKeyRetrieval=True 以允许客户端自动从服务器请求公钥。 请注意,AllowPublicKeyRetrieval=True 可能允许恶意代理执行 MITM 攻击以获取明文密码,因此默认情况下为 False,必须显式启用。
因为AllowPublicKeyRetrieval, Allow Public Key Retrieval这一项默认为false,所以需要在jdbc连接上增加"allowPublicKeyRetrieval=true"
解决方式
jdbc连接增加"allowPublicKeyRetrieval=true"
jdbc:mysql://xxxx/xxx?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true