问题
I have a master/slave MySql replication.
Im looking for a tool that will allow me to monitor the replication (see it has no error, check on the lag, etc.)
I prefer a visual tool that will allow all team members get visibility on the status and not a script tool.
any ideas?
回答1:
#!/bin/bash
HOST=your-server-ip
USER=mysql-user
PASSWORD=mysql-password
SUBJECT="Mysql replication problem"
EMAIL=your@email.address
RESULT=`mysql -h $HOST -u$USER -p$PASSWORD -e 'show slave status\G' | grep Last_SQL_Error | sed -e 's/ *Last_SQL_Error: //'`
if [ -n "$RESULT" ]; then
echo "$RESULT" | mail -s "$SUBJECT" $EMAIL
fi
回答2:
We are using the following bash script. You could do the same idea in php and web base the code.
#!/bin/sh
## Joel Chaney##
## joel.chaney@mongoosemetrics.com (look at robots.txt) ##
## 2012-02-03 ##
repeat_alert_interval=30 # minutes for lock file life
lock_file=/tmp/slave_alert.lck # location of lock file
EMAIL=YOURNAME@YOURCOMPANY.DOM # where to send alerts
SSTATUS=/tmp/sstatus # location of sstatus file
### Code -- do not edit below ##
NODE=`uname -n`
## Check if alert is locked ##
function check_alert_lock () {
if [ -f $lock_file ] ; then
current_file=`find $lock_file -cmin -$repeat_alert_interval`
if [ -n "$current_file" ] ; then
# echo "Current lock file found"
return 1
else
# echo "Expired lock file found"
rm $lock_file
return 0
fi
else
touch $lock_file
return 0
fi
}
SLAVE=mysql
$SLAVE -e 'SHOW SLAVE STATUS\G' > $SSTATUS
function extract_value {
FILENAME=$1
VAR=$2
grep -w $VAR $FILENAME | awk '{print $2}'
}
Master_Binlog=$(extract_value $SSTATUS Master_Log_File )
Master_Position=$(extract_value $SSTATUS Exec_Master_Log_Pos )
Master_Host=$(extract_value $SSTATUS Master_Host)
Master_Port=$(extract_value $SSTATUS Master_Port)
Master_Log_File=$(extract_value $SSTATUS Master_Log_File)
Read_Master_Log_Pos=$(extract_value $SSTATUS Read_Master_Log_Pos)
Slave_IO_Running=$(extract_value $SSTATUS Slave_IO_Running)
Slave_SQL_Running=$(extract_value $SSTATUS Slave_SQL_Running)
Slave_ERROR=$(extract_value $SSTATUS Last_Error)
ERROR_COUNT=0
if [ "$Master_Binlog" != "$Master_Log_File" ]
then
ERRORS[$ERROR_COUNT]="master binlog ($Master_Binlog) and Master_Log_File ($Master_Log_File) differ"
ERROR_COUNT=$(($ERROR_COUNT+1))
fi
POS_DIFFERENCE=$(echo ${Master_Position}-${Read_Master_Log_Pos}|bc)
if [ $POS_DIFFERENCE -gt 1000 ]
then
ERRORS[$ERROR_COUNT]="The slave is lagging behind of $POS_DIFFERENCE"
ERROR_COUNT=$(($ERROR_COUNT+1))
fi
if [ "$Slave_IO_Running" == "No" ]
then
ERRORS[$ERROR_COUNT]="Replication is stopped"
ERROR_COUNT=$(($ERROR_COUNT+1))
fi
if [ "$Slave_SQL_Running" == "No" ]
then
ERRORS[$ERROR_COUNT]="Replication (SQL) is stopped"
ERROR_COUNT=$(($ERROR_COUNT+1))
fi
if [ $ERROR_COUNT -gt 0 ]
then
if [ check_alert_lock == 0 ]
then
SUBJECT="${NODE}-ERRORS in replication"
BODY=''
CNT=0
while [ "$CNT" != "$ERROR_COUNT" ]
do
BODY="$BODY ${ERRORS[$CNT]}"
CNT=$(($CNT+1))
done
BODY=$BODY" \n${Slave_ERROR}"
echo $BODY | mail -s "$SUBJECT" $EMAIL
fi
else
echo "Replication OK"
fi
回答3:
You can use any programming language to query mysql and fetch the results from:
show slave status; <-- execute on slave
show master status; <-- execute on master
If you think this is a bad idea, then install phpmyadmin
, there is an already built-in GUI for replication monitoring, like: http://demo.phpmyadmin.net/master-config/ (replication)
回答4:
If you're just interested in whether the slave is up to date or not:
mysql 'your connection info' -e 'show slave status\G' | grep -i seconds_behind
回答5:
I've used a few different approaches, the most basic being using a PHP web page to check the slave status, and then getting standard monitoring tools to monitor the page. This is a nice approach as it means your existing monitoring tools can be used for alerts by checking the web page.
Example: to check the status of a database server at host db1.internal
http://mywebserver.com/replicationtest.php?host=db1.internal
Should always return "Yes"
replicationtest.php:
<?php
$username="myrepadmin";
$password="";
$database="database";
mysql_connect($_REQUEST['host'],$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="show slave status;";
$result=mysql_query($query);
$arr = mysql_fetch_assoc($result);
echo $arr['Slave_SQL_Running'] ;
mysql_close();
?>
You could also monitor Seconds_Behind_Master, Last_IO_Errno, Last_SQL_Errno etc. You can monitor this web page externally, or add it into many standard monitoring tools that can check a web page. I've used free service http://monitor.us
Alternatively, if you don't mind running code from 3rd parties on your internal infrastructure http://newrelic.com offer great server monitoring tools with a web interface, and include a MySQL plugin that provides lots of great info such as Query Analysis, InnoDB metrics and Replication status with lag monitors. New Relic specialise on web app monitoring but the free service allows you to monitor an unlimited number of servers.
I currently use a combination of these tools with the above web page being used to trigger alerts for emergencies, and the NewRelic tools for viewing long term performance and trend analysis.
回答6:
The question is:
- do you want to know if your Mysql replication is OK
- OR do you want to know if your data are consistent ?
You can't rely only on SHOW SLAVE STATUS output to know if your slave is identical to Master: a (bad) try to solve an error that halted your replication may imply that some INSERT or UPDATE or whatever, never occured on your slave.
To check this you have to read SHOW SLAVE STATUS, of course, everything has to be ok in that output, but you also have to compare data (ie number of rows, checksum, ...).
I've written a PHP tool to do that: https://bitbucket.org/verticalassertions/verticalslave It features :
- check replication (checks on show slave status values, check table, checksum table, ...)
- check replication with auto-repair (same as above + dump of replicated tables in errors)
- dump of non-replicated databases (listed in config)
- reset replication when you broke all to the ground - basically a dump of replicated databases and start slave)
- send shorten report by mail, link to full report on website version
- store past reports
- can be run from CLI (crontab) or manually from website you set up
Feel free to fork and improve. I'm sure some tools are better (especially in layout xD ), but I needed a tool that does exactly what I ask and no fancy things I couldn't figure.
来源:https://stackoverflow.com/questions/8518811/mysql-replication-monitoring-tool