利用keepalive+mysql replication 实现数据库的高可用
[root@localhost keepalived-1.2.13]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root @localhost keepalived-1.2.13]# mkdir /etc/keepalived
[[email protected] keepalived-1.2.13]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[[email protected] keepalived-1.2.13]# cp /usr/local/sbin/keepalived /usr/sbin/
[[email protected] keepalived-1.2.13]# chkconfig --level 345 keepalived on
router_id KeepAlive_Mysql
}vrrp_script check_run {
script "/app/daily_test/shell/mysql_check.sh"
interval 300
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
notify_master /app/daily_test/shell/master.sh
notify_master /app/daily_test/shell/backup.sh
notify_master /app/daily_test/shell/stop.sh
virtual_ipaddress {
10.207.130.59
}
}
global_defs {
router_id KeepAlive_Mysql
}vrrp_script check_run {
script "/app/daily_test/shell/mysql_check.sh"
interval 300
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
notify_master /app/daily_test/shell/master.sh
notify_master /app/daily_test/shell/backup.sh
notify_master /app/daily_test/shell/stop.sh
virtual_ipaddress {
10.207.130.59
}
}
while true
do
mysql -e "show status;">/dev/null 2>&1
i=$?
ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ];then
exit 0
else
if [ $i = 1 ] && [ $j = 0 ];then
exit 0
else
if [ $count -gt 5 ];then
break
fi
let count++
continue
fi
fi
done
/etc/init.d/keepalived stop
master_Log_File=$(mysql -e "show slave status\G" |grep -w master_Log_File |awk -F":" '{print $2}')
Relay_Log_File=$(mysql -e "show slave status\G" |grep -w Relay_master_Log_File |awk -F":" '{print $2}')
Read_master_Log_Pos=$(mysql -e "show slave status\G" |grep -w Read_master_Log_Pos |awk -F":" '{print $2}')
Exec_master_Log_Pos=$(mysql -e "show slave status\G" |grep -w Exec_master_Log_Pos |awk -F":" '{print $2}')
i=1
while true
do
if [ $master_Log_File = $Relay_Log_File ] && [ $Read_master_Log_Pos = $Exec_master_Log_Pos ];then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ];then
break
fi
continue
let i++
fi
done
mysql -e "stop slave;"
mysql -e "set global innodb_support_xa=1;"
mysql -e "set global sync_binlog=0;"
mysql -e "set global innodb_flush_log_at_trx_commit=2;"
mysql -e "set global event_scheduler=1;"
mysql -e "flush logs;grant all privileges on *.* to 'admin'@'%' identified by '123456';flush privileges;"
mysql -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
mysql -e "grant all privileges on *.* to 'admin'@'%' identified by '123456';flush privileges;"
mysql -e "set global event_scheduler=0;"
mysql -e "set global innodb_support_xa=1;"
mysql -e "set global sync_binlog=0;"
mysql -e "set global innodb_flush_log_at_trx_commit=2;"
mysql -e "grant all privileges on *.* to 'admin'@'%' identified by '123456';flush privileges;"
mysql -e "set global innodb_support_xa=1;"
mysql -e "set global sync_binlog=1;"
mysql -e "set global innodb_flush_log_at_trx_commit=1;"
i=1
while true
do
M_File1=$(mysql -e "show master status\G" | grep -w File | awk -F":" '{print $2}')
M_Position1=$(mysql -e "show master status\G" | grep -w Position | awk -F":" '{print $2}')
sleep 1
M_File2=$(mysql -e "show master status\G" | grep -w File | awk -F":" '{print $2}')
M_Position2=$(mysql -e "show master status\G" | grep -w Position | awk -F":" '{print $2}')
if [ $M_File1 = $M_File2 ] && [ $M_Position1 = $M_Position2 ];then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ];then
break
fi
continue
let i++
fi
done
转载于:https://my.oschina.net/liting/blog/403964