關於MySql連接url參數的設置 專題

報錯:html

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上添加配置:java

useSSL=true

eg:mysql

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


報錯:sql

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

緣由:apache

對於值爲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=passwordtomcat

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)
相關文章
相關標籤/搜索