关于JDBC使用mysql数据库,因为mysql版本过高而发出的警告

1. 问题描述

警告信息:WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

分析问题:为什么会出现这个警告?

  • 很明显,这不是我们的代码出错。但其实查一下就知道了,这是因为mysql版本过高,处于安全性考虑,所以默认都是开启了SSL。因此你要通过java的JDBC来连接mysql,那显然是不安全的,所以你必须设置不适用SSL。也就是useSSL=false。
  • 但其实仔细看这个警告信息,就会发现,它已经告诉你要怎么解决这个问题了:You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification

2、解决问题

  • 如果你的数据库连接池dataSource的四大连接参数,driver、url、username和password都是硬编码(写死在代码中),那么直接在指定url连接的后面补上useSSL=false 即可。
    注意:链接后面加参数,记得要先加 ? 噢,参数与参数之间就用 & 来连接。
  • 如果你的数据库连接池dataSource的连接参数是保存在properties配置文件中的话,那也很简单,那就不需要修改代码,直接在配置文件中进行修改url链接即可。
  • 建议:这种参数,最好还是使用配置文件的方式来存储,即方便修改,也不用修改源码。重点是:把编译依赖变成了运行依赖!
发布了23 篇原创文章 · 获赞 1 · 访问量 1649

猜你喜欢

转载自blog.csdn.net/weixin_43978412/article/details/98494421