mysql高可用-读写分离-双主多从

一.体系架构

在Keepalived + amoeba高可用负载均衡架构中,keepalived负责实现High-availability (HA) 功能控制前端机VIP(虚拟网络地址),当有设备发生故障时,热备服务器可以瞬间将VIP自动切换过来,实际运行中体验只有2秒钟切换时间,
通过amoeba mysql中间件对master和slave进行读写分离,主master负责写,两台slave负责读,备master负责实时同步主master的数据

二. 优点

  1. 读写分离
  2. mysql slave支持横向扩展,提高性能
  3. 数据库内容支持定期备份,在发生重大操作失误后可以进行回退操作
  4. mysql master热备,能在master出现问题后自动切换到另一台备份master上继续使用

三. 系统环境

两台负载机器安装:centos7.2+docker+amoeba+keepalived,分别命名为:MYSQL_MASTER,MYSQL_BACKUP。
多台mysql slave,这里使用两台

服务器 操作系统 IP地址 安装软件
MYSQL_MASTER Centos 7.2 64位 10.141.1.31 docker+amoeba+keepalived
MYSQL_BACKUP Centos 7.2 64位 10.141.1.5 docker+amoeba+keepalived
MYSQL_SLAVE_1 Centos 7.2 64位 10.141.1.12 docker+mysql
MYSQL_SLAVE_2 Centos 7.2 64位 10.141.1.25 docker+mysql
虚拟IP 10.141.1.8

四. 搭建环境

1. 主机准备

全部主机执行命令

setenforce 0 #关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld #关闭防火墙
systemctl stop iptables #关闭iptables

2. docker安装(全部主机执行命令)

a. 在线安装

参考: (https://docs.docker.com/install/linux/docker-ce/centos/#uninstall-old-versions)

yum install docker

b. 离线二进制安装:

参考:(https://www.jianshu.com/p/46b9a351f749)

3. 安装mysql(全部安装)

a. 拉取镜像

docker pull mysql:5.7

b. 修改mysql配置文件,vim mysqld.cnf

[mysqld]
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
datadir     = /var/lib/mysql
#log-error  = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server_id = 11               #备份写12,不能一样
auto-increment-offset = 1    #备份mysql填写0
auto-increment-increment = 2 

c. 启动mysql

docker run --name mysql -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 -v /${PWD}/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -d mysql:5.7

4. 设置mysql双主多从

a. 赋权

(1)master给backup和两个slave增加复制账号

grant replication slave on *.* to 'repl'@'10.141.1.5' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.12' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.25' identified by '123456';
flush  privileges;

(2)backup给master和两个slave增加复制账号

grant replication slave on *.* to 'repl'@'10.141.1.31' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.12' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.25' identified by '123456';
flush  privileges;

(3)设置backup同步master

show master status; #在master执行,记住File和Position 值
change master to master_host='10.141.1.31',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1207; #在backup执行,master_log_file为上面的File值,master_log_pos为Position 值
start slave; #启动
show slave status\G; 查看Slave_IO_Running和Slave_SQL_Running两个值为yes即成功
image.png

(4)设置master同步backup

show master status; #在backup执行,记住File和Position 值
change master to master_host='10.141.1.5',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1050; #在master执行,master_log_file为上面的File值,master_log_pos为Position 值
start slave; #启动
show slave status\G; 查看Slave_IO_Running和Slave_SQL_Running两个值为yes即成功
image.png

(5)设置slave同步master

show master status; #在master执行,记住File和Position 值
change master to master_host='10.141.1.8',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1207; #在backup执行,master_log_file为上面的File值,master_log_pos为Position 值
start slave; #启动
show slave status\G; 查看Slave_IO_Running和Slave_SQL_Running两个值为yes即成功

5. 读写分离

a. 分配amoeba账号,全部执行

grant all on *.* to 'amoeba'@'%' identified by '123';

b. 启动amoeba服务

docker run --name amoeba -it -d -p 8066:8066 -v /${PWD}/conf:/usr/local/amoeba/conf commanderhu/amoeba

启动后修改conf下的amoeba.xml ,dbServers.xml 文件,然后重启

docker restart amoeba

c. 测试

mysql -uamoeba -p123 -P8066 -h10.141.1.8
show databases;

6. keepalived安装配置

参考(https://www.jianshu.com/p/72dfd4d4cf9bc

master配置

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_mysql_port {     #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/root/chk_mysql.sh"   #这里通过脚本监测
    interval 2                   #脚本执行间隔,每2s检测一次
    weight -10                    #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
    rise 1                    #检测1次成功就算成功。但不修改优先级
}

vrrp_instance VI_1 {
    state MASTER
    #nopreempt
    notify_master /root/to_master.sh
    unicast_src_ip  10.141.1.29
    unicast_peer {
                 10.141.1.24
    }
    interface eth0
    virtual_router_id 58
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.141.1.8
    }

track_script {               
   chk_mysql_port             
}
}

slave配置

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script chk_mysql_port {     #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/root/chk_mysql.sh"   #这里通过脚本监测
    interval 2                   #脚本执行间隔,每2s检测一次
    weight -10                    #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
    rise 1                    #检测1次成功就算成功。但不修改优先级
}

vrrp_instance VI_1 {
    state BACKUP
    notify_master /root/to_master.sh
    #nopreempt
    unicast_src_ip  10.141.1.24
    unicast_peer {
                 10.141.1.29
    }
    interface eth0
    virtual_router_id 58
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.141.1.8
    }

   track_script {
     chk_mysql_port
   }
}

slave变master执行的脚本

re=`mysql -h10.141.1.29 -uroot -p1234 -e "show master status;" | grep -v File`

echo $re

binlog=`echo $re | awk '{print $1}'`
position=`echo $re | awk '{print $2}'`
echo $binlog ------ $position

mysql -h10.141.1.30 -uroot -p1234 -e "stop slave;change master to master_host='10.141.1.8',master_port=3306,master_user='repl',master_password='123456',master_log_file='$binlog',master_log_pos=$position;start slave;show slave status\G;"
mysql -h10.141.1.4 -uroot -p1234 -e "stop slave;change master to master_host='10.141.1.8',master_port=3306,master_user='repl',master_password='123456',master_log_file='$binlog',master_log_pos=$position;start slave;show slave status\G;"

检查脚本

counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    #systemctl stop keepalived
    exit 1
fi
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,961评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,444评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,009评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,082评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,101评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,271评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,738评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,395评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,539评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,434评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,481评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,160评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,749评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,816评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,038评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,548评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,140评论 2 341