keepalived实现mysql高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived实现mysql高可用相关的知识,希望对你有一定的参考价值。
说明:需要3个节点,主节点(202.207.178.6)、从节点(202.207.178.7)、测试节点(202.207.178.8),虚拟地址:202.207.178.4。配置实现mysql数据库主从复制,并使用keepalived实现高可用,可以实现主数据库down机时,从数据库可用!
mysql主从复制详见本人博客http://10927734.blog.51cto.com/10917734/1868323
前提:
由于是高可用服务,首先配置高可用服务实现的一些基本条件
1)节点名称必须跟uname -n命令的执行结果一致
node1:
# hostname node1
# vim /etc/sysconfig/network
HOSTNAME=node1
node2:
# hostname node2
# vim /etc/sysconfig/network
HOSTNAME=node2
2)节点之间必须通过ssh互信通信
[[email protected] ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ‘‘
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[[email protected] ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ‘‘
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
3)集群各节点之间时间必须同步
使用ntp服务器同步时间
ntpdate ip(配置了ntp服务的主机地址)
4)配置本地解析:
[[email protected] ~]# vim /etc/hosts
202.207.178.6 node1
202.207.178.7 node2
[[email protected] ~]# scp /etc/hosts node2:/etc/
一、安装keepalived,实现高可用
在主节点上:
1、安装keepalived
# tar xf keepalived-1.2.1.tar.gz
# cd keepalived-1.2.1
# ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-642.11.1.el6.i686/
# make && make install
注意:
可能需要安装此包:
# yum install popt-devel
发现/usr/src/kernels/为空,需要安装内核源码
# yum install kernel-devel
2、配置keepalived
1)设置一个变量
# DIR=/usr/local/
2)执行如下语句,将keepalived的配置文件放置在/etc下相关目录中
# cp $DIR/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p
/etc/keepalived
# cp $DIR/sbin/keepalived /usr/sbin/
3)为keepalived提供配置文件
# cd /etc/keepalived/
# vim keepalived.conf(添加以下内容)
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 5
#非抢占 nopreempt
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
202.207.178.4
}
}
virtual_server 202.207.178.4 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 202.207.178.6 3306 {
weight 100
notify_down /data/sh/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
4)启动服务
# /etc/init.d/keepalived restart
# ps -ef | grep keepalived
此时发现服务已经启动!
# tail -fn 100 /var/log/messages
此时发下一直滚屏,是缺少一个模块,下面加载此模块
# modprobe ip_vs
# /etc/init.d/keepalived restart
# tail -fn 100 /var/log/messages
此时一切正常
在从节点上:
配置同主节点,只是在配置文件中修改以下几项即可:
state BACKUP
priority 90
real_server 202.207.178.7 3306
二、配置相关操作,并测试
1、在主从节点上分别添加如下两个文件并授权,实现在一方的数据库down机时停止keepalived,主从角色实现切换
# vim /data/sh/mysql.sh
/etc/init.d/keepalived stop
# chmod +x /data/sh/nginx.sh
2、在主从节点上分别对数据库授权
mysql>grant all on fsy.* to [email protected]‘202.207.178.%‘ identified by ‘123456‘;
3、此时就可以开始测试了
1)再启动一台虚拟机,用此虚拟机连接数据库
# mysql -uroot -h202.207.178.4 -p
2)在主节点停止mysql数据库,此时会发现测试节点的数据库依然连接正常!
到此为止,已经使用keepalived实现了mysql高可用!
欢迎批评指正!
本文出自 “10917734” 博客,请务必保留此出处http://10927734.blog.51cto.com/10917734/1875658
以上是关于keepalived实现mysql高可用的主要内容,如果未能解决你的问题,请参考以下文章