分类: LINUX
2013-02-19 17:58:19
主机规划:
virtual_server ip :192.168.1.212
mysql1: 192.168.1.206
mysql2: 192.168.1.210
安装mysql
# yum -y install mysql mysql-server
# rpm -qa |grep mysql
192.168.1.210
[mysqld]master-connect-retry=60
配置同步,互相设置另一台服务器为mysql主
192.168.1.210安装keepalived
# wget
# yum -y install gcc gcc-c++ openssl-devel
# tar xf keepalived-1.2.2.tar.gz
# cd keepalived-1.2.2
# ./configure --prefix=/usr/local/keepalived
# make && make install
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# mkdir /etc/keepalived
keepalived配置文件
192.168.1.206
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id MySQL-ha } vrrp_script check_run { script "/root/keepalived_check_mysql.sh" interval 5 } vrrp_sync_group VG1 { group { VI_1 } } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 88 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 11111 } track_script { check_run } virtual_ipaddress { 192.168.1.212 } }
192.168.1.210
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id MySQL-ha } vrrp_script check_run { script "/root/keepalived_check_mysql.sh" interval 5 } vrrp_sync_group VG1 { group { VI_1 } } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 88 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 11111 } track_script { check_run } virtual_ipaddress { 192.168.1.212 } }
检测脚本
# cat /root/keepalived_check_mysql.sh
#!/bin/bash MYSQL=/usr/bin/mysql MYSQL_HOST=localhost MYSQL_USER=root MYSQL_PASSWORD=000000 CHECK_TIME=3 #mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0 MYSQL_OK=1 function check_mysql_helth (){ $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1 if [ $? = 0 ] ;then MYSQL_OK=1 else MYSQL_OK=0 fi return $MYSQL_OK } while [ $CHECK_TIME -ne 0 ] do let "CHECK_TIME -= 1" check_mysql_helth if [ $MYSQL_OK = 1 ] ; then CHECK_TIME=0 exit 0 fi if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ] then /etc/init.d/keepalived stop exit 1 fi sleep 1 done