解决mysql连接超时和mysql连接错误的问题


mysql连接超时和mysql连接错误 在生产环境中,偶尔且不规律的出现mysql连接超时和创建连接出错的问题:

15-09-2020 13:25:46 info - java.sql.sqlnontransientconnectionexception: could not create connection to database server.
15-09-2020 13:25:46 info -     at com.mysql.cj.jdbc.exceptions.sqlerror.createsqlexception(sqlerror.java:526)
15-09-2020 13:25:46 info -     at com.mysql.cj.jdbc.exceptions.sqlerror.createsqlexception(sqlerror.java:513)
...
15-09-2020 13:25:46 info - caused by: java.lang.arrayindexoutofboundsexception: 24
15-09-2020 13:25:46 info -     at com.mysql.cj.mysqla.io.buffer.readinteger(buffer.java:271)
15-09-2020 13:25:46 info -     at com.mysql.cj.mysqla.io.mysqlacapabilities.setinitialhandshakepacket(mysqlacapabilities.java:62)
15-09-2020 13:25:46 info -     at com.mysql.cj.mysqla.io.mysqlaprotocol.readservercapabilities(mysqlaprotocol.java:482)
15-09-2020 13:25:46 info -     at com.mysql.cj.mysqla.io.mysqlaprotocol.beforehandshake(mysqlaprotocol.java:367)
15-09-2020 13:25:46 info -     at com.mysql.cj.mysqla.io.mysqlaprotocol.connect(mysqlaprotocol.java:1412)
15-09-2020 13:25:46 info -     at com.mysql.cj.mysqla.mysqlasession.connect(mysqlasession.java:132)
15-09-2020 13:25:46 info -     at com.mysql.cj.jdbc.connectionimpl.connectonetryonly(connectionimpl.java:1726)
20/09/05 02:41:58 info dataximport: 2020-09-05 02:41:58.819 [0-0-0-writer] warn  commonrdbmswriter$task - 回滚此次写入, 采用每次写入一行方式提交. 因为:no operations allowed after statement closed.
20/09/05 02:41:58 info dataximport: 2020-09-05 02:41:58.824 [0-0-0-writer] error writerrunner - writer runner received exceptions:
20/09/05 02:41:58 info dataximport: com.alibaba.datax.common.exception.dataxexception: code:[dbutilerrorcode-05], description:[往您配置的写入表中写入数据时失败.].  - com.mysql.cj.jdbc.exceptions.communicationsexception: communications link failure
20/09/05 02:41:58 info dataximport: 
 20/09/16 00:47:58 info : the last packet successfully received from the server was 4,882 milliseconds ago.  the last packet sent successfully to the server was 4,883 milliseconds ago.
 20/09/16 00:47:58 info :     at com.mysql.cj.jdbc.exceptions.sqlerror.createcommunicationsexception(sqlerror.java:590)
 20/09/16 00:47:58 info :     at com.mysql.cj.jdbc.exceptions.sqlexceptionsmapping.translateexception(sqlexceptionsmapping.java:57)
 20/09/16 00:47:58 info : caused by: com.mysql.cj.core.exceptions.cjcommunicationsexception: communications link failure
 20/09/16 00:47:58 info : 
。。。
20/09/16 00:47:58 info : caused by: java.net.socketexception: 连接超时 (write failed)
 20/09/16 00:47:58 info :     at java.net.socketoutputstream.socketwrite0(native method)
。。。
这种超时错误,如果拿到度娘去搜,一般都会说是wait_timeout,但是这种问题很好定位;而且日志中3秒或者更少的时间就报错的话,而且是在重复使用某个connection的时候就报错了,就很可能不是这个问题了 。需要进一步分析 。
而光凭这些报错信息,是很难定位到问题的确切原因的,作为研发的话,很容易想到环境问题或者网络问题 。
【解决mysql连接超时和mysql连接错误的问题】中途解决过程经历了各种猜测,各种尝试,都没有见效,最后发现自己被强迫使用的mysql的6.0.6这个版本的驱动,这个版本的驱动不稳定,可能出现奇怪的问题 。更换为5.x版本或者8.x版本都可以解决这个问题 。
发出来希望同道中人能避免被这个问题恶心到 。
如何知道自己代码实际使用到哪个jar下面的驱动呢?有的时候错误日志能体现出来,有的时候可能需要打印确切的日志信息
在获取connection以后,调用这个方法即可 。
我的问题比较坑的地方在于,不知道谁在jre的ext目录下放了这个:mysql-connector-java-6.0.6.jar:6.0.6
导致所有的java程序必须只能用这个jar(基于双亲委派)
public static void printjar(object o) {        string classname = o.getclass().getname();        string classnamepath = classname.replace(".", "/") + ".class";        url is = o.getclass().getclassloader().getresource(classnamepath);        string ppath = is.getfile();        ppath = org.apache.commons.lang3.stringutils.replace(ppath, "%20", " ");        logger.info("类所在的jar为");        logger.info(org.apache.commons.lang3.stringutils.removestart(ppath, "/"));    }
连接mysql错误create connection sqlexception, url: jdbc:mysql://localhost:3306/*****? 具体报错如下:
2018-11-12 16:14:21.704 error 9752 --- [eate-1537371824] com.alibaba.druid.pool.druiddatasource   : create connection sqlexception, url: jdbc:mysql://localhost:3306/*****?allowmultiqueries=true&useunicode=true&characterencoding=utf-8&usessl=false, errorcode 1193, state hy000
java.sql.sqlexception: unknown system variable 'query_cache_size'
    at com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:957)
    at com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3878)
    at com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3814)
    at com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:2478)
    at com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:2625)
    at com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2547)
    at com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2505)
    at com.mysql.jdbc.statementimpl.executequery(statementimpl.java:1370)
    at com.mysql.jdbc.connectionimpl.loadservervariables(connectionimpl.java:3862)
    at com.mysql.jdbc.connectionimpl.initializepropsfromserver(connectionimpl.java:3290)
    at com.mysql.jdbc.connectionimpl.connectonetryonly(connectionimpl.java:2299)
    at com.mysql.jdbc.connectionimpl.createnewio(connectionimpl.java:2085)
    at com.mysql.jdbc.connectionimpl.<init>(connectionimpl.java:795)
    at com.mysql.jdbc.jdbc4connection.<init>(jdbc4connection.java:44)
    at sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method)
    at sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62)
    at sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45)
    at java.lang.reflect.constructor.newinstance(constructor.java:423)
    at com.mysql.jdbc.util.handlenewinstance(util.java:404)
    at com.mysql.jdbc.connectionimpl.getinstance(connectionimpl.java:400)
    at com.mysql.jdbc.nonregisteringdriver.connect(nonregisteringdriver.java:327)
    at com.alibaba.druid.filter.filterchainimpl.connection_connect(filterchainimpl.java:156)
    at com.alibaba.druid.filter.stat.statfilter.connection_connect(statfilter.java:218)
    at com.alibaba.druid.filter.filterchainimpl.connection_connect(filterchainimpl.java:150)
    at com.alibaba.druid.pool.druidabstractdatasource.createphysicalconnection(druidabstractdatasource.java:1560)
    at com.alibaba.druid.pool.druidabstractdatasource.createphysicalconnection(druidabstractdatasource.java:1623)
    at com.alibaba.druid.pool.druiddatasource$createconnectionthread.run(druiddatasource.java:2468)

解决方法 本人是mysql版本问题,用的是mysql8.0,将mysql驱动改成如下:5.1.6
<!--mysql驱动--><dependency>  <groupid>mysql</groupid>  <artifactid>mysql-connector-java</artifactid>  <version>5.1.6</version></dependency>修改后的连接信息如下:
spring:    datasource:        type: com.alibaba.druid.pool.druiddatasource        driverclassname: com.mysql.jdbc.driver        druid:            first:  #数据源1                url: jdbc:mysql://localhost:3306/数据库名称?allowmultiqueries=true&useunicode=true&characterencoding=utf-8                username: *****                password: *****........................or  2020/05/28 加
spring:http:encoding:charset: utf-8force: trueenabled: truedatasource:type: com.alibaba.druid.pool.druiddatasourcedriverclassname: com.mysql.jdbc.driverplatform: mysqlurl: jdbc:mysql://127.0.0.1:3306/数据库名称?useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull&servertimezone=gmt%2b8username: xxxxxxpassword: xxxxxx#初始化链接数initialsize: 5#最小的空闲连接数minidle: 5#最大活动连接数maxactive: 20#从池中取连接的最大等待时间,单位msmaxwait: 60000#每xxms运行一次空闲连接回收器timebetweenevictionrunsmillis: 60000#池中的连接空闲xx毫秒后被回收minevictableidletimemillis: 300000validationquery: select1fromdualtestwhileidle: truetestonborrow: falsetestonreturn: falsefilters: stat,wall,log4jlogslowsql: true以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com 。
-- 展开阅读全文 --

    推荐阅读