前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >双VIP-mysql主主

双VIP-mysql主主

作者头像
@凌晨
发布2020-05-28 17:25:45
2.8K0
发布2020-05-28 17:25:45
举报
文章被收录于专栏:Man_DockerMan_Docker

1.数据库架构图:

双VIP-mysql主主
双VIP-mysql主主

2.安装环境信息: master1 172.16.90.13 CentOS 7.2 Keepalived读 VIP:172.16.90.15 master2 172.16.90.14 CentOS 7.2 Keepalived读 VIP:172.16.90.16

3.MySQL双主配置

master1修改my.cnf,新增如下配置:

server-id=13
log-bin=mysql-bin
sync-binlog=1
binlog-checksum=none
binlog-format=mixed
auto-increment-increment=2
auto-increment-offset=1
log-slave-updates
slave-skip-errors=all

master2修改my.cnf,新增如下配置:

server-id=14
log-bin=mysql-bin
sync-binlog=1
binlog-checksum=none
binlog-format=mixed
auto-increment-increment=2
auto-increment-offset=1
log-slave-updates
slave-skip-errors=all

在master1中为mysql从库账户授权:

grant replication slave on . to 'sync'@'%' identified by 'syncpwd'; flush privileges; show master status; #当前主库状态,即master1 show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000004 | 599 | | | | +------------------+----------+--------------+------------------+-------------------+

在master2中为mysql从库账户授权:

grant replication slave on . to 'sync'@'%' identified by 'syncpwd'; flush privileges; show master status; #当前主库状态,即master2 show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 468 | | | | +------------------+----------+--------------+------------------+-------------------+

在maste1中指定master2为主库:

stop slave; change master to master_host='172.16.90.14',master_user='sync',master_password='syncpwd',master_log_file='mysql-bin.000002',master_log_pos=468; flush privileges; SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; start slave;

在maste2中指定master1为主库:

stop slave; change master to master_host='172.16.90.13',master_user='sync',master_password='syncpwd',master_log_file='mysql-bin.000004',master_log_pos=599; flush privileges; SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; start slave;

MySQL双主配置完成,验证配置成功:

show slave status\G #master1中显示的信息 1. row Slave_IO_State: Waiting for master to send event Master_Host: 172.16.90.13 Master_User: sync Master_Port: 3306 Slave_IO_Running: Yes Slave_SQL_Running: Yes show slave status\G #master2中显示的信息 1. row Slave_IO_State: Waiting for master to send event Master_Host: 172.16.90.14 Master_User: sync Master_Port: 3306 Slave_IO_Running: Yes Slave_SQL_Running: Yes

4.Keepalived高可用配置

安装:yum install -y keepalived 启动:systemctl stop keepalived 说明: 当两台服务器都正常的时候 用户写数据默认访问服务器A,如果A有异常则访问B服务器。 用户读数据默认访问服务器B,如果B有异常则访问A服务器。

服务器A的写数据初始权重为100,B为90 服务器A的读数据初始权重为90,B为100 检测进程检测到异常时,会使得本机的权重下降20

服务器A

vrrp_script chk_master1 {
    script "/opt/context/keepalive_check/chk_mysql.sh"
    interval 2            
    weight -20 
}

vrrp_instance VI_MASTER1 {
    state MASTER
    interface eno16780032
    virtual_router_id 51
    priority 100
    mcast_src_ip 172.16.90.13
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 5678
    }
    virtual_ipaddress {
        172.16.90.15
    }

    track_script {
        chk_master1
    }
}

vrrp_instance VI_MASTER2 {
    state BACKUP
    interface eno16780032
    virtual_router_id 52
    priority 90
    mcast_src_ip 172.16.90.13
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 15678
    }
    virtual_ipaddress {
        172.16.90.16
    }
}

服务器B

vrrp_script chk_master2 {
    script "/opt/context/keepalive_check/chk_mysql.sh"
    interval 2
    weight -20
}

vrrp_instance VI_MASTER1 {
    state BACKUP
    interface eno16780032
    virtual_router_id 51
    priority 90
    mcast_src_ip 172.16.90.14
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 5678
    }
    virtual_ipaddress {
        172.16.90.15
    }
}

vrrp_instance VI_MASTER2 {
    state MASTER
    interface eno16780032
    virtual_router_id 52
    priority 100
    mcast_src_ip 172.16.90.14
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 15678
    }
    virtual_ipaddress {
        172.16.90.16
    }

    track_script {
        chk_master2
    }
}

检测脚本

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    systemctl stop keepalived
fi
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档