标签:
公司的项目还处于开发阶段,我把整个后台服务临时放在阿里云上供前端测试,用的阿里云的ECS云服务器,HTTP请求服务器和数据库服务都安装在一台机子上(穷啊,凑合用),做测试用,配置相当低:单核1Gb。其实我对服务器多大配置能承受多大访问压力并没有多大概念。前不久使用Jmeter进行http接口性能测试,发现短时间内访问量比较大时,总是会请求错误,根据返回的结果提示是数据库错误,查看一下数据库状态,果真数据库宕机了。
service mysqld status

#!/bin/bashpgrep -x mysqld &> /dev/nullif [ $? -ne 0 ]thenecho "At time: `date` :MySQL is stop .">> /var/log/mysql_messagesservice mysql start#echo "At time: `date` :MySQL server is stop."elseecho "MySQL server is running ."fi
将上述脚本保存到mysql.sh中,上传到服务器,运行该脚本可以发现输出数据库服务正在运行
# sh mysql.shMySQL server is running
每隔一定时间自动运行脚本
crontab -e
输入(如果不能输入,按键盘上的Insert键就能输入了)
*/5 * * * * /your_dir/mysql.sh
service cron restart
#!/bin/bashpgrep -x mysqld &> /dev/nullif [ $? -ne 0 ]thenmosquitto_pub -t mysql_status -m "Failed"service mysql start#echo "At time: `date` :MySQL server is stop."elsemosquitto_pub -t mysql_status -m "Running"fi
import paho.mqtt.client as mqtt# The callback for when the client receives a CONNACK response from the server.def on_connect(client, userdata, flags, rc):print("Connected with result code "+str(rc))# Subscribing in on_connect() means that if we lose the connection and# reconnect then subscriptions will be renewed.client.subscribe("mysql_status")#订阅,第一个参数是订阅的主题# The callback for when a PUBLISH message is received from the server.def on_message(client, userdata, msg):print(msg.topic+" "+str(msg.payload))client = mqtt.Client()client.on_connect = on_connectclient.on_message = on_messageclient.connect("主机名或ip", 1883, 60)#第一个参数为主机名,及Mosquitto所在服务器,第二个参数是端口# Blocking call that processes network traffic, dispatches callbacks and# handles reconnecting.# Other loop*() functions are available that give a threaded interface and a# manual interface.client.loop_forever()

标签:
原文地址:http://www.cnblogs.com/star91/p/5059378.html