基于Mysql 5.7 GTID 搭建双主Keepalived 高可用
实验环境
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 高可用的更多相关文章
- MySQL集群搭建(6)-双主+keepalived高可用
双主 + keepalived 是一个比较简单的 MySQL 高可用架构,适用于中小 MySQL 集群,今天就说说怎么用 keepalived 做 MySQL 的高可用. 1 概述 1.1 keepa ...
- 004.MySQL双主+Keepalived高可用
一 基础环境 主机名 系统版本 MySQL版本 主机IP Master01 CentOS 6.8 MySQL 5.6 172.24.8.10 Master02 CentOS 6.8 MySQL 5.6 ...
- MySQL双主+Keepalived高可用
原文转自:https://www.cnblogs.com/itzgr/p/10233932.html作者:木二 目录 一 基础环境 二 实际部署 2.1 安装MySQL 2.2 初始化MySQL 2. ...
- 利用LVS+Keepalived搭建Mysql双主复制高可用负载均衡环境
应用背景: MySQL复制(主主,主从...)能在保证数据的备份的同时也能够做读写分离分摊系统压力,但是发生单点故障时,需要手动 切换到另外一台主机.LVS和Keppalived可以设定一个VIP来实 ...
- keepalived+mysql双主复制高可用方案
MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换.而Keepalived通过虚拟IP,实现了 ...
- 【 Linux 】Keepalived实现双主模型高可用集群
要求: 1. 两台web服务器安装wordpress,数据库通过nfs共享 2. 使用keepalived实现双主模型 环境: 主机: 系统:CentOS6.7 x64 ...
- KeepAlived双主模式高可用集群
keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测:通过共用的虚拟IP地址对外提 ...
- mysql 主主+ Keepalived 高可用
这是在mysql互为主从的基础上做的 yum -y install keepalived #两台机器上都装 配置Keepalived主从, vrrp_instance VI_1 { state ...
- 虚拟机 搭建LVS + DR + keepalived 高可用负载均衡
一:环境说明: LVS-DR-Master: 10.3.0.82 LVS-DR-Backup: 10.3.0.70 VIP: 10.3.0.60 ...
随机推荐
- HDU4310HERO贪心问题
问题描述 When playing DotA with god-like rivals and pig-like team members, you have to face an embarrass ...
- c# 实时监控数据库 SqlDependency
http://blog.csdn.net/idays021/article/details/49661855 class Program { private static string _connSt ...
- beta冲刺3
一,昨天的问题: 页面整理还没做 我的社团这边的后台数据库未完成,前端代码修改未完成. 二,今天已完成 页面整理基本完成,把登陆独立出来了,然后基本处理掉了多余页面(反正也没几个--) 我的社团这边试 ...
- Python实现基于协程的异步爬虫
一.课程介绍 1. 课程来源 本课程核心部分来自<500 lines or less>项目,作者是来自 MongoDB 的工程师 A. Jesse Jiryu Davis 与 Python ...
- Codeforces 240 F. TorCoder
F. TorCoder time limit per test 3 seconds memory limit per test 256 megabytes input input.txt output ...
- border 三角形 有边框的 东西南北的 气泡三角形
链接地址:http://www.cnblogs.com/blosaa/p/3823695.html
- DOM中的事件对象(event)
在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含着所有与事件相关的信息. 包括导致事件的元素.事件的类型以及其他与特定事件相关的信息. 例如:鼠标操作导致的事件对象中,会包含鼠 ...
- 完美解决ubuntu Desktop 16.04 中文版firefox在非root用户不能正常启动的问题
ubuntu安装好后,默认安装有firefox浏览器,不过,非root的账户登录,双击firefox图标,居然出现如下提示:Your Firefox profile cannot be loaded. ...
- LeetCode & Q189-Rotate Array-Easy
Array Description: Rotate an array of n elements to the right by k steps. For example, with n = 7 an ...
- WPF 自定义Calendar样式(日历样式,周六周日红色显示)
一.WPF日历控件基本样式 通过Blend获取到Calendar需要设置的三个样式CalendarStyle.CalendarButtonStyle.CalendarDayButtonStyle.Ca ...