实验环境

CentOS 6.9

MySQL 5.7.18

Keepalived v1.2.13

拓扑图

10.180.2.161 M1

10.180.2.162 M2

10.180.2.200 VIP

环境准备

关闭SELINUX

[root@M1 keepalived]# setenforce 0

setenforce: SELinux is disabled , 并修改/etc/sysconfig/selinux 下的SELINUX=disabled


关闭IPTABLES

[root@M1 keepalived]# service iptables stop
[root@M1 keepalived]# chkconfig --del iptables

安装Mysql 软件包,请详见之前的笔记

搭建主从结构

两台节点的server-id 必须不同,这里我设置M1 为13306,M2 未23306

开启GTID 模式,并更新自己的binlog

gtid-mode=on

enforce-gtid-consistency=1

log_slave_updates = 1

M1创建测试库,并导入到M2

root@localhost:mysql3306.sock [(none)]>select * from testha.t1;
+----+------+
| id | c1 |
+----+------+
| 1 | a1 |
| 2 | a2 |
| 3 | a3 |
| 4 | a4 |
+----+------+
4 rows in set (0.00 sec)

M1 :mysqldump --single-transaction --master-data=2 --databases=testha -uroot -p123456 -S /data/mysql3306.sock > /tmp/testha.sql

M2 :mysql -uroot -p -S /data/mysql3306.sock < testha.sql

创建复制账号,复制配置

两个节点 : create user repl@'%' identified by '123456';  grant replication slave on *.* to repl@'%' ;

M2: change master to master_host='M1',MASTER_USER='repl',master_password='123456',master_port=3306,master_auto_position=1;  /GTID 必须的设置

M2: root@localhost:mysql3306.sock [(none)]>start slave;

检查主从状态

root@localhost:mysql3306.sock [(none)]>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: M1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: 3306-binlog.000007
Read_Master_Log_Pos: 870
Relay_Log_File: M2-relay-bin.000002
Relay_Log_Pos: 873
Relay_Master_Log_File: 3306-binlog.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

尝试在M1 insert 一行是否能同步到M2, insert into t1 values(5,'a5');

在M2 select * from t1;

root@localhost:mysql3306.sock [(none)]>select * from testha.t1;
+----+------+
| id | c1 |
+----+------+
| 1 | a1 |
| 2 | a2 |
| 3 | a3 |
| 4 | a4 |
| 5 | a5 |

因为使用GTID, 已经执行过的SQL 语句不会在M1 上面再次执行,所以在M1 可以直接change master to , 然后start slave

root@localhost:mysql3306.sock [testha]>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: M2
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: 3306-binlog.000001
Read_Master_Log_Pos: 154
Relay_Log_File: M1-relay-bin.000002
Relay_Log_Pos: 371
Relay_Master_Log_File: 3306-binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

安装Keepalived

两个节点都安装以下包

yum install keepalived
yum install MySQL-python

创建用于监控mysql 状态的用户

grant replication client on *.* to monitor@'%' identified by 'P@ssw0rd';  /在其中一个节点执行即可,因为已经互为主备

M1 M2修改 /etc/keepalived/keepalived.conf

vrrp_script vs_mysql_82 {
script "/etc/keepalived/checkMySQL.py -h 10.180.2.161 -P 3306"  /用于检测mysql 心跳, IP 写自身IP
interval 60
}
vrrp_instance VI_82 {
state BACKUP
nopreempt      /新加入的节点不抢占
interface eth1  /根据自身网卡
virtual_router_id 82 /同一组keepalived 必须一样,并且同一个局域网必须唯一,要不然VRRP 会冲突报错
priority 100    /建议所有节点都一样
advert_int 5
authentication {
auth_type PASS
auth_pass 1111  /不能超过8位
}
track_script {
vs_mysql_82
}
virtual_ipaddress {
10.180.2.200
}
}

编辑监控脚本

/etc/keepalived/checkMySQL.py

[root@M1 keepalived]# more checkMySQL.py
#!/usr/bin/python
#coding: utf-8
#

import sys
import os
import getopt
import MySQLdb
import logging

dbhost='10.180.2.161'
dbport=3306
dbuser='monitor'
dbpassword='Password'

def checkMySQL():
global dbhost
global dbport
global dbuser
global dbpassword

shortargs='h:P:'
opts, args=getopt.getopt(sys.argv[1:],shortargs)
for opt, value in opts:
if opt=='-h':
dbhost=value
elif opt=='-P':
dbport=value
#print "host : %s, port: %d, user: %s, password: %s" % (dbhost, int(dbport), dbuser, dbpassword)
db = instanceMySQL(dbhost, dbport, dbuser, dbpassword)
st = db.ishaveMySQL()
#if ( db.connect() != 0 ):
# return 1
#db.disconnect()
return st

class instanceMySQL:
conn = None
def __init__(self, host=None,port=None, user=None, passwd=None):
self.dbhost= host
self.dbport = int(port)
self.dbuser = user
self.dbpassword = passwd

def ishaveMySQL(self):
cmd="ps -ef | egrep -i \"mysqld\" | grep %s | egrep -iv \"mysqld_safe\" | grep -v grep | wc -l" % self.dbport
mysqldNum = os.popen(cmd).read()
cmd ="netstat -tunlp | grep \":%s\" | wc -l" % self.dbport
mysqlPortNum= os.popen(cmd).read()
#print mysqldNum, mysqlPortNum
if ( int(mysqldNum) <= 0):
print "error"
return 1
if ( int(mysqldNum) > 0 and mysqlPortNum <= 0):
return 1
return 0

def connect(self):
# print "in db conn"
# print "host : %s, port: %d, user: %s, password: %s" % (self.dbhost, self.dbport, self.dbuser, self.dbpassword)
try:
self.conn=MySQLdb.connect(host="%s"%self.dbhost, port=self.dbport,user="%s"%dbuser, passwd="%s"%self.dbpas
sword)
except Exception, e:
# print " Error"
print e
return 1
return 0
def disconnect(self):
if (self.conn):
self.conn.close()
self.conn = None

if __name__== "__main__":
st=checkMySQL()
sys.exit(st)

测试脚本是否成功

[root@M1 keepalived]# ./checkMySQL.py -h 10.180.2.261 -P 3306  如果没有返回说明正常

启动keepalived 

service keepalived start

检测keepalived状态

tail -f /var/log/messages

M1:

Jul 28 10:21:19 M1 Keepalived_vrrp[15221]: Using LinkWatch kernel netlink reflector...
Jul 28 10:21:19 M1 Keepalived_vrrp[15221]: VRRP_Instance(VI_82) Entering BACKUP STATE
Jul 28 10:21:19 M1 Keepalived_vrrp[15221]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jul 28 10:21:19 M1 Keepalived_healthcheckers[15220]: Using LinkWatch kernel netlink reflector...
Jul 28 10:21:19 M1 Keepalived_vrrp[15221]: VRRP_Script(vs_mysql_82) succeeded
Jul 28 10:21:35 M1 Keepalived_vrrp[15221]: VRRP_Instance(VI_82) Transition to MASTER STATE
Jul 28 10:21:40 M1 Keepalived_vrrp[15221]: VRRP_Instance(VI_82) Entering MASTER STATE
Jul 28 10:21:40 M1 Keepalived_vrrp[15221]: VRRP_Instance(VI_82) setting protocol VIPs.
Jul 28 10:21:40 M1 Keepalived_vrrp[15221]: VRRP_Instance(VI_82) Sending gratuitous ARPs on eth1 for 10.180.2.200
Jul 28 10:21:40 M1 Keepalived_healthcheckers[15220]: Netlink reflector reports IP 10.180.2.200 added
Jul 28 10:21:45 M1 Keepalived_vrrp[15221]: VRRP_Instance(VI_82) Sending gratuitous ARPs on eth1 for 10.180.2.200

M2:

[root@M2 keepalived]# tail -f /var/log/messages
Jul 28 10:21:26 M2 Keepalived_vrrp[5179]: Registering gratuitous ARP shared channel
Jul 28 10:21:26 M2 Keepalived_healthcheckers[5178]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 28 10:21:26 M2 Keepalived_healthcheckers[5178]: Configuration is using : 5157 Bytes
Jul 28 10:21:26 M2 Keepalived_vrrp[5179]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 28 10:21:26 M2 Keepalived_vrrp[5179]: Configuration is using : 62857 Bytes
Jul 28 10:21:26 M2 Keepalived_vrrp[5179]: Using LinkWatch kernel netlink reflector...
Jul 28 10:21:26 M2 Keepalived_vrrp[5179]: VRRP_Instance(VI_82) Entering BACKUP STATE
Jul 28 10:21:26 M2 Keepalived_vrrp[5179]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jul 28 10:21:26 M2 Keepalived_healthcheckers[5178]: Using LinkWatch kernel netlink reflector...
Jul 28 10:21:26 M2 Keepalived_vrrp[5179]: VRRP_Script(vs_mysql_82) succeeded

至此双主KeepAlived 搭建成功。

基于Mysql 5.7 GTID 搭建双主Keepalived 高可用的更多相关文章

  1. MySQL集群搭建(6)-双主+keepalived高可用

    双主 + keepalived 是一个比较简单的 MySQL 高可用架构,适用于中小 MySQL 集群,今天就说说怎么用 keepalived 做 MySQL 的高可用. 1 概述 1.1 keepa ...

  2. 004.MySQL双主+Keepalived高可用

    一 基础环境 主机名 系统版本 MySQL版本 主机IP Master01 CentOS 6.8 MySQL 5.6 172.24.8.10 Master02 CentOS 6.8 MySQL 5.6 ...

  3. MySQL双主+Keepalived高可用

    原文转自:https://www.cnblogs.com/itzgr/p/10233932.html作者:木二 目录 一 基础环境 二 实际部署 2.1 安装MySQL 2.2 初始化MySQL 2. ...

  4. 利用LVS+Keepalived搭建Mysql双主复制高可用负载均衡环境

    应用背景: MySQL复制(主主,主从...)能在保证数据的备份的同时也能够做读写分离分摊系统压力,但是发生单点故障时,需要手动 切换到另外一台主机.LVS和Keppalived可以设定一个VIP来实 ...

  5. keepalived+mysql双主复制高可用方案

    MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换.而Keepalived通过虚拟IP,实现了 ...

  6. 【 Linux 】Keepalived实现双主模型高可用集群

    要求:    1. 两台web服务器安装wordpress,数据库通过nfs共享    2. 使用keepalived实现双主模型 环境:    主机:        系统:CentOS6.7 x64 ...

  7. KeepAlived双主模式高可用集群

    keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测:通过共用的虚拟IP地址对外提 ...

  8. mysql 主主+ Keepalived 高可用

    这是在mysql互为主从的基础上做的 yum -y install keepalived    #两台机器上都装 配置Keepalived主从, vrrp_instance VI_1 { state ...

  9. 虚拟机 搭建LVS + DR + keepalived 高可用负载均衡

    一:环境说明:   LVS-DR-Master:    10.3.0.82   LVS-DR-Backup:    10.3.0.70   VIP:                10.3.0.60  ...

随机推荐

  1. shiro权限框架(三)

    三.身份验证 身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份 ID 一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明 在 shiro 中,用户需要提供 principa ...

  2. S2第一本书内测

    <深入.NET平台和C#编程>内部测试题-笔试试卷 一 选择题 1) 以下关于序列化和反序列化的描述错误的是( C). a) 序列化是将对象的状态存储到特定存储介质中的过程 b) 二进制格 ...

  3. New UWP Community Toolkit - Staggered panel

    概述 前面 New UWP Community Toolkit 文章中,我们对 2.2.0 版本的重要更新做了简单回顾,其中简单介绍了 Staggered panel,本篇我们结合代码详细讲解  St ...

  4. 浅谈element-ui中的BEM范式实践

    日常的工作中,我们无时无刻不在和样式打交道.没有样式的页面就如同一部电影,被人随意地在不同地方做了截取. BEM规范应该是对于我们现在前端组件开发中我觉得是最合适的一套范式了.所以,我在自己的日常工作 ...

  5. CSS的盒子模型有哪些,区别是什么

    1)盒模型: 内容(content).填充(padding).边界(margin). 边框(border)   2)有两种, IE 盒子模型.标准 W3C 盒子模型:IE的content部分包含了 b ...

  6. 2018最新版本Sublime Text3注册码(仅供测试交流使用)

    -– BEGIN LICENSE -– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...

  7. 新手使用mac上的textedit写HTML时遇到的问题及解决办法

    刚开始在mac上学习HTML,总结一下遇到的问题和解决办法 问题:使用textedit编写html,在网页上却仍然显示的是代码. 解决办法: 打开textedit后打开文本编辑 选择偏好设置 按如图所 ...

  8. 201621123050 《Java程序设计》第7周学习总结

    1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 1.事件:用户的操作,例如点击或输入之类的操作 2. ...

  9. centos 安装配置 mysql

    安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...

  10. Linux 下的权限改变与目录配置

    Linux 下的权限改变与目录配置 ./代表本目录的意思. (1):用户与用户组, 1:文件所有者,文件被某一用户所有 2:用户组:    对文件给与一个或者多个用户权限配置 3:其它人: (2):l ...