How-to setup MySQL HA by using keepalived

With MySQL replication and keepalived, we can setup a quite robust high available MySQL environment in a few steps:

Environment:
Host1: db01.wordpress.com
Host2: db02.wordpress.com
DBVIP: mysql.wordpress.com 10.0.0.1

1. Setup MySQL Master-Master replication
Ref: Setup MySQL replication

2. Install keepalivedat both hosts

--using apt-get, for Ubuntu
apt-get install keepalived
 
--using yum, for Redhat
yum install keepalive

3. Config keepalived

1) Add keepalived config file /etc/keepalived/keepalived.conf
Config file for host db01:

! Configuration File for keepalived
global_defs {
      notification_email {
        [email protected]
      }
      notification_email_from [email protected]
      smtp_server mx.wordpress.com
      smtp_connect_timeout 30
      router_id mysql-ha
      }
 
vrrp_script check_mysql {
   script "/mysql/keepalived_check.sh db02.wordpress.com"
   interval 2
   weight 2
}
 
vrrp_instance VI_1 {
      state BACKUP
      interface eth1
      virtual_router_id 51
      priority 100
      advert_int 1
      nopreempt  # only needed on higher priority node
      authentication {
      auth_type PASS
      auth_pass 1111
      }
 
      track_script {
        check_mysql
      }
      virtual_ipaddress {
        10.0.0.1/24 dev eth1 label eth1:1
      }
      notify_master /mysql/keepalived_master.sh
      notify_backup /mysql/keepalived_backup.sh
}

Config file of host db02:

Copy the config file in db01, and change this line:From   script "/mysql/keepalived_check.sh db02.wordpress.com"to   script "/mysql/keepalived_check.sh db01.wordpress.com"

2) Add scripts to both nodes
/mysql/keepalived_check.sh : monitor MySQL (for the host/network down, keepalived has internal mechanism to monitor them)

#!/bin/bash
# monitor mysql status
# if this node mysql is dead and its slave delay less than 120 seconds, then stop its keepalived. The other node will bind the IP.
 
export MYSQL_HOME=/mysql
export PATH=$MYSQL_HOME/bin:$PATH
 
mysql="$MYSQL_HOME/bin/mysql"
delay_file="$MYSQL_HOME/slave_delay_second.log"
slave_host=$1
 
$mysql -u root --connect_timeout=3 --execute="select version();"
 
if [ $? -ne 0 ]; then
 delayseconds=`cat $delay_file`
 if [ $delayseconds -le 120 ]; then
   /etc/init.d/keepalived stop
 fi
 exit 1 #bad
fi
 
# Get slave delay time and save it
$mysql -urepluser -prepluser -h$slave_host --connect_timeout=3 -e"select version();"
if [ $? -eq 0 ]; then
  delayseconds=`$mysql -urepluser -prepluser -h$slave_host --connect_timeout=3 -e"show slave status\G"|grep Seconds_Behind_Master|awk ‘{print \$2}‘`
  if [[ "$delayseconds" =~ ^[0-9]+$ ]] ; then
     echo "$delayseconds" > $delay_file
  else
     echo "9999" > $delay_file
  fi
fi
exit 0 #good

/mysql/keepalived_master.sh : it will be called when the node becomes master

#!/bin/bash
 
my_host=`hostname`
current_date=`/bin/date +"%b %d %H:%M:%S"`
From="$my_host"
[email protected]
 
Subject="$my_host is MASTER"
Msgboday="$current_date : mysql.wordpress.com is online at $my_host"
echo "$Msgboday" | /usr/bin/mailx  -s "$Subject" "$mail_list"

/mysql/keepalived_backup.sh : it will be called when the node becomes slave

#!/bin/bash
 
my_host=`hostname`
current_date=`/bin/date +"%b %d %H:%M:%S"`
From="$my_host"
[email protected]
 
Subject="$my_host is BACKUP"
Msgboday="$current_date : mysql.wordpress.com is offline at $my_host"
echo "$Msgboday" | /usr/bin/mailx  -s "$Subject" "$mail_list"

4. Start keepalived at both nodes


1

2

3

4

5


service keepalived start

or

/etc/init.d/keepalived start

Check its log file at /var/log/messages

5. Test it
Scenarios:
A. Stop MySQL at the master node
B. Shutdown master node network
C. Shutdown master node OS
D. Split-brain (the nodes cannot connect to each other) – In my test, keepalived didn’t do anything in this situation.

Check result:
1) Check emails
2) Check IP using ifconfig at both nodes
2) Connect to DB without stop:


1

2

3

4

5

6


while true loop

do

date

mysql -urepluser -prepluser -hmysql.wordpress.com -e"select @@hostname;"

sleep 1

done;

In my test, the db cannot be connected for just 2 seconds.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


Mon Oct 29 22:30:51 GMT+7 2012

+---------------+

| @@hostname    |

+---------------+

| db01          |

+---------------+

Mon Oct 29 22:30:52 GMT+7 2012

ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘mysql.wordpress.com‘ (111)

Mon Oct 29 22:30:53 GMT+7 2012

ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘mysql.wordpress.com‘ (111)

Mon Oct 29 22:30:54 GMT+7 2012

+---------------+

| @@hostname    |

+---------------+

| db02          |

+---------------+

时间: 2024-10-06 22:26:56

How-to setup MySQL HA by using keepalived的相关文章

MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(一)

准备服务器: docker network create --subnet=192.168.0.0/16 staticnetdocker run -d --privileged -v `pwd`/mysql_data:/data -p 3001:3306 --name mysql5-master --hostname mysql5-master --net staticnet --ip 192.168.0.101 eiki/mysql:5.7.23 /usr/sbin/init docker r

mysql主从配置&&基于keepalived的主备切换

mysql互为主从设置 && 主备切换配置 需求说明: 1.公司架构一直是一台单独的mysql在线上跑,虽然一直没有出现什么宕机事件,但是出于一个高可用的考虑,提出主从备份.主备切换的需求: 2.实现这个需求的前一段时间只是在做数据库备份的时候实现了主从热备,为了实现主备切换功能,继续操作上述需求: 实验环境: master1:10.1.156.3:3306 master2:10.1.156.5:3306 my.cnf配置文件关于主从这块的修改: master1: server-id =

HA Cluster和KeepAlived

HA Cluster和KeepAlived =========================================================================== 概述: =========================================================================== HA Cluster:  1.介绍 ★Linux Cluster类型: LB:Load Balancing,负载均衡集群: 存在SPoF(单点故

理解 OpenStack 高可用(HA) (5): MySQL HA

本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)Neutron L3 Agent HA - DVR (分布式虚机路由器) (4)RabbitMQ HA (5)MySQL HA 1. MySQL HA 方案概括 Mysql HA 方案有很多种,包括: mmm: http://mysql-mmm.org/ mha: https://code.googl

理解 OpenStack 高可用(HA)(4):RabbitMQ 和 Mysql HA

本系列会分析 OpenStack 的高可用性(HA)解决方案: (1)概述 (写着中...) (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)Neutron L3 Agent HA - DVR (分布式虚机路由器) (4)RabbitMQ 和 Mysql HA 1. 基础知识 1.1 Pacemaker Pacemaker 承担集群资源管理者(CRM - Cluster Resource Manager)的角色,它是一款开源的高可用资源管理软件,适合各种大

在Centos7玩转Mysql半同步和keepalived+MHA(二)

在上一篇完成了Mysql的半同步配置. 链接:在Centos7玩转Mysql半同步和keepalived+MHA(一),本篇则继续完成MHA+keepalived的配置. 角色 IP OS MySQL MHA mha-manager 192.168.1.92 CentOS 7 无 mha-manager mha-node mysql-master 192.168.1.151 CentOS 7 mysql-master mha-node mysql-slave01 192.168.1.152 Ce

inotify+rsync+mysql主主复制+keepalived实现zabbix高可用

思路:主备机通过inotify+rsync实现文件同步,通过mysql主主复制实现数据同步,同一时刻只有一台服务器能对外提供zabbix服务,web端登陆虚ip访问,被监控主机通过配置虚ip来完成监控. 配置: 主机 ip 操作系统 zabbix版本 mysql版本 inotify版本 rsync版本 备注 zabbix-server01 172.27.9.80 Centos7.3.1611 zabbix_server (Zabbix) 3.4.10 5.7.22 3.14 3.1.2 关闭防火

MySQL 高可用之 keepalived+Mysql 双主双活。

环境描述:[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) IP 规划:MySQL-M 192.168.10.10 MySQL-S 192.168.10.20 配置两服务服务器双主: MySQL 安装: [[ema

Mysql双主加Keepalived

一.MySQL于keepalived简介** 前言: 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mysql入口,增加高可用.不过多主需要考虑自增长ID问题,这个需要特别设置配置文件,比如双主,可以使用奇偶,总之,主之间设置自增长ID相互不冲突就能完美解决自增长ID冲突问题. 1.1.MySQL** 1.1.1.MySQL主从复制原理 复制分成三步: \