|
公司的网络环境是这样:
mysql主服务器ip地址分别为:
192.168.0.20,192.168.0.100,192.168.0.105
从服务器ip地址为:192.168.0.30其中
3307端口用来同步192.168.0.20的数据库,其套接字文件为/tmp/mysql2.sock
3308端口用来同步192.168.0.100的数据库,其套接字文件为/tmp/mysql3.sock
3309端口用来同步192.168.0.105的数据库,其套接字文件为/tmp/mysql4.sock
这里我没有讲述如何配置主从复制服务器及如何在同一台服务器通过修改配置文件来开启多个mysql端口
我这里只是描述了如何通过shell脚本来监控主从复制复制之间的状态,以便及时发现与解决问题,你可以通过修改这个脚本来得到更多的信息
脚本原代码如下,没有做任何更多的解释,一看就懂
#!/bin/bash
#written by zhanghy
##### Setting some variables #####
#192.168.10.20-----/tmp/mysql2.sock
#192.168.10.100----/tmp/mysql3.sock
#192.168.10.105----/tmp/mysql4.sock
mycommand=/usr/local/mysql/bin/mysql
server2="192.168.10.20 192.168.10.100 192.168.10.105"
socket="/tmp/mysql2.sock /tmp/mysql3.sock /tmp/mysql4.sock"
ruser2=root
myuser1=root
##### SLAVES: Check replication #####
#check replication status
for i in $socket
do
tmpmysql1=$($mycommand -u $myuser1 --socket=$i -e "show slave status\G" | \
awk 'BEGIN {
tel = 0
}
/Slave_IO_Running: Yes/ { tel++ }
/Slave_SQL_Running: Yes/ { tel++ }
END {
if ( tel == 2 ) {
print "Yes"
} else {
print "No"
}
}'
)
done
#define display function
report_repl_status()
{
echo -e "\n"
echo "$replication"
echo "Master log_bin Positiontmp2mysql1"
}
report_ok_info()
{
echo -e "\n"
echo "Slave Read_Master_Log_Pos($n):$tmp2mysql2"
echo "Seconds_Behind_Master:$tmp2mysql3"
}
report_error_info()
{
echo "Last_Errno($n):$tmp2mysql2"
echo "Last_Error($n):$tmp2mysql3"
echo -e "\n"
}
#get mysql master server logbin position
get_server_logbin_position()
{
for hostname in $server2
do
replication="REPL OK For $hostname - Replicationrocess is fine"
tmp2mysql1=$($mycommand -u $myuser1 -h "$hostname" -e "show master status\G" | \
awk '/Position/ {print $2}')
report_repl_status
done
}
#get replication slave status where slave is ok
#you can get more information by set variables
output_replication_info()
{
get_server_logbin_position
for n in $socket
do
tmp2mysql2=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
awk '/Read_Master_Log_Pos/ {print $2}')
tmp2mysql3=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
awk '/Seconds_Behind_Master/ {print $2}')
report_ok_info
done
}
#get slave error information when slave is error
#you can get more information by set variables
output_error_info()
{
get_server_logbin_position
for n in $socket
do
tmp2mysql2=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
awk '/Last_Errno/ {print $2}')
tmp2mysql3=$(echo 'show slave status\G' |$mycommand -u $myuser1 --socket=$n | \
awk '/Last_Error/ {print $2}')
report_error_info
done
}
# Here you generate the part of the output
if [ $tmpmysql1 = 'Yes' ]
then
output_replication_info
else
output_error_info
fi
##### END OF SCRIPT ##### |
|