mysql双主配置讲义
admin
2023-02-25 03:20:06
0

下文内容主要给大家带来mysql双主配置讲义,这里所讲到的知识,与书籍略有不同,都是专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮助。

结尾2

1.数据库架构图:
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

对于以上关于mysql双主配置讲义,如果大家还有更多需要了解的可以持续关注我们的行业推新,如需获取专业解答,可在官网联系售前售后的,希望该文章可给大家带来一定的知识更新。

上一篇:HTTPS 相关问题

下一篇:libnuma.so.1 is needed

相关内容

热门资讯

雷达、机库、营房、燃料库、飞机... 据《华盛顿邮报》5月6日报道,通过卫星影像分析发现,自2月28日战事爆发以来,伊朗空袭已在中东美军军...
从买买买到租租租,“租用一代”... “五一”假期还在路上,年轻人已经“租”起来了。 “租三天,不到300块钱。”五一放假前一周,清清已给...
【品牌】摩托罗拉大折叠屏新机定... 此前联想预热将于5月19日19点举行联想天禧AI一体多端全场景新品超能之夜活动,届时将带来多款新品,...
常州欣隽益取得接线端子用快速冲... 国家知识产权局信息显示,常州欣隽益科技有限公司取得一项名为“接线端子用快速冲切装置”的专利,授权公告...
美媒:特朗普在结束伊朗战争问题... 据“国会山”网站5月6日报道,周二晚间,美国总统特朗普突然宣布终止旨在打破伊朗对霍尔木兹海峡掌控的军...
和创硅材料取得熔融石英制品擦洗... 国家知识产权局信息显示,东海县和创硅材料有限公司取得一项名为“一种熔融石英制品的擦洗脱泥装置”的专利...
美商务部长再就爱泼斯坦案接受国... 5月6日,美国商务部长卢特尼克“自愿”就其与爱泼斯坦的关系接受美国国会众议院监督与政府改革委员会的问...
“五一”小长假黄金零售市场新变... 【大河财立方 记者 孙凯杰】 “五一”小长假,黄金价格出现一波短暂调整,线下黄金零售市场热度如何?5...
以军3周来首次袭击黎巴嫩首都 据以色列总理内塔尼亚胡和国防部长卡茨当地时间5月6日晚发表的联合声明,以军当天对黎巴嫩首都贝鲁特南郊...
东莞移动:5G-A智擎护航, ... “五一”期间,松山湖草莓音乐节、广东国际汽车展示交易会·春季、2026茶园游会田园文化美食节、大岭山...