首页 > 代码库 > mysql+keepalived
mysql+keepalived
mysql+keepalived
1.安装
# yum install -y openssl-devel popt-devel
# tar zxf keepalived-1.2.8.tar.gz
# cd keepalived-1.2.8
# ./configure --prefix=/usr/local/keepalived
# make && make install
2.将keepalived命令、启动脚本及配置文件复制到相应目录下并启动服务
# which keepalived
/usr/local/sbin/keepalived
# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
# /etc/init.d/keepalived start
3.创建配置文件及检测脚本
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_mysql {
script "/root/check_mysql.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #mysql2此处改为BACKUP
interface eth1
virtual_router_id 51
priority 100 #mysql此处改为90,或小于100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_mysql
}
virtual_ipaddress {
192.168.36.200/24 dev eth1
}
}
# check_mysql.sh
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=redhat
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1
#$mysqlclient --host=$host --port=$port --user=$user --password=$password -e "show databases;" > /dev/null 2>&1
if [ $? == 0 ]
then
echo " $host mysql login successfully"
exit 0
else
#echo " $host mysql login faild" #若mysql关闭,则keepalived关闭
/etc/init.d/keepalived stop
exit 2
fi
4.数据库授权远程登录
# mysql -p
mysql> grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘redhat‘;
mysql> flush privileges;
# mysql -uroot -predhat -h192.168.36.200 #vip登录
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cheungssh |
| discuz |
| mysql |
+--------------------+
4 rows in set (0.03 sec)
mysql> quit
Bye
5.检测
此时vip在mysql1的eth1上,且两端的mysql、keepalived均开启
# ip a
eth1:
inet 192.168.36.160/24 brd 192.168.36.255 scope global eth1
inet 192.168.36.200/24 scope global secondary eth1
# /etc/init.d/mysqld status
mysqld (pid 7219) is running...
# /etc/init.d/keepalived status
keepalived (pid 7248) is running...
MYSQL1端:关闭mysql1的mysql服务,keepalived会自动关闭且vip飘在mysql2上
# /etc/init.d/mysqld stop
Stopping mysqld: [ OK ]
# /etc/init.d/mysqld status
mysqld is stopped
# /etc/init.d/keepalived status
keepalived is stopped
MYSQL2端:查看vip
# ip a
eth1:
inet 192.168.36.161/24 brd 192.168.36.255 scope global eth1
inet 192.168.36.200/24 scope global secondary eth1
MYSQL1端:开启mysql、keepalived服务,vip飘回来
# /etc/init.d/mysqld start
Starting mysqld: [ OK ]
# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
# ip a
2: eth1:
inet 192.168.36.160/24 brd 192.168.36.255 scope global eth1
inet 192.168.36.200/24 scope global secondary eth1
本文出自 “真水无香” 博客,请务必保留此出处http://chengyanli.blog.51cto.com/11399167/1846790
mysql+keepalived