Thursday, August 30, 2012

Mysql: replication check by comparing count rows

//get all tables from db and add them in dbase.tables file
SELECT TABLE_NAME,SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbasename' GROUP BY TABLE_NAME;

//add tables_check.sh script on each dbase server
#!/bin/bash

for x in `cat dbase.tables`;
do mysql -u root -psecret --skip-column-names -e "select count(*) from $x" dbasename;
done


//add replication_count_check.sh script on the 'master' server
#!/bin/bash

 ssh root@192.168.1.1 'sh tables_check.sh' > tables_check1.log
 ssh root@192.168.1.2 'sh tables_check.sh' > tables_check2.log


//ssh -o 'StrictHostKeyChecking no' can be also used if not added in know_hosts

if (cmp tables_check1.log tables_check2.log);
then echo "OK" ;
else  echo "Rows are not matching" | mail -s 'Dbase: Replication problem' user@email.com ; fi



**don't forget to exchange ssh keys

No comments:

Post a Comment