解决mysql连接超时和mysql连接错误的问题
目录
- mysql连接超时和mysql连接错误
- 连接MySQL错误create connection SQLException, url: jdbc:mysql://localhost:3306/*****?
- 解决方法
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的6.0.6这个版本的驱动,这个版本的驱动不稳定,可能出现奇怪的问题。更换为5.x版本或者8.x版本都可以解决这个问题。
发出来希望同道中人能避免被这个问题恶心到。
如何知道自己代码实际使用到哪个jar下面的驱动呢?有的时候错误日志能体现出来,有的时候可能需要打印确切的日志信息
在获取connection以后,调用这个方法即可。
我的问题比较坑的地方在于,不知道谁在jre的ext目录下放了这个:mysql-connector-java-6.0.6.jar:6.0.6
导致所有的java程序必须只能用这个jar(基于双亲委派)
?1 2 3 4 5 6 7 8 9 | 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
?1 2 3 4 5 6 | <!--MySQL驱动--> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.6</ version > </ dependency > |
修改后的连接信息如下:
?1 2 3 4 5 6 7 8 9 10 | 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 加
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | spring: http: encoding: charset: UTF-8 force: true enabled: true datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Driver platform: mysql url: jdbc:mysql://127.0.0.1:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8 username: XXXXXX password: XXXXXX #初始化链接数 initialSize: 5 #最小的空闲连接数 minIdle: 5 #最大活动连接数 maxActive: 20 #从池中取连接的最大等待时间,单位ms maxWait: 60000 #每XXms运行一次空闲连接回收器 timeBetweenEvictionRunsMillis: 60000 #池中的连接空闲XX毫秒后被回收 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT1FROMDUAL testWhileIdle: true testOnBorrow: false testOnReturn: false filters: stat,wall,log4j logSlowSql: true |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/shuwei003/article/details/108818207
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。