keepalived是安装在两台MySQL服务器上的
首先安装keepalived 过程不解释就正常解压安装就好
安装后配置 vim /etc/keepalived/keepalived.conf 内容如下
10.1.1.20的配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state backup #两台配置此处均是BACKUP
interface eth0
virtual_router_id 51
priority 100 #优先级,另一台改为90
advert_int 1
nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.25
}
}
virtual_server 10.1.1.25 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50 #会话保持时间
protocol TCP
real_server 10.1.1.20 3306 {
weight 3
notify_down /tmp/nimei.sh #检测到mysql服务挂了就执行这个脚本(脚本要自己写哈)
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}
10.1.1.21 的配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state backup
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.25
}
}
virtual_server 10.1.1.25 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.1.1.21 3306 {
weight 3
notify_down /tmp/nimei.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
编写监控mysql服务是否挂了的脚本,按照上面配置文件的位置编写脚本。
vim /tmp/nimei.sh
#!/bin/sh
pkill keepalived
脚本很简单啊 就一句,目的是当keepalived检测到mysql服务挂了之后触发这个脚本,杀死keepalived进程,让另一台机器接管
好 修改后启动keeplived服务
介此整个集群搭建完成
三、测试
找一台机器用虚拟ip连接mysql
[root@localhost html]# mysql -uab -h 10.1.1.25 -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 736
Server version: 5.1.66-log Source distribution
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>
这样成功连上了,然后你可以任意关闭某台机器,或者某台机器的mysql服务,看看还能不能连上!!
谢谢!!