高可用Keepalived+MySQL Replication

mysql架构为主从形式,当master故障时,会自动切换到slave上,当然也可设置为双master,但是有个弊端,比如,当某用户发表文章时,由于此时主机的压力很大,假设落后2000s,那么这台主机就挂了,另一台主机接管,vip漂移到从机上时,因为同步延长大,用户刚才发表的文章还没有复制过来,于是用户又发表了一篇文章,当原来的master修好后,由于SQL和IO线程还处于开启状态,因此还会继续同步刚才没有复制完的数据,这时有可能把用户新发表的文章更改掉,造成用户数据丢失。主从架构,故障切换后,采取人工方式重新与新的master进行同步复制。

1.1.       安装

[[email protected] home]# tar -zxvfkeepalived-1.2.7.tar.gz

[[email protected] home]# cd keepalived-1.2.7

[[email protected] keepalived-1.2.7]#./configure --prefix=/usr/local/keepalived

........

Keepalived configuration

------------------------

Keepalived version       : 1.2.7

Compiler                 : gcc

Compiler flags           : -g -O2 -DETHERTYPE_IPV6=0x86dd

Extra Lib                : -lpopt -lssl -lcrypto

Use IPVS Framework       : Yes

IPVS sync daemon support : Yes

IPVS use libnl           : No

Use VRRP Framework       : Yes

Use VRRP VMAC            : No

SNMP support             : No

Use Debug flags          : No

[[email protected] keepalived-1.2.7]#

[[email protected] keepalived-1.2.7]# mkdir/etc/keepalived

[[email protected] keepalived-1.2.7]# cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

[[email protected] keepalived-1.2.7]# cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[[email protected] keepalived-1.2.7]# cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[[email protected] keepalived-1.2.7]# cp/usr/local/keepalived/sbin/keepalived /usr/sbin/

[[email protected] keepalived-1.2.7]# chkconfig--add keepalived

[[email protected] keepalived-1.2.7]# chkconfig--level 345 keepalived on

[[email protected] keepalived-1.2.7]#

1.1.1.      keepalived安装成功检查

[[email protected] keepalived]# ll

total 16

drwxr-xr-x 2 root root 4096 Jul 24 13:26bin

drwxr-xr-x 5 root root 4096 Jul 24 13:26etc

drwxr-xr-x 2 root root 4096 Jul 24 13:26sbin

drwxr-xr-x 3 root root 4096 Jul 24 13:26share

[[email protected] keepalived]# cd etc/

[[email protected] etc]# ll

total 12

drwxr-xr-x 3 root root 4096 Jul 24 13:26keepalived

drwxr-xr-x 3 root root 4096 Jul 24 13:26rc.d

drwxr-xr-x 2 root root 4096 Jul 24 13:26sysconfig

[[email protected] etc]# cd keepalived/

1.2.       主服务器配置1.2.1.      keepalived配置

[[email protected] keepalived-1.2.7]# cd/etc/keepalived/

[[email protected] keepalived]# vikeepalived.conf

  

global_defs {

router_id KeepAlive_Mysql

}

vrrp_script check_run {

script  "/home/sh/mysql_check.sh"

interval 300

}

vrrp_sync_group VG1 {

group {

VI_1

}

}

vrrp_instance VI_1 {

state MASTER

#备服务器改为BACKUP

#   state BACKUP

interface eth0

virtual_router_id 51

priority 100

#备服务器改为90

#   priority 90

advert_int 1

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

check_run

}

notify_master /home/sh/master.sh

notify_backup /home/sh/backup.sh

notify_stop /home/sh/stop.sh

virtual_ipaddress {

192.168.6.200

}

}

notify_master:状态改变为master后执行的脚本;

notify_backup:状态改变为backup后执行的脚本;

notify_stop:VRRP停止后执行的脚本

使用到四个脚本:mysql_check.sh、master.sh、backup.sh、stop.sh

1.2.2.      健康检查脚本

[[email protected]]# vi mysql_check.sh

  

#!/bin/bash

.  /root/.bash_profile

count=1

while  true

do

mysql  -e "show status;" > /dev/null 2>&1

i=$?

ps aux  | grep mysqld | grep -v grep > /dev/null 2>&1

j=$?

if [ $i  = 0 ] && [ $j = 0 ]

then

exit 0

else

if [ $i = 1 ] && [ $j = 0 ]

then

exit 0

else

if [ $count -gt 5 ]

then

break

fi

let count++

continue

fi

fi

done

/etc/init.d/keepalived  stop

首先判断同步复制是否执行完毕,如果没执行完毕,等1分钟,不论是否执行,都跳过,并停止同步复制进程,其次,更改前端程序连接的业务账号admin的权限和密码,并记录当前切换以后的日志和pos点。

1.2.3.      主服务器脚本

[[email protected]]# vi master.sh

  

#!/bin/bash

.  /root/.bash_profile

Master_Log_File=$(mysql  -e "show slave statusG" | grep -w Master_Log_File | awk -F":  " ‘{print $2}‘)

Relay_Master_Log_File=$(mysql  -e "show slave statusG" | grep -w Relay_Master_Log_File | awk  -F": " ‘{print $2}‘)

Read_Master_Log_Pos=$(mysql  -e "show slave statusG" | grep -w Read_Master_Log_Pos | awk  -F": " ‘{print $2}‘)

Exec_Master_Log_Pos=$(mysql  -e "show slave statusG" | grep -w Exec_Master_Log_Pos | awk  -F": " ‘{print $2}‘)

i=1

while  true

do

if [  $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos  -eq $Exec_Master_Log_Pos ]

then

echo "ok"

break

else

sleep 1

if [ $i -gt 60 ]

then

break

fi

continue

let i++

fi

done

mysql  -e "stop slave;"

mysql  -e "set global innodb_support_xa=0;"

mysql  -e "set global sync_binlog=0;"

mysql  -e "set global innodb_flush_log_at_trx_commit=0;"

mysql  -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO ‘admin‘@‘%‘ IDENTIFIED BY  ‘123456‘;flush privileges;"

mysql  -e "show master status;" > /tmp/master_status_$(date  "+%y%m%d-%H%M").txt

1.2.4.      备服务器脚本

[[email protected]]# vi backup.sh

  

#!/bin/bash

. /root/.bash_profile

mysql -e "GRANT ALL PRIVILEGES ON  *.* TO ‘admin‘@‘%‘ IDENTIFIED BY ‘1q2w3e4r‘;flush privileges;"

mysql -e "set global  event_scheduler=0;"

mysql -e "set global innodb_support_xa=0;"

mysql -e "set global  sync_binlog=0;"

mysql -e "set global  innodb_flush_log_at_trx_commit=0;"

1.2.5.      keepalived停止后执行的脚本

stop.sh表示keepalived停止后执行的脚本,首先更改admin密码,其次设置参数,保证不丢失数据,最后查看是否有写操作,不论是否执行完毕,1分钟后退出:

[[email protected] keepalived]# vi stop.sh

  

#!/bin/bash

. /root/.bash_profile

mysql -e "GRANT ALL PRIVILEGES ON  *.* TO ‘admin‘@‘%‘ IDENTIFIED BY ‘1q2w3e4r‘;flush privileges;"

mysql -e "set global  innodb_support_xa=1;"

mysql -e "set global  sync_binlog=1;"

mysql -e "set global  innodb_flush_log_at_trx_commit=1;"

M_File1=$(mysql -e "show master  statusG" | awk -F‘: ‘ ‘/File/{print $2}‘)

M_Position1=$(mysql -e "show master  statusG" | awk -F‘: ‘ ‘/Position/{print $2}‘)

sleep 1

M_File2=$(mysql -e "show master  statusG" | awk -F‘: ‘ ‘/File/{print $2}‘)

M_Position2=$(mysql -e "show master  statusG" | awk -F‘: ‘ ‘/Position/{print $2}‘)

i=1

while true

do

if [ $M_File1 = $M_File1 ] && [  $M_Position1 -eq $M_Position2 ]

then

echo "ok"

break

else

sleep 1

if [ $i -gt 60 ]

then

break

fi

continue

let i++

fi

done

至此,配置完毕,只要把master关机或者把mysq停掉,vip就会漂移到slave上,在把原来的master修复好后,vip并不会自己漂移过去,仍旧会停留在slave上,这样做的好处是防止数据频繁切换导致数据不一致。

1.3.       keepalived管理

查看进程:

[[email protected] keepalived]# ps -aux|grepkeepalived

Warning: bad syntax, perhaps a bogus ‘-‘?See /usr/share/doc/procps-3.2.7/FAQ

root     4147  0.0  0.0  4016   676 pts/1    R+  13:42   0:00 grep keepalived

[[email protected] keepalived]# servicekeepalived start

Starting keepalived:                                       [  OK  ]

[[email protected] keepalived]# ps -aux|grepkeepalived

Warning: bad syntax, perhaps a bogus ‘-‘?See /usr/share/doc/procps-3.2.7/FAQ

root     4158  0.0  0.0  5040   576 ?        Ss  13:42   0:00 keepalived -D

root     4159  0.0  0.1  5088  1428 ?        S   13:42   0:00 keepalived -D

root     4160  0.1  0.0  5088   936 ?        S   13:42   0:00 keepalived -D

root     4163  0.0  0.0  4016   672 pts/1    R+  13:43   0:00 grep keepalived

[[email protected] keepalived]# lsmod|grep ip_vs

ip_vs_rr                6081  3

ip_vs                  78081  5 ip_vs_rr

[[email protected] keepalived]# tail -f/var/log/messages

Jul 24 13:44:44 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.5]:1358.

Jul 24 13:44:50 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server [192.168.201.100]:443.

Jul 24 13:44:50 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.2]:1358.

Jul 24 13:44:56 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.201.100]:443.

Jul 24 13:44:56 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.2]:1358.

Jul 24 13:44:56 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.3]:1358.

Jul 24 13:44:56 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.4]:1358.

Jul 24 13:44:56 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.5]:1358.

Jul 24 13:45:02 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.201.100]:443.

Jul 24 13:45:02 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.2]:1358.

Jul 24 13:45:08 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.201.100]:443.

Jul 24 13:45:08 gflinuxKeepalived_healthcheckers[4159]: Timeout connect, timeout server[192.168.200.2]:1358.

Jul 24 13:45:08

时间: 2024-10-18 14:16:55

高可用Keepalived+MySQL Replication的相关文章

mysql流行的四种高可用架构之:Keepalived+Mysql Replication

关于MySQL-HA,目前有很多种解决方案,目前互联网上用的较多的是Keepalived+Mysql Replication组合.MMM+Mysql Replication组合.Heartbeat+DRBD+Mysql组合,以及红帽RHSC高可用集群套件.

MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移

转自 MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移 - KK ——专注数据 - 博客频道 - CSDN.NEThttp://blog.csdn.net/kk185800961/article/details/51115264# 系统信息: mysql主库 192.168.1.152 CentOS 5.6 mysql 5.6.22 mysql从库 192.168.1.153 CentOS 5.6 mysql 5.6.22 VIP 192.168.1.150 my

专职DBA-基于MHA高可用搭建MySQL读写分离架构-Atlas

专职DBA-基于MHA高可用搭建MySQL读写分离架构-Atlas 1.Atlas介绍 Atlas是由Qihoo360,Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目. 它是在mysql-proxy-0.8.2版本的基础上,对其进行了优化,增加了一些新的功能特性. 360内部使用Atlas运行的mysql业务,每天承载的读写请求数达几十亿条. 下载地址:https://github.com/Qihoo360/Atlas/releases 注意: 1.Atlas只能安装运

MySQL 高可用:mysql+mycat实现数据库分片(分库分表)

本文引用于http://blog.csdn.net/kk185800961/article/details/51147029 MySQL 高可用:mysql+mycat实现数据库分片(分库分表) 什么是MYCAT: 一个彻底开源的,面向企业应用开发的大数据库集群 支持事务.ACID.可以替代MySQL的加强版数据库 一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群 一个融合内存缓存技术.NoSQL技术.HDFS大数据的新型SQL Server 结合传统数据库和新型分布式数

Linux高可用集群方案之配置heartbeat v2基于crm+hb_gui接口,配置高可用httpd,mysql,lvs

本章主要配置heartbeat v2基于crm+hb_gui接口,配置高可用httpd,mysql,lvs. 如何安装heartbeat v2.httpd.nfs.配置心跳连接.ssh密钥通信.同步时间.添加名称解析.配置yum源等请参照: >> Linux高可用集群方案之配置heartbeat v2基于haresources配置文件的httpd高可用集群 http://ccschan.blog.51cto.com/11854461/1922966  ll  本文导航    · 前期准备及相关

mysql高可用+keepalived

生产环境中一台mysql主机存在单点故障,所以我们要确保mysql的高可用性,即俩台mysql服务器如果其中有一台mysql服务器挂掉后,另外一台就能立刻接替进行工作. MYSQL的高可用方案一般有 Keepalived+双主,MHA,PXC,MMM,Heartbeat+DRBD等 比较常用的是keepalived+双主MHA和PXC 这次主要介绍利用keepalived实现MYSQL数据库的高可用. 基本思路:俩台MYSQL互为主从关系(双主),通过keepalived配置配置虚拟IP,实现当

mysql系列之8.mysql高可用 (keepalived)

环境: 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/keepaliv

数据库高可用架构(MySQL、Oracle、MongoDB、Redis)

一.MySQL MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换 服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失败之后需要手动恢复主从架构 MySQL中型高可用架构 方案:MMM + MySQL双主 + 多从高可用方案 服务器资源: 1.至少五台PC Server,2台MySQL主库,2台MySQL从库,1台MMM Monitor: 2.1台MMM Monitor选择低配: 3.如果不采用F5作为从库的负载均衡器

[转]数据库高可用架构(MySQL、Oracle、MongoDB、Redis)

一.MySQL MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换 服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失败之后需要手动恢复主从架构 MySQL中型高可用架构 方案:MMM + MySQL双主 + 多从高可用方案 服务器资源: 1.至少五台PC Server,2台MySQL主库,2台MySQL从库,1台MMM Monitor: 2.1台MMM Monitor选择低配: 3.如果不采用F5作为从库的负载均衡器