环境: centos6.5_x64
准备:
两台mysql机器
主1 master: 192.168.32.130
主2 backup: 192.168.32.131
VIP: 192.168.32.100
步骤:
1.配置mysql的双主架构
2.安装keepalived
keepalived-1.2.23.tar.gz
yum -y install openssl openssl-devel
wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
tar zxvf keepalived-1.2.23.tar.gz
cd keepalived-1.2.23
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
make && make install
3.把keepalived设置为系统服务
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
4.修改keepalived配置
vi /etc/keepalived/keepalived.conf //详见.conf详细配置
vi /usr/local/keepalived/mysql.sh
#!/bin/bash pkill keepalived
chmod +x /usr/local/keepalived/mysql.sh
/etc/init.d/keepalived start
5.测试
5.1 分别在两台mysql机器上授权, 允许用户远程登录
mysql> grant all on *.* to‘kkk‘@‘192.168.32.%‘ identified by ‘123456‘;
mysql> flush privileges;
5.2 在任何一台机器上, 通过虚拟IP来连接mysql服务器
mysql -ukkk -p123456 -h192.168.32.100
5.3 停止master,看是否能正常切换到backup,然后来查看VIP在哪台服务器上
service mysqld stop
ip addr
5.4 master服务器故障恢复后,是否主动抢占资源,成为活动服务器 (取决于这个参数nopreempt)
keepalived.conf详细配置
! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 #优先级 (backup设置为90) advert_int 1 nopreempt #不主动抢占资源,只在master这台优先级高的设置 (backup不设置) authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.100 } } virtual_server 192.168.32.100 3306 { delay_loop 2 #lb_algo wrr #lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.32.130 3306 { #真实mysql服务器的ip地址 weight 3 notify_down /usr/local/keepalived/mysql.sh #貌似没有执行脚本, why? TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } }
;