MMM高可用架构

MMM高可用架构

MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,可以说是mysql主主复制管理器。虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡。关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写入),这个套件也能对居于标准的主从配置的任意数量的从服务器进行读负载均衡,所以你可以用它来在一组居于复制的服务器启动虚拟ip,除此之外,它还有实现数据备份、节点之间重新同步功能的脚本。


MMM提供了自动和手动两种方式移除一组服务器中复制延迟较高的服务器的虚拟ip,同时它还可以备份数据,实现两节点之间的数据同步等。由于MMM无法完全的保证数据一致性,所以MMM适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景。MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。对于那些对数据的一致性要求很高的业务,非常不建议采用MMM这种高可用架构。

MMM架构图:

MMM高可用架构

MMM这种模式,master,slaver容易被控死,两个就两个
MHA模式,有高可扩展性,一主双备,一个区域。再扩展一主双备,一个区域
都要安装node

首先我们开五台虚拟机,做规划

主服务器1 192.168.136.191 db1
主服务器2 192.168.136.168 db2
从服务器1 192.168.136.185 db3
从服务器2 192.168.136.184 db4
监控服务器 192.168.136.135

配置阿里云源。每台服务器都要装

wget -O /etc/yum.repos.d/CentOS-Base.repo httP://mirrors.aliyun.com/repo/Centos-7.repo

安装epel-release源,每台服务器都要装

yum -y install epel-release
yum clean all && yum makecache

搭建本地YUM源,所有服务器都要装,除了监控服务器


yum install mariadb-server mariadb -y`

修改主配置文件,出来监控服务器其他四台都要修改。复制,粘贴就行。

vim /etc/my.cnf
9dd
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queries.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1  #注意每台id都要是不一样的
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1

每台服务器都要关防火墙,增强功能,开启数据库

systemctl stop firewalld.service
setenforce 0
systemctl start mariadb.service 
[root@localhost ~]# mysql #进入M1数据库
进入M1服务器查看日志文件的名称和位置值
MariaDB [(none)]> show master status; #查看日志文件的名称和位置值
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

进入M2服务器查看日志文件的名称和位置值

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

两台主服务器都执行,给予权限

grant replication slave on *.* to 'replication'@'192.168.136.%' identified by '123456';`

M1服务器指向M2的服务器,地址,日志文件名称,位置参数


change master to master_host='192.168.136.168',master_user='relication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;

M2服务器指向M1的服务器,地址,日志文件名称,位置参数

change master to master_host='192.168.136.167',master_user='relication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
#两台服务器都执行开启同步数据
MariaDB [(none)]> slave start;
#两台主服务器都执行,查看同步数据的状态
MariaDB [(none)]> show slave statusG;
#看到下面的IO线程和状态都是YES就是正确了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#我们测试一下两个主服务器是否能同步数据
M1创建数据一个数据库
MariaDB [(none)]> create database myschool;
Query OK, 1 row affected (0.00 sec)
M2
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myschool           |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

------------------------------------------从服务器同步数据---------------------------------------

#s1和s2从服务器
[root@localhost ~]# mysql
#都指向M1主服务器的地址,日志文件和参数
MariaDB [(none)]> change master to master_host='192.168.136.191',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
#在M1创建一个数据库
MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.00 sec)
#其他三台服务器都有这个数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
#所有服务器都要装
yum -y install mysql-mmm*
#到第一台主服务器开始配置
[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# ls
mmm_agent.conf  mmm_common.conf  mmm_mon.conf  mmm_mon_log.conf  mmm_tools.conf
[root@localhost mysql-mmm]# vim mmm_common.conf
<host default>
cluster_interface       ens33
pid_path                /run/mysql-mmm-agent.pid
bin_path                /usr/libexec/mysql-mmm/
replication_user        replication
replication_password    123456
agent_user              mmm_agent
agent_password          123456
</host>
<host db1>
ip      192.168.136.191
mode    master
peer    db2
</host>
<host db2>
ip      192.168.136.168
mode    master
peer    db1
</host>
<host db3>
ip      192.168.136.185
mode    slave
</host>
<host db4>
ip      192.168.136.184
mode    slave
</host>
<role writer>
hosts   db1, db2
ips     192.168.136.200 #虚拟地址
mode    exclusive
</role>
<role reader>
hosts   db3, db4
ips     192.168.136.210, 192.168.136.220  #虚拟地址
mode    balanced

用scp把文件传给其他服务器

scp mmm_common.conf root@192.168.136.168:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.185:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.184:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.135:/etc/mysql-mmm/

配置监控服务器账号

[root@localhost mysql-mmm]# vim mmm_mon.conf
ping_ips            192.168.136.191,192.168.136.168,192.168.136.185,192.168.136.184 输入你所有的地址
auto_set_online     10  #自带上线时间为10s
monitor_password    123456  #修改密码为123456
在所有数据库给mmm_agent授权
grant super, replication client, process on *.* to 'mmm_agent'@'192.168.136.%' identified by '123456';
grant replication client on *.* to 'mmm_monitor'@'192.168.136.%' identified by '123456';

对应着修改配置文件(M2,S1,S2)

[root@localhost mysql-mmm]# vim mmm_agent.conf
this db2
this db3
this db4
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service
#回到监控服务器
systemctl start mysql-mmm-monitor.service 

查看各节点的情况

[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles: writer(192.168.136.200)
db2(192.168.136.168) master/ONLINE. Roles:
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#更改绑定的虚拟地址
[root@localhost mysql-mmm]# mmm_control move_role writer db2
#测试监控服务器功能是否完善
[root@localhost mysql-mmm]# mmm_control checks all
db4  ping         [last change: 2019/11/25 16:38:25]  OK
db4  mysql        [last change: 2019/11/25 16:38:25]  OK
db4  rep_threads  [last change: 2019/11/25 16:38:25]  OK
db4  rep_backlog  [last change: 2019/11/25 16:38:25]  OK: Backlog is null
db2  ping         [last change: 2019/11/25 16:38:25]  OK
db2  mysql        [last change: 2019/11/25 16:38:25]  OK
db2  rep_threads  [last change: 2019/11/25 16:38:25]  OK
db2  rep_backlog  [last change: 2019/11/25 16:38:25]  OK: Backlog is null
db3  ping         [last change: 2019/11/25 16:38:25]  OK
db3  mysql        [last change: 2019/11/25 16:38:25]  OK
db3  rep_threads  [last change: 2019/11/25 16:38:25]  OK
db3  rep_backlog  [last change: 2019/11/25 16:38:25]  OK: Backlog is null
db1  ping         [last change: 2019/11/25 16:38:25]  OK
db1  mysql        [last change: 2019/11/25 16:38:25]  OK
db1  rep_threads  [last change: 2019/11/25 16:38:25]  OK
db1  rep_backlog  [last change: 2019/11/25 16:38:25]  OK: Backlog is null
[root@localhost mysql-mmm]# mmm_control move_role writer db1
OK: Role 'writer' has been moved from 'db2' to 'db1'. Now you can wait some time and check new roles info!
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles: writer(192.168.136.200)
db2(192.168.136.168) master/ONLINE. Roles:
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#第一台主服务器关闭数据库模拟故障
[root@localhost mysql-mmm]# systemctl stop mariadb.service
#回到监控服务器测试,虚拟网址就变更了
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/HARD_OFFLINE. Roles:
db2(192.168.136.168) master/ONLINE. Roles: writer(192.168.136.200)
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#再把第一台主服务器开启数据库
[root@localhost mysql-mmm]# systemctl start mariadb.service
#在回到监控服务器查看主服务器状态
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles:
db2(192.168.136.168) master/ONLINE. Roles: writer(192.168.136.200)
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#监控服务器
[root@localhost mysql-mmm]# yum install mariadb-server mariadb -y
#再M1服务器为监控器地址授权登录
MariaDB [(none)]> grant all on *.* to 'testba'@'192.168.136.135' identified by '123456';
MariaDB [(none)]> flush privileges;

回到监控服务器测试一下能不能登录

mysql -utestdba -p -h 192.168.136.200
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 2562
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> 

创建一个表格再去其他服务器查看数据有没有被同步

MariaDB [(none)]> create database chen;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chen               |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
6 rows in set (0.01 sec)

以上就是我们全部的内容,谢谢收看