MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换。而Keepalived通过虚拟IP,实现了双主对外的统一接口以及自动检查、失败切换机制。联合使用,可以实现MySQL数据库的高可用方案。

实验环境:
OS:centos 6.x x86_64系统
MySQL版本: :mysql 5.6.22   64 位
A: master :192.168.79.3 3306
B: slave :192.168.79.4 3306

操作系统时间一致更改:
# date -s "20150319 15:51:42"
# hwclock --systohc

AB数据库安装及主从配置

新建mysql用户,用户组,创建 datadir 
# groupadd mysql 
# useradd mysql -g mysql -s /sbin/nologin -d /opt/mysql

解压mysql二进制安装包,对解压后的mysql目录加一个符号连接
# cd /opt/mysql
# tar -xvzf mysql-5.6.22-linux-glibc2.5-x86_64.tar.gz
# cd /usr/local/
# ln -s /opt/mysql/mysql-5.6.22-linux-glibc2.5-x86_64 mysql
# ls
bin etc games include lib lib64 libexec mysql sbin share src

创建mysql的安装目录并修改权限
# mkdir -p /data/mysql/mysql_3306/{data,logs,tmp}
chown -R mysql:mysql /data/mysql/
chown -R mysql:mysql /usr/local/mysql/

加环境变量,解决找不到mysql命令的问题
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile

创建修改mysql配置文件
修改my.cnf

server_id
log_slave_updates
gtid-mode= off
binlog-ignore-db=mysql
replicate-ignore-db=mysql
auto_increment_offset= 1;
auto_increment_increment= 2;
ps:主从库的server_id不要一样。 如果从库是整个拷贝的uuid也不要一样,data下的auto.cnf.  从库:auto_increment_offset= 2;auto_increment_increment= 2;

初始化系统数据文件 ,在basedir下初始化
# ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf --datadir=/data/mysql/mysql_3306/data

安装mysql后的善后工作
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysql start

delete from mysql.user where user!='root' or host!='localhost';
truncate mysql.db;
drop database test;
grant all privileges on *.* to 'liyt'@'%' identified by 'liyt';
grant replication slave, replication client on *.* to 'repl'@'%' identified by 'replslave';
flush privileges;

reset master;

启动 slave mysql服务
A:
>show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mybinlog.000001 | 120 | | | |
+-----------------+----------+--------------+------------------+-------------------+
B:
change master to master_host='192.168.79.3', master_port=3306 ,master_user='repl', master_password='replslave', master_log_file='mybinlog.000001', master_log_pos=120;

B:
>show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mybinlog.000001 | 120 | | | |
+-----------------+----------+--------------+------------------+-------------------+
A:
change master to master_host='192.168.79.4', master_port=3306 ,master_user='repl', master_password='replslave', master_log_file='mybinlog.000001', master_log_pos=120;

测试双主同步:创建新的数据库和表看是否能同步

keepalived安装及配置
GRANT REPLICATION CLIENT ON *.* TO 'monitor'@'%' IDENTIFIED BY 'monitor';

#yum install keepalived

#yum install MySQL-python

配置A:
[root@taotao ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
router_id MYSQL_3
}
vrrp_script chk_mysql {
script "/etc/keepalived/checkMySQL.py -h 192.168.79.3 -P 3306"
interval 60 
}
vrrp_instance VI_MYSQL1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 82
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.79.66
}
}
这里state不配置MASTER,是期望在MASTER宕机后再恢复时,不主动将MASTER状态抢过来,避免MySQL服务的波动。
由于不存在使用lvs进行负载均衡,不需要配置虚拟服务器virtual server,下同。

配置B:
[root@taotao ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
router_id MYSQL_4
}
vrrp_script chk_mysql {
script "/etc/keepalived/checkMySQL.py -h 192.168.79.4 -P 3306"
interval 60 
}
vrrp_instance VI_MYSQL1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 82
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.79.66
}
}

checkMySQL.pyAB完全一样代码如下:

# cat /etc/keepalived/checkMySQL.py
#!/usr/bin/python
#coding: utf-
# import sys
import os
import getopt
import MySQLdb
import logging dbhost='localhost'
dbport=
dbuser='monitor'
dbpassword='monitor' def checkMySQL():
global dbhost
global dbport
global dbuser
global dbpassword shortargs='h:P:'
opts, args=getopt.getopt(sys.argv[:],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
#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) <= ):
print "error"
return
if ( int(mysqldNum) > and mysqlPortNum <= ):
return
return 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.dbpassword)
except Exception, e:
# print " Error"
print e
return
return
def disconnect(self):
if (self.conn):
self.conn.close()
self.conn = None if __name__== "__main__":
st=checkMySQL()
sys.exit(st)
 
A B启用keepalived
# /etc/init.d/keepalived start
 shell>chkconfig –level 2345 keepalived on
 ps:先启动,你内心期望成为对外服务的机器,确认VIP绑定到那台机器上,然后在启动另外一台的keepalived

观察配置A的日志:
[root@taotao ~]# /etc/init.d/keepalived start
[root@taotao ~]# tail -f /var/log/messages

Mar 20 05:09:01 taotao Keepalived[56536]: Starting Keepalived v1.2.13 (10/15,2014)
Mar 20 05:09:01 taotao Keepalived[56538]: Starting Healthcheck child process, pid=56539
Mar 20 05:09:01 taotao Keepalived[56538]: Starting VRRP child process, pid=56540
Mar 20 05:09:01 taotao Keepalived_healthcheckers[56539]: Netlink reflector reports IP 192.168.79.3 added
Mar 20 05:09:01 taotao Keepalived_healthcheckers[56539]: Netlink reflector reports IP fe80::20c:29ff:fed8:3944 added
Mar 20 05:09:01 taotao Keepalived_healthcheckers[56539]: Registering Kernel netlink reflector
Mar 20 05:09:01 taotao Keepalived_healthcheckers[56539]: Registering Kernel netlink command channel
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Netlink reflector reports IP 192.168.79.3 added
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Netlink reflector reports IP fe80::20c:29ff:fed8:3944 added
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Registering Kernel netlink reflector
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Registering Kernel netlink command channel
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Registering gratuitous ARP shared channel
Mar 20 05:09:01 taotao Keepalived_healthcheckers[56539]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 20 05:09:01 taotao Keepalived_healthcheckers[56539]: Configuration is using : 6251 Bytes
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Configuration is using : 63953 Bytes
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: Using LinkWatch kernel netlink reflector...
Mar 20 05:09:01 taotao Keepalived_healthcheckers[56539]: Using LinkWatch kernel netlink reflector...
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Entering BACKUP STATE
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Mar 20 05:09:01 taotao Keepalived_vrrp[56540]: VRRP_Script(chk_mysql) succeeded
Mar 20 05:09:16 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Transition to MASTER STATE
Mar 20 05:09:21 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Entering MASTER STATE
Mar 20 05:09:21 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) setting protocol VIPs.
Mar 20 05:09:21 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Sending gratuitous ARPs on eth0 for 192.168.79.66
Mar 20 05:09:21 taotao Keepalived_healthcheckers[56539]: Netlink reflector reports IP 192.168.79.66 added
Mar 20 05:09:26 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Sending gratuitous ARPs on eth0 for 192.168.79.66
ps:包含了三个进程: keepalived healthcheck vrrp协议 三个进程

测试:

1)关闭A服务的mysql服务观察A和B的日志
A:
[root@taotao ~]# /etc/init.d/mysqld stop
Shutting down MySQL....[ OK ]
[root@taotao ~]# tail -f /var/log/messages
Mar 20 05:36:04 taotao Keepalived_vrrp[56540]: VRRP_Script(chk_mysql) failed
Mar 20 05:36:07 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Entering FAULT STATE
Mar 20 05:36:07 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) removing protocol VIPs.
Mar 20 05:36:07 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Now in FAULT state
Mar 20 05:36:07 taotao Keepalived_healthcheckers[56539]: Netlink reflector reports IP 192.168.79.66 removed

B:
[root@taotao ~]# tail -f /var/log/messages
Mar 20 05:36:03 slave1 Keepalived_vrrp[48658]: VRRP_Instance(VI_MYSQL1) Transition to MASTER STATE
Mar 20 05:36:08 slave1 Keepalived_vrrp[48658]: VRRP_Instance(VI_MYSQL1) Entering MASTER STATE
Mar 20 05:36:08 slave1 Keepalived_vrrp[48658]: VRRP_Instance(VI_MYSQL1) setting protocol VIPs.
Mar 20 05:36:08 slave1 Keepalived_vrrp[48658]: VRRP_Instance(VI_MYSQL1) Sending gratuitous ARPs on eth0 for 192.168.79.66
Mar 20 05:36:08 slave1 Keepalived_healthcheckers[48657]: Netlink reflector reports IP 192.168.79.66 added
Mar 20 05:36:13 slave1 Keepalived_vrrp[48658]: VRRP_Instance(VI_MYSQL1) Sending gratuitous ARPs on eth0 for 192.168.79.66

2)开启A服务的mysql服务观察A日志

A:
[root@taotao ~]# /etc/init.d/mysqld start
Starting MySQL...........................[ OK ]
[root@taotao ~]# tail -f /var/log/messages

Mar 20 05:42:01 taotao Keepalived_vrrp[56540]: VRRP_Script(chk_mysql) succeeded
Mar 20 05:42:03 taotao Keepalived_vrrp[56540]: VRRP_Instance(VI_MYSQL1) Entering BACKUP STATE

通过vip连接mysql

# mysql -h 192.168.79.66 -P 3306 -uliyt -p

keepalived+mysql双主复制高可用方案的更多相关文章

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

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

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

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

  3. MySQL 高可用性—keepalived+mysql双主(有详细步骤和全部配置项解释)

    博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 前言:生产环境中一台mysql主机存在单 ...

  4. keepalived+MySQL双主搭建

    keepalived+MySQL双主搭建过程 首先要简单了解一下keepalived: Keepalived是Linux下一个轻量级别的高可用解决方案.高可用(High Avalilability,H ...

  5. 分布式数据存储 - MySQL双主复制

    上篇文章<分布式数据存储 - MySQL主从复制>,我们说到MySQL主从复制很好的保障了从库,读的高可用性.so,问题来了: 1.针对主库,写的高可用性又是如何做到高可用性? 2.如果需 ...

  6. MySQL 高可用性—keepalived+mysql双主

    MySQL 高可用性—keepalived+mysql双主(有详细步骤和全部配置项解释) - 我的博客 - CSDN博客https://blog.csdn.net/qq_36276335/articl ...

  7. MySQL双主复制

    原文发表于cu:2017-06-12 本文简单介绍MySQL双主复制原理及1个简单是双主复制验证. 一.MySQL双主复制原理 1. 双主复制原理 master-master复制的两台服务器,既是ma ...

  8. Keepalived+MySQL双主

    一.Keepalived+MySQL Replication的应用场景 MySQL的高可用方案有cluster,MMM,MHA等,这些高可用方案都要三台服务器以上,成本有点高,今天介绍一个低成本高可用 ...

  9. mysql双主复制总结

    双主复制: 1).在两台服务器上各自建立一个具有复制权限的用户: 2).修改配置文件: # 主服务器A上 [mysqld] server-id = 10 log-bin = mysql-bin rel ...

随机推荐

  1. (原创) mac 10.9.2 eclipse 的 CDT 的 异常的修复

    测试平台:macbook air 2012 , os x 10.9.2 , eclipse 4.3   在升级了 10.9 之后,eclipse 的CDT 无法正常使用了   异常表现:   1. 文 ...

  2. CDZSC_2015寒假新人(2)——数学 P

    P - P Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  3. c#或获取系统的特殊路径,如我的文档等

    Console.WriteLine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); System.E ...

  4. zepto.1.1.6.js源码中的each方法学习笔记

    each方法接受要遍历的对象和对应的回调函数作为参数,它的作用是: 1.如果要遍历的对象是类似数组的形式(以该对象的length属性值的类型是否为number类型来判断),那么就把以要遍历的对象为执行 ...

  5. spring3+hibernate3+(dbcp+oracle+拦截器事务配置)整合(一)

    1.applicationContext-base.xml文件 <?xml version="1.0" encoding="UTF-8"?>< ...

  6. Response 关于浏览器header的方法

    Response.AddHeader   Response.AddHeader使用实例 1.文件下载,指定默认名 Response.AddHeader("content-type" ...

  7. Python学习笔记五,函数及其参数

    在Python中如何自定义函数:其格式为 def 函数名(函数参数): 内容

  8. 高可用集群(HA)之Keeplived原理+配置过程

    原理--> 通过vrrp协议,定义虚拟路由,在多个服务节点上进行转移. 通过节点优先级,将初始虚拟路由到优先级高的节点上,checker工作进程检测到主节点出问题时,则降低此节点优先级,从而实现 ...

  9. C++----练习--string输入输出

    1.要使用标准库中的string 类型.也要增加头文件 #include<string> 2.例子: #include<iostream> #include<string ...

  10. [分享]我的LABVIEW快速开发串口测试软件实例

    LABVIEW是美国国家仪器公司(National Instruments,简称NI)的创新产品,它允许编程人员使用图形方式来进行编程,摒弃了艰涩难懂的代码,只需要拖动相应图形控件然后连线,进行简单的 ...