欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL+keepalived的高可用自动切换实现教程

程序员文章站 2022-05-25 12:47:58
Keepalived安装配置: [root@localhost ~]# wget tar zxf keepalived-1.2.13.tar.gz cdkeepalived-...

Keepalived安装配置:

[root@localhost ~]# wget

tar zxf keepalived-1.2.13.tar.gz

cdkeepalived-1.2.13

yum install gcc gcc-c++

yum install kernel-devel -y

[root@localhost keepalived-1.2.13]# ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-696.20.1.el6.x86_64/

&&make && make install

[root@localhost keepalived-1.2.13]# DIR=/usr/local/

[root@localhost keepalived-1.2.13]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

[root@localhost keepalived-1.2.13]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived

[root@localhost keepalived-1.2.13]# cp /usr/local/sbin/keepalived /usr/sbin/

修改Master服务器上keepalived.conf配置如下,vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

251469868@qq.com

}

notification_email_from 251469868@qq.com

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 {

192.168.227.100

}

}

virtual_server 192.168.227.100 3306 {

delay_loop 6

lb_algo wrr

lb_kind DR

persistence_timeout 60

protocol TCP

real_server 192.168.227.138 3306 {

weight 100

notify_down /data/sh/msyql.sh

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

}

[root@localhost keepalived]# /etc/init.d/keepalived restart

停止 keepalived: [失败]

正在启动 keepalived: [确定]

[root@localhost keepalived]# /etc/init.d/keepalived restart

停止 keepalived: [确定]

正在启动 keepalived: [确定]

[root@localhost keepalived]# ps -ef |grep keep

root 46101 30792 0 07:57 pts/0 00:00:00 vi keepalived.conf

root 46140 1 0 07:59 00:00:00 keepalived -D

root 46142 46140 0 07:59 00:00:00 keepalived -D

root 46143 46140 0 07:59 00:00:00 keepalived -D

root 46148 30792 0 07:59 pts/0 00:00:00 grep keep

[root@localhost keepalived]# tail -fn100 /var/log/messages

[root@localhost keepalived]# cd

[root@localhost ~]# modprobe ip_vs

[root@localhost ~]# lsmod |grep ip_vs

ip_vs 125694 1

libcrc32c 1246 1 ip_vs

ipv6 334932 294 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6

[root@localhost ~]# /etc/init.d/keepalived restart

停止 keepalived: [确定]

正在启动 keepalived: [确定]

[root@localhost ~]# tail -fn 10 /var/log/messages

Mysql 从服务器配置keepalived.conf跟master 一样,只需要把Realserver IP修改成

real_server 192.168227.139;优先级从100改为90即可

在master、slave数据库上创建/data/sh/mysql.sh脚本,内容为:

pkill keepalived

然后分别重启两台数据库上keepalive服务即可,最后测试停止master mysql 服务,是否会自动切换

到backup上