最近服务器不定时出现连接mysql数据库错误,造成路由通http://nctoro.com不正常(连接数据库失败,请检查数据库信息。错误编号:2002)重启ISS问题依然存在,重启mysql服务问题依然未解决。利用Netstat  -n -a 查询是否有程序占用3306端口,发现没有任何程序占用。重启服务器后,网站就能正常访问,过段时间问题又会出现。

     一直找不到原因,就找到个临时解决方法,利用批处理程序检测端口是否能正常连通,不能连通的话重启服务器。首先需要下载一个tcping小工具,把tcping.exe放到windows/system32 下

   第一步:新建一个文本文档,内容为:

@echo off
tcping 127.0.0.1 3306 >>1.log

保存为aaa.bat

   第二步:新建一个文本文档,内容为:

@echo off
type C:zd1.log | findstr /i "open" >nul
if %errorlevel%==0 goto t1 
if %errorlevel%==1 goto end
:t1

del 1.log
echo 打开端口成功 >>chk.log
exit
:end
del 1.log
echo 打开端口失败 >>chk.log
shutdown -r -t 0

保存为bbb.bat
 

代码解释:tcping 127.0.0.1 3306 >>1.log  tcping 127.0.0.1 的3306端口(当然这里的端口可以修改成自己要检测的端口)并保存到1.log文件

 

Probing 127.0.0.1:3306/tcp - Port is open (82 bytes read) - time=63ms
Probing 127.0.0.1:3306/tcp - Port is open (82 bytes read) - time=0ms
Probing 127.0.0.1:3306/tcp - Port is open (82 bytes read) - time=0ms
Probing 127.0.0.1:3306/tcp - Port is open (82 bytes read) - time=0ms

Ping statistics for 127.0.0.1:3306:
     4 probes sent. 
Approximate trip times in milli-seconds:
     Minimum = 0ms, Maximum = 63ms, Average = 15ms

上面是连接成功后保存的内容

Probing 127.0.0.1:3306/tcp -  - time=938ms
Probing 127.0.0.1:3306/tcp -  - time=1063ms
Probing 127.0.0.1:3306/tcp -  - time=969ms
Probing 127.0.0.1:3306/tcp -  - time=985ms

Ping statistics for 127.0.0.1:3306:
     4 probes sent. 
Approximate trip times in milli-seconds:
     Minimum = 938ms, Maximum = 1063ms, Average = 988ms
以上是连接失败后保存的内容

 

type C:zd1.log | findstr /i "open" >nul    打开1.log文件看是否有open(C:zd1.log 根据自己文件位置更改)
if %errorlevel%==0 goto t1    有open就跳到t1
if %errorlevel%==1 goto end  没有就跳到end
:t1
echo 打开端口成功
exit
:end
del 1.log 连接失败后删除1.log
echo 打开端口失败 

shutdown -r -t 0 连接失败后重启服务器

 

第四步:利用计划任务来建立每5分钟重复检测服务

计划任务.jpg

一定要先建立aaa.bat的计划任务。