Keepalived双机热备

1、环境

192.168.56.11

192.168.56.12

已配制好双主复制

虚拟IP预定为 192.168.56.51

2、下载

http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

3、安装

1) 安装openssl

yum install openssl openssl-devel

2)安装keepalived

tar zxvf keepalived-1.2.13.tar.gz 
./configure --prefix=/usr/local/keepalived
make && make install

4、配制


1)建立启动脚本

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
chmod +x /etc/init.d/keepalived


2)设置路径

PATH=/usr/local/keepalived/sbin:/usr/local/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin


3)设置启动参数



sed -i 's@^.*KEEPALIVED_OPTIONS.*=.*$@KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"@g' /usr/local/keepalived/etc/sysconfig/keepalived


4)启动

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
service keepalived restart


5、配制脚本

cat /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script vs_mysql {
    script "/usr/local/keepalived/checkMySQL.sh -h 192.168.56.11 -P 3306"
    interval 3
}

vrrp_instance vip1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        vs_mysql
    }
    virtual_ipaddress {
        192.168.56.51
    }
}

cat /usr/local/keepalived/checkMySQL.sh
#!/bin/bash
# checkMySQL.sh
# script "/etc/keepalived/checkMySQL.sh -h 192.168.11.82 -P 3306"

function usage()
{
  echo "usage:"
  echo "example:# checkMySQL.sh -h 192.168.11.82 -P 3306"
}

function isHaveMySQL()
{
  mysqldNum=$(ps -ef | egrep -i "mysqld" | grep $1 | egrep -iv "mysqld_safe" | grep -v grep | wc -l)
  mysqlPortNum=$(netstat -tunlp | grep $1 | wc -l)
  echo $1   $mysqldNum   $mysqlPortNum
  if (( $mysqldNum >= 1  )) && (( $mysqlPortNum >= 1 ))
  then
    return 0
  else
    return 1
  fi
  return 1
}


while getopts "h:P:" option
do
    case "$option" in
        h)
            dbhost="$OPTARG";;
        P)
            dbport="$OPTARG";;
        \?)
            usage
            exit 1;;
    esac
done

if [ "-$dbhost" = "-" ]; then
	usage
	exit 1
fi

if [ "-$dbport" = "-" ]; then
	usage
	exit 1
fi

res=$(isHaveMySQL $dbport)
exit $?
检测脚本中实际未用到ip

6、测试

先后在11与12上执行service keepalived restart启动服务
注意:不要同时启动,要有时间间隔
可以看到11进入主模式


而12进入备用状态



这时通过vip访问mysql,显示连接到了11
G:\Tools\Console2>mysql -utest -ptest -h192.168.56.51
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.21-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| mysql11    |
+------------+
1 row in set (0.00 sec)


如果停止11的mysql,可以看到主备切换


12切换到了主模式

再次通过sql确认
mysql> select @@hostname;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> select @@hostname;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    7
Current database: *** NONE ***

+------------+
| @@hostname |
+------------+
| mysql12    |
+------------+
1 row in set (0.01 sec)
经过一次错误提示后,连接到了12


启动11的mysql,11会自动切换至主模式







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章