Mysql – Check if remote thesql is running

cronMySQLshell-scripting

I want a bash shell script that i can run using a cron job to check if mysql on a remote server is running. If it is, then do nothing, other start the server.

The cronjob will be checking the remote server for a live (or not) mysql every minute. I can write the cron job myself, but i need help with the shell script that checks if a remote mysql is up or down. The response after a check if up or down is not important. But the check is important.

Thank you.

Best Answer

Connect to mysqld using mysqladmin

MySQL Documentation says about mysqladmin ping

Check whether the server is available. The return status from mysqladmin is 0 if the server is running, 1 if it is not. This is 0 even in case of an error such as Access denied, because this means that the server is running but refused the connection, which is different from the server not running.

First, create a user with no privileges

mysql> GRANT USAGE ON *.* TO ping@'%' IDENTIFIED BY 'ping';

Create a script like this

#!/bin/bash

MYSQL_USER=ping
MYSQL_PASS=ping
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
mysqladmin ping ${MYSQL_CONN} 2>/dev/null 1>/dev/null
MYSQLD_RUNNING=${?}
if [ ${MYSQLD_RUNNING} -eq 1 ]; then service mysql start ; fi

Place that script in a crontab as you wish

Make sure the firewall is open on 3306 so ping@'%' can connect.

Related Topic