JDBC连接MySQL遇到的问题记录

gitpage博客地址https://xisynotz.xyz

1.驱动包版本问题!!

会不断报错连接不上数据库,而且似乎没有明显的提示。

数据库用的是Mysql8版本,但工程里面mysql驱动包却是5.1.37版本。只需修改驱动包为8.0.11版本即可。

而且驱动的包也改变了,由原来的:/generatorSqlmapCustom/lib/mysql-connector-java-5.1.28-bin.jar

换成了:mysql-connector-java-8.0.11.jar

Mysql8.0连接JDBC驱动需要注意 驱动的类换了个名字 Class.forName(“com.mysql.cj.jdbc.Driver”);

2.Establishing SSL connection without server’s identity verification is not recommended.

stackoverflow

加上useSSL=false

con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=false", "root", "123456");

3.连接数超出

'root' has exceeded the 'max_questions' resource (current value: 2) 

一般情况下,用户是不会修改这个值,0的话就是没有限制,然而我手欠,修改了这个值。

解决此问题,应该用别的超级权限的用户登录,直接修改这个值,然后刷新权限。

4.时区问题

java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

加上serverTimezone=GMT

con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=true&serverTimezone=GMT", "root", "123456");

5.MySQL 8.0 Public Key Retrieval is not allowed

最简单的解决方法是在连接后面添加 allowPublicKeyRetrieval=true

文档给出的解释是:

如果用户使用了 sha256_password 认证,密码在传输过程中必须使用 TLS 协议保护,但是如果 RSA 公钥不可用,可以使用服务器提供的公钥;可以在连接中通过 ServerRSAPublicKeyFile 指定服务器的 RSA 公钥,或者AllowPublicKeyRetrieval=True参数以允许客户端从服务器获取公钥;但是需要注意的是 AllowPublicKeyRetrieval=True可能会导致恶意的代理通过中间人攻击(MITM)获取到明文密码,所以默认是关闭的,必须显式开启。

猜你喜欢

转载自blog.csdn.net/saber_jk/article/details/92697654
今日推荐