前言:MySQL5.7+keepalived双主配置MySQL高可用相对来说是比较成熟的方案,下面我们简单来说一下主要的配置步骤。
一、配置双主
这步相对来说比较简单,我们只讨论GTID模式下的主从配置。
- 从库配置:
change master to master_host='master_ip',master_port=port,master_user='repl',master_password='xxxx',master_auto_position=1;
- 主库配置
change master to master_host='slave_ip',master_port=port,master_user='repl',master_password='xxxx',master_auto_position=1;
二、配置keepalived
1.安装keepalived
yum install keepalived
安装成功后可以查看版本
[root@master vagrant]# keepalived -v
Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Copyright(C) 2001-2017 Alexandre Cassen, <acassen@gmail.com>
2.更改配置文件
vim /etc/keepalived/keepalived.conf
[root@master keepalived]# cat keepalived.conf
vrrp_script vs_mysql_82 {
script "/etc/keepalived/checkMySQL.py -h 192.168.50.3 -P 3306"
interval 60
}
vrrp_instance VI_82 {
state BACKUP
nopreempt
interface enp0s8
virtual_router_id 82
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
vs_mysql_82
}
virtual_ipaddress {
192.168.50.5
}
}
增加MySQL检查脚本
[root@master keepalived]# cat checkMySQL.py
#!/usr/bin/python
#coding: utf-8
#
import sys
import os
import getopt
import MySQLdb
import logging
dbhost='localhost'
dbport=3306
dbuser='monitor'
dbpassword='m0n1tor'
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.dbpassword)
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)
从库做同样配置,注意修改绑定的网卡和真实的IP,两个节点启动keepalived即可
网友评论