关于MySql链接url参数的设置 专题

报错:

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.

解决办法:
在jdbc.url上添加配置:

useSSL=true

eg:

jdbc.url=jdbc:mysql://localhost:3306/dbName?useUnicode=true&useSSL=true

报错:

Caused by: java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.ResultSetRow.getNativeTimestamp(ResultSetRow.java:620) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.ByteArrayRow.getNativeTimestamp(ByteArrayRow.java:187) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.ResultSetImpl.getNativeTimestamp(ResultSetImpl.java:4308) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.ResultSetImpl.getTimestampInternal(ResultSetImpl.java:5904) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5588) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5617) ~[mysql-connector-java-5.1.42.jar:5.1.42]
    

解决办法1:
在jdbc.url上添加配置:

zeroDateTimeBehavior=convertToNull

eg: jdbc.url
=jdbc:mysql://localhost:3306/dbName?useUnicode=true&useSSL=true&zeroDateTimeBehavior=convertToNull

原因:

对于值为0000-00-00 00:00:00(默认值)的记录,根据不同的配置,会返回不同的结果:
不配置:默认返回异常
zeroDateTimeBehavior=round 0001-01-01 00:00:00.0
zeroDateTimeBehavior=convertToNull null

解决方法2:
将日期”0000-00-00 00:00:00”改为正常日期即可。

关于"0000-00-00 00:00:00”:
在mysql中"0000-00-00 00:00:00”是作为一个特殊值存在的,但是在Java中, java.sql.Date 会被视为 不合法的值,被JVM认为格式不正确。

 

查询的结果集中某数据行的日期值为0000-00-00 00:00:00。因MySQL的时间类型datetime范围是1000-01-01 00:00:00 到 9999-12-31 23:59:59,所以报错。

 

 

 

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
jdbc.username=root
jdbc.password=password

database.url=jdbc:mysql://database_server:3306/oauth?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC

 

特别说明其中的jdbc.url配置:如果你已经升级好了mysql-connector,其中的characterEncoding=utf8可以被自动被识别为utf8mb4(当然也兼容原来的utf8),而autoReconnect配置我强烈建议配上,我之前就是忽略了这个属性,导致因为缓存缘故,没有读取到DB最新配置,导致一直无法使用utf8mb4字符集,多么痛的领悟!!

没有让mysql驱动开启批量执行sql的开关。

怎么开启呢?在拼装mysql链接的url时,为其加上allowMultiQueries参数,设置为true,如下:

jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

mysql JDBC URL格式如下: 

jdbc:mysql://[host:port],[host:port]…/[database][?参数名1][=参数值1][&参数名2][=参数值2]… 

常用的几个较为重要的参数: 

参数名称 参数说明 缺省值 最低版本要求 
user 数据库用户名(用于连接数据库) 所有版本 
passWord 用户密码(用于连接数据库) 所有版本 
useUnicode 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true false 1.1g 
characterEncoding 当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk false 1.1g 
autoReconnect 当数据库连接异常中断时,是否自动重新连接? false 1.1 
autoReconnectForPools 是否使用针对数据库连接池的重连策略 false 3.1.3 
failOverReadOnly 自动重连成功后,连接是否设置为只读? true 3.0.12 
maxReconnects autoReconnect设置为true时,重试连接的次数 3 1.1 
initialTimeout autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒 2 1.1 
connectTimeout 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本 0 3.0.1 
socketTimeout socket操作(读写)超时,单位:毫秒。 0表示永不超时 0 3.0.1 

对应中文环境,通常mysql连接URL可以设置为: 

代码如下: jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false 

在使用数据库连接池的情况下,最好设置如下两个参数: 

autoReconnect=true&failOverReadOnly=false 

需要注意的是,在xml配置文件中,url中的&符号需要转义成  
&  

比如在tomcat的
server.xml中配置数据库连接池时,mysql jdbc url样例如下:  代码如下:

jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly 

http://www.jb51.net/article/47764.htm

 

jdbc:mysql://127.0.0.1:3306/demo_ds?useServerPrepStmts=true&cachePrepStmts=true
设置参数useServerPrepStmts=true,这个参数的作用是让MySQL服务进行prepare。没有这个参数就是让JDBC进行prepare,MySQL完全感知不到,是没有什么意义的。
cachePrepStmts=true这个参数的意思是告诉JDBC缓存需要prepare的SQL

https://www.cnblogs.com/softidea/p/9180189.html

 

在spring boot  2.1.6.RELEASE 中,如果没有配置&serverTimezone=Asia/Shanghai , 就会一直报错:

019-07-24 19:07:30.903 ERROR 9712 --- [eate-1446291553] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://localhost:3306/wxmp?useUnicode=true&characterEncoding=utf-8&useSSL=false, errorCode 0, state 01S00

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.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:156) ~[druid-1.1.10.jar:1.1.10]
    at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:218) ~[druid-1.1.10.jar:1.1.10]
    at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:150) ~[druid-1.1.10.jar:1.1.10]
    at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1560) ~[druid-1.1.10.jar:1.1.10]
    at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1623) ~[druid-1.1.10.jar:1.1.10]
    at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2468) ~[druid-1.1.10.jar:1.1.10]
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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.
    at sun.reflect.GeneratedConstructorAccessor48.newInstance(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_191]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_191]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.16.jar:8.0.16]
    ... 9 common frames omitted

Disconnected from the target VM, address: '127.0.0.1:57625', transport: 'socket'
2019-07-24 19:07:31.324  INFO 9712 --- [      Thread-15] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2019-07-24 19:07:31.332  INFO 9712 --- [      Thread-15] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.alibaba.druid.pool.DataSourceDisableException
### The error may exist in file [D:\project\work\smartwx-boot\target\classes\mapper\AccountMapper.xml]
### The error may involve com.wxmp.wxcms.mapper.AccountDao.listForPage
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.alibaba.druid.pool.DataSourceDisableException
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)

 

    原文作者:mysql
    原文地址: https://www.cnblogs.com/softidea/p/5765624.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞