【数据库】mysql架构之MMM,高可用同时实现读写分离

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 文章目录前言一、mmm架构1.0 思路1.1 架构

一、mmm架构

1.0 思路

  • 主从复制:先安装数据库–>主服务器授权,刷新授权–>从节点同步配置(需先获得主的文件名及偏移量)
  • 注:两个主需要互相做主从,为了故障转换之后数据能继续同步

1.1 架构

作用 IP地址 安装服务
主负载均衡器 192.168.13.10 mmm/mysql5.7
备负载均衡器 192.168.13.20 mmm/mysql5.7
节点服务器1 192.168.13.30 mmm/mysql5.7
节点服务器2 192.168.13.40 mmm/mysql5.7
monitor监控节点 192.168.13.50 mmm

1.2 主负载均衡

systemctl stop firewalld
setenforce 0
vim /etc/my.cnf
  server-id = 1
  log-error=/usr/local/mysql/data/mysql_error.log
  general_log=ON
  general_log_file=/usr/local/mysql/data/mysql_general.log
  slow_query_log=ON
  slow_query_log_file=mysql_slow_query.log
  long_query_time=5
  binlog-ignore-db=mysql,information_schema
  log_bin=mysql_bin
  log_slave_updates=true
  sync_binlog=1
  innodb_flush_log_at_trx_commit=1
  auto_increment_increment=2
  auto_increment_offset=1
scp /etc/my.cnf root@192.168.13.20:/etc/
scp /etc/my.cnf root@192.168.13.30:/etc/
scp /etc/my.cnf root@192.168.13.40:/etc/
systemctl restart mysqld
mysql -uroot -pabc123
  grant replication slave on *.* to 'replication'@'192.168.13.%' identified by '123456';
  show master status;
  #此处获得二进制日志文件名及偏移量,其他服务器配置主从复制时候需要
  grant super, replication client, process on *.* to 'mmm_agent'@'192.168.13.%' identified by '123456';
  grant replication client on *.* to 'mmm_monitor'@'192.168.13.%' identified by '123456';
  flush privileges;
  #为monitor服务器授权
  change master to master_host='192.168.13.20',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=460;
  #此时的二进制文件名与偏移量是从master2获得的,原因:当故障转移后,能够和新主保持主从复制(可以先不执行这一步,等操作完master2再回来授权)
  show slave status\G
  #查看主从同步是够配置成功
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum -y install mysql-mmm*
cd /etc/mysql-mmm/
vim mmm_common.conf
  4行:cluster_interface       ens33
  8行:replication_password    123456
  10行:agent_password          123456
  14行:ip      192.168.13.10
  20行:ip      192.168.13.20
  26行:ip      192.168.13.30
  24-28行复制:24,,28 co 28
  31行:ip      192.168.13.40
  35行:hosts   db1, db2
  37行:ips     192.168.13.188
  41行:hosts   db3, db4
  43行:ips     192.168.13.198, 192.168.13.199
scp mmm_common.conf root@192.168.13.20:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.13.30:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.13.40:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.13.50:/etc/mysql-mmm/
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service

1.3 备负载均衡器

systemctl stop firewalld
setenforce 0
vim /etc/my.cnf
  server-id = 2
systemctl restart mysqld.service
mysql -u root -pabc123
  grant replication slave on *.* to 'replication'@'192.168.13.%' identified by '123456';
  show master status;
  #此处获得二进制日志文件名及偏移量,为了给master1使用
  change master to master_host='192.168.13.20',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=460;
  #此时的二进制文件名与偏移量是从master1获得的,原因:为了保持数据一致性,随时可以进行故障转移
  grant super, replication client, process on *.* to 'mmm_agent'@'192.168.13.%' identified by '123456';
  grant replication client on *.* to 'mmm_monitor'@'192.168.13.%' identified by '123456';
  flush privileges;
  #为monitor服务器授权
  show slave status\G
  #查看主从同步是够配置成功
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum -y install mysql-mmm*
vim /etc/mysql-mmm/mmm_agent.conf
  6行:this db2
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service

1.4 从服务器

systemctl stop firewalld
setenforce 0
vim /etc/my.cnf
  server-id = 3
systemctl restart mysqld.service
mysql -u root -pabc123
  change master to master_host='192.168.13.10',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=460;
  #与master1保持主从复制
  grant super, replication client, process on *.* to 'mmm_agent'@'192.168.13.%' identified by '123456';
  grant replication client on *.* to 'mmm_monitor'@'192.168.13.%' identified by '123456';
  flush privileges;
  #为monitor服务器授权
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum -y install mysql-mmm*
vim /etc/mysql-mmm/mmm_agent.conf
  6行:this db3
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service
#另外一台从服务器重复此操作,server-id = 4,6行:this db4。修改这两项即可

1.5 monitor服务器

systemctl stop firewalld
setenforce 0
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum -y install mysql-mmm*
vim /etc/mysql-mmm/mmm_mon.conf
  8行:ping_ips        192.168.13.10,192.168.13.20,192.168.13.30,192.168.13.40
  9行:auto_set_online     10
  20行:monitor_user        mmm_monitor
  21行:onitor_password    123456
systemctl restart mysql-mmm-monitor.service
mmm_control show
#检查各节点的情况,正确显示如下
  db1(192.168.13.10) master/ONLINE. Roles: writer(192.168.13.188)
  db2(192.168.13.20) master/ONLINE. Roles: 
  db3(192.168.13.30) slave/ONLINE. Roles: reader(192.168.13.198)
  db4(192.168.13.40) slave/ONLINE. Roles: reader(192.168.13.199)
#mmm高可用搭建完毕
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
Prometheus 监控 关系型数据库
数据库同步革命:MySQL GTID模式下主从配置的全面解析
数据库同步革命:MySQL GTID模式下主从配置的全面解析
12 0
|
1天前
|
关系型数据库 MySQL 数据库
mysql 中文问号,mybatis-plus insert中文数据库显示问号
mysql 中文问号,mybatis-plus insert中文数据库显示问号
|
2天前
|
SQL 关系型数据库 数据库
17. Python 数据库操作之MySQL和SQLite实例
17. Python 数据库操作之MySQL和SQLite实例
31 2
|
2天前
|
SQL Oracle 关系型数据库
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
|
2天前
|
SQL 关系型数据库 MySQL
mysql 数据库导出导入到本地文件
mysql 数据库导出导入到本地文件
|
2天前
|
Oracle 关系型数据库 MySQL
数据库漫谈-MySQL
MySQL的发展大体上分为4个阶段:
|
2天前
|
SQL 监控 关系型数据库
MySQL慢查询日志配置指南:发现性能瓶颈,提升数据库效率
MySQL慢查询日志配置指南:发现性能瓶颈,提升数据库效率
10 0
|
2天前
|
关系型数据库 MySQL 分布式数据库
数据库专家带你体验PolarDB MySQL版 Serverless的极致弹性特性
作为数据库专家,我有幸带大家深入体验阿里巴巴自主研发的下一代关系型分布式云原生数据库——PolarDB MySQL版的Serverless极致弹性特性。在这个云原生和分布式技术飞速发展的时代,Pola
|
2天前
|
关系型数据库 MySQL Linux
Linux下mysql添加用户并授权数据库权限
Linux下mysql添加用户并授权数据库权限
19 0
|
2天前
|
关系型数据库 MySQL 5G
Mysql数据迁移3个快速方法与数据库恢复
Mysql数据迁移3个快速方法与数据库恢复
11 0