创建检查用户
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;
}
评论区