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
2)安裝keepalived
make && make install
4、配製
1)建立啓動腳本
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalivedchmod +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/bin3)設置啓動參數
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、配製腳本
! 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 $?
檢測腳本中實際未用到ip6、測試
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)
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