MySQL Server 5.7.21 + mysql-connector-java 5.1.38 + druid 1.0.8
一.配置步骤:
1.pom.xml 引入相关jar包:
<!-- mysql数据库的驱动包 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
2.spring.xml 数据源配置:
<!-- 数据源,阿里Druid连接池 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" init-method="init" destroy-method="close">
<!-- 数据库信息 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/manangement?useUnicode=true&characterEncoding=utf8&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="5" />
<property name="minIdle" value="1" />
<property name="maxActive" value="300" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- PSCache,并且指定每个连接上PSCache的大小(mysql数据库建议关闭) -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="-1" />
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat" />
</bean>
3.Druid监控配置这里没有写,数据库相关信息也可以写在config.properties资源文件里面;
二.注意事项:
1.MySQL Server 和 mysql-connector-java 要对应 ,如果是使用了 MySQL Server 8+和 mysql-connector-java 8+ 版本;
DataSource配置需要修改为:
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/manangement?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=时区标志" />
2. 报错:
a. java.sql.SQLException: Unknown system variable 'tx_isolation'
原因是mysql-connector-java版本过低,升级一下版本;
b.com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3299)
原因是MySQL Server 和 mysql-connector-java 版本不一致,使Druid驱动加载不能连接数据库;改成相应版本就行;如MySQL Server 5.7.21 + mysql-connector-java 5.1.38 + druid 1.0.8
c.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.
原因是默认SSL连接,禁用时在url上加上useSSL=false,如:
jdbc:mysql://localhost:3306/manangement?useUnicode=true&characterEncoding=utf8&useSSL=false
---------------------
作者:黄粱一梦-努力努力
来源:CSDN
原文:https://blog.csdn.net/qq_26369317/article/details/80681221
版权声明:本文为博主原创文章,转载请附上博文链接!