侧边栏壁纸
博主头像
知我隧道博主等级

一个被程序员生涯耽误的UI设计师

  • 累计撰写 24 篇文章
  • 累计创建 10 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

mysql主从健康监控

kongbai121
2022-01-13 / 0 评论 / 1 点赞 / 446 阅读 / 1492 字 / 正在检测是否收录...

创建检查用户

CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';
GRANT REPLICATION CLIENT ON *.* TO 'test'@'localhost' IDENTIFIED BY 'test';
FLUSH PRIVILEGES;

检查脚本

#!/bin/bash
array=($(mysql -utest -ptest-e "show slave status\G" | grep "Running" | awk '{print $2}'))
if [ "${array[0]}" == "Yes" ] && [ "${array[1]}" == "Yes" ]
then
   echo "Slave is running!"
   exit 0
else
   echo " Slave is not running"
   exit 2
#   exit 0
fi

Java代码调用

/**
 * 调用检测脚本
 * @param bashName 脚本名称
 * @return  是否健康
*/
public Boolean testingSlave(String bashName) {
    Process process = null;
    String command = "./" + bashName;
    try {
        process = Runtime.getRuntime().exec(command);
        int exitValue = process.waitFor();
        return exitValue == 0;
    } catch (IOException | InterruptedException e) {
        log.error("检查数据库主从出错, 错误信息: {}", e.getMessage());
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
    return false;
}
1

评论区