MYSQL [Warning] Aborted connection 42355 to db

来源:互联网 发布:王震对新疆的功过知乎 编辑:程序博客网 时间:2024/05/11 08:33
1,问题现象:在/db/mysql/log/mysql.err日志中出现大量的如下信息(web用的是tomcat7.0,设置连接超时时间为100秒):
[root@lovebuy114 ~]# tail -f /db/mysql/log/mysql.err 
2015-04-17 09:35:47 24074 [Warning] Aborted connection 42355 to db: 'memberunion2' user: 'zhanghui' host: '172.16.151.112' (Got timeout reading communication packets)
2015-04-17 09:35:47 24074 [Warning] Aborted connection 42365 to db: 'memberunion2' user: 'zhanghui' host: '172.16.151.112' (Got timeout reading communication packets)
2015-04-17 09:35:47 24074 [Warning] Aborted connection 42366 to db: 'memberunion2' user: 'zhanghui' host: '172.16.151.112' (Got timeout reading communication packets)
2015-04-17 09:35:47 24074 [Warning] Aborted connection 42376 to db: 'memberunion2' user: 'zhanghui' host: '172.16.151.112' (Got timeout reading communication packets)
2015-04-17 09:35:47 24074 [Warning] Aborted connection 42367 to db: 'memberunion2' user: 'zhanghui' host: '172.16.151.112' (Got timeout reading communication packets)
2015-04-17 09:35:47 24074 [Warning] Aborted connection 42377 to db: 'memberunion2' user: 'zhanghui' host: '172.16.151.112' (Got timeout reading communication packets)
2015-04-17 09:35:47 24074 [Warning] Aborted connection 42373 to db: 'memberunion2' user: 'zhanghui' host: '172.16.151.112' (Got timeout reading communication packets)
解决办法:
修改[root@lovebuy114 ~]# grep timeout /etc/my.cnf
interactive_timeout = 120
wait_timeout = 120
log_warnings=1 //注意,我这里原来是2。修改成1后,问题现象果然但是已经不存在了。
在命令行中可以这样修改:
mysql>set global log_warning=1;
mysql>set global interactive_timeout = 120;
mysql>set global wait_timeout = 120;
参数简要说明:
1)interactive_timeout:
参数含义:服务器关闭交互式连接前等待活动的秒数。交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的客户端。
参数默认值:28800秒(8小时)


(2)wait_timeout:
参数含义:服务器关闭非交互连接之前等待活动的秒数。
在线程启动时,根据全局wait_timeout值或全局interactive_timeout值初始化会话wait_timeout值,取决于客户端类型(由mysql_real_connect()的连接选项CLIENT_INTERACTIVE定义)。
参数默认值:28800秒(8小时)


MySQL服务器所支持的最大连接数是有上限的,因为每个连接的建立都会消耗内存,因此我们希望客户端在连接到MySQL Server处理完相应的操作后,应该断开连接并释放占用的内存。如果你的MySQL Server有大量的闲置连接,他们不仅会白白消耗内存,而且如果连接一直在累加而不断开,最终肯定会达到MySQL Server的连接上限数,这会报'too many connections'的错误。对于wait_timeout的值设定,应该根据系统的运行情况来判断。在系统运行一段时间后,可以通过show processlist命令查看当前系统的连接状态,如果发现有大量的sleep状态的连接进程,则说明该参数设置的过大,可以进行适当的调整小些。

问题:
   如果在配置文件my.cnf中只设置参数wait_timeout=100,则重启服务器后进入,执行:
   Mysql> show variables like “%timeout%”;
会发现参数设置并未生效,仍然为28800(即默认的8个小时)。
查询资料后,要同时设置interactive_timeout和wait_timeout才会生效。
【mysqld】
wait_timeout=100
interactive_timeout=100
重启MySQL Server进入后,查看设置已经生效。

另外:java连接池也会导致这个问题。
参见资料:
http://javacrazyer.iteye.com/blog/721393/
发现Hibernate的内置连接池性能是非常的差,还不如直接用第三方的c3p0,改用C3P0连接池,这
个连接池会自动 处理数据库连接被关闭的情况。要使用C3P0很简单,先从Hibernate里把c3p0-0.9.1.jar复
制到项目的lib目录中,再在 hibernate.properties里去掉hibernate.c3p0开头的那些属性的注释(使用缺
省值或自己需要的数值),这样 Hibernate就会自动使用C3P0代替内置的连接池了。到目前为止前面的问题
没有再出现过。 
具体在hibernate.cfg.xml中配置如下
Xml代码  收藏代码
<span style="font-size: medium;"><property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>  
        <!--连接池的最小连接数-->   
        <property name="hibernate.c3p0.min_size">5</property>  
        <!--最大连接数-->   
        <property name="hibernate.c3p0.max_size">30</property>  
        <!--连接超时时间-->  
        <property name="hibernate.c3p0.timeout">1800</property>  
        <!--statemnets缓存大小-->   
        <property name="hibernate.c3p0.max_statements">100</property>  
        <!--每隔多少秒检测连接是否可正常使用  -->   
        <property name="hibernate.c3p0.idle_test_period">121</property>  
        <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3-->   
        <property name="hibernate.c3p0.acquire_increment">1</property>  
        <property name="hibernate.c3p0.validate">true</property></span>  
0 0
原创粉丝点击