简体   繁体   English

Bash:服务器出现故障时关闭MySQL连接

[英]Bash: Close MySQL connection when server fails

I have a simple script to connect to a extern MySQL server. 我有一个简单的脚本连接到外部MySQL服务器。

#!/bin/bash
server=1.2.3.1
port=3306
user=root
pass=root
db=radius

output=$(mysql -N -h $server -u $user -p$pass $db -e  "select * from table)
echo "$output"

The problem is when the mysql service fails on server 1.2.3.1, because the script gets stucked waiting for answer a long time! 问题是当服务器1.2.3.1上的mysql服务失败时,因为脚本被卡住了等待很长时间的答案!

How can I wait only for 1 second and if server doesn't respond, the script kills the process? 我怎么能等待1秒钟,如果服务器没有响应,脚本会终止进程? Or how can I check if the service is up on the remote server before? 或者如何检查以前服务是否在远程服务器上?

Regards. 问候。

I didn't test this out but I suppose you could set a timer and then test whether your result has a value. 我没有测试这个,但我想你可以设置一个计时器,然后测试你的结果是否有值。

#!/bin/bash
server=1.2.3.1
port=3306
user=root
pass=root
db=radius

output=$(mysql -N -h $server -u $user -p$pass $db -e  "select * from table)
sleep 1
if ! [[ $output ]]
   mysql exit
fi
echo "$output"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM