最近遇到一个问题,查询数据库的时候报了一个Mysql2::Error: closed MySQL connection 的错误。为什么会出现这种错误呢?我分析了一下:
我使用的是mysql,姑且用mysql服务做说明。
mysql server有个配置项:wait_timeout,我们查查官方文档:
connect_timeout
Command-Line Format --connect_timeout=#
System Variable Name connect_timeout
Variable Scope Global
Dynamic Variable Yes
Permitted Values Type integer
Default 10
Min Value 2
Max Value 31536000
The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake. The default value is 10 seconds.
Increasing the connect_timeout value might help if clients frequently encounter errors of the form Lost connection to MySQL server at 'XXX', system error: errno.
不仔细解释了,这个配置项的意思就是一个数据库连接的等待超时时间,就是说,如果一个连接建立之后,如果超过这个时间没有操作,这个连接就会断开。
基于这个知识点,我来解释一下我为什么会出现这个错误。
我的使用场景是启动一个进程,在这个进程里会得到别的服务推送过