shell 内存监控 mysql监控






远程监控mysql端口
yum install telnet nmap nc -y

进程检查
ps -ef |grep mysql| grep -v grep |wc -l


准备php的环境依赖
yum remove php-mysql
yum install php_mysqlnd php
编写php连接mysql代码

<?php
$mysql_id=mysql_connect("127.0.0.1:23308","root","1234") or mysql_error();
if ($mysql_id){
    echo "mysql connection successful";
}else {
echo mysql_error();
}

准备python的环境依赖
yum install python3 python3-devel python3-pip
通过python的包管理工具,安装链接mysql的模块
pip3 install pymysql
编写python连接mysql代码

import pymysql

db = pymysql.connect(
     host="localhost",
     port=23308,
     user="root",
     password="1234",
     db="mysql",
     charset="utf8"
)

cursor=db.cursor()
cursor.execute("select version()")
data=cursor.fetchone()
print("数据库连接正确,该数据库的版本为:%s"%data)
db.close()

开发shell if脚本
通过执行python文件 来确定mysql正常

#!/bin/bash
dir="/mnt/mysql_Co/"
cd $dir
python3  mysql_py.py > /dev/null 2>&1
if [ $? -eq 0 ]
 then
     echo "mysql连接成功,已经启动了"
else
     echo "mysql连接失败,没有启动!"
     systemctl start mariadb
fi

posted on 2023-06-13 11:12  sprr  阅读(20)  评论(0编辑  收藏  举报