MySQL架构——mmm部署实例

时间:2022-06-25 07:18:30

mmm架构的来源

  • 众所周知,MySQL自身提供了主从复制,然后可以很轻松实现master-master双向复制,同时再为其中一个Master节点搭建一个Slave库。这样就实现了MySQL-MMM架构的基础:master1和master2之间双向复制,同时Master1和Slave1之间是主从复制。
  • 这样整个体系中存在两个Master,正常情况下只有一个master对外提供写服务。如果对外提供服务的master意外宕机了,这是MySQL本身并不具备failover切换的能力,尽管集群中仍然有一个正常的master节点,但应用仍不可用。mysql-mmm就是为了解决这个问题诞生的。

mmm架构的原理

  • MySQL-MMM是Master-Master Replication Manager for MySQL(mysql主主复制管理器)的简称,是Google的开源项目(Perl脚本),主要用来监控mysql主主复制并做失败转移
  • 其原理是将真实数据库节点的IP(RIP)映射为虚拟IP(VIP)集,在这个虚拟的IP集中,有一个专用于write的IP,多个用于read的IP,这个用于Write的VIP映射着数据库集群中的两台master的真实IP(RIP),以此来实现Failover的切换,其他read的VIP可以用来均衡读(balance)。

mmm机构优缺点

优点

  • 使用Perl脚本语言开发及完全开源
  • 提供了读写VIP(虚拟IP),使服务器角色的变更对前端应用透明
  • MMM提供了从服务器的延迟监控
  • MMM提供了主数据库故障转移后从服务器对新主的重新同步功能
  • 很容易对发生故障的主数据库重新上线

缺点

  • 发布时间比较早不支持MySQL新的复制功能(基于GTID的复制)
  • 没有读负载均衡的功能
  • 在进行主从切换时,容易造成数据丢失
  • MMM监控服务存在单点故障

mmm架构原理图

MySQL架构——mmm部署实例

实验部署

环境部署

master1IP地址:192.168.144.167
master2IP地址:192.168.144.151
slave1IP地址:192.168.144.168
slave2IP地址:192.168.144.145
monitorIP地址:192.168.144.164

在master1master2slave1slave2服务器中安装MYSQL数据库

[[email protected] ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo   //获取源地址
[[email protected] ~]# yum -y install epel-release      //安装epel源
[[email protected] ~]# yum clean all && yum makecache     //yum缓存清空
[[email protected] ~]# yum -y install mariadb-server mariadb      //安装mariadb数据库
[[email protected] ~]# systemctl stop firewalld.service       //关闭防火墙
[[email protected] ~]# setenforce 0
[[email protected] ~]# systemctl start mariadb.service        //开启数据库

修改master1数据库配置文件

[[email protected] ~]# vim /etc/my.cnf        //进入编辑配置文件
[mysqld]
log_error=/var/lib/mysql/mysql.err        //错误日志文件
log=/var/lib/mysql/mysql_log.log           //主从日志存放位置
log_slow_queries=/var/lib/mysql_slow_queris.log     //man日志
binlog-ignore-db=mysql,information_schema         //二进制文件
character_set_server=utf8        //字符集
log_bin=mysql_bin             //二进制日志文件
server_id=11            //服务id(不能相同)
log_slave_updates=true   //允许从服务器更新
sync_binlog=1            //同步日志
auto_increment_increment=2    //自增列
auto_increment_offset=1               //起始点
[[email protected] ~]# systemctl restart mariadb.service   //重启数据库

使用scp复制数据库配置文件到其他MySQL服务,并在其他MySQL服务器中修改server_id

[[email protected] ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=22)
[[email protected] ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=33)
[[email protected] ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=44)
[[email protected] ~]# netstat -anpt | grep 3306       //查看端口3306
tcp      0   0 0.0.0.0:3306     0.0.0.0:*         LISTEN      4235/mysqld  

配置主主复制

master1

[[email protected] ~]# mysql   ##进入数据库

MariaDB [(none)]> show master status;   ##查看主服务器的状态信息
 ------------------ ---------- -------------- -------------------------- 
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
 ------------------ ---------- -------------- -------------------------- 
| mysql_bin.000001 |      245 |              | mysql,information_schema |
 ------------------ ---------- -------------- -------------------------- 
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to ‘replication‘@‘192.168.144.%‘ identified by ‘123456‘;     //授权给144段的网段复制的权限用户名replication密码123456
Query OK, 0 rows affected (0.00 sec)

master2

MariaDB [(none)]> change master to master_host=‘192.168.144.167‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245;     //在master2上同步master1服务器
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to ‘replication‘@‘192.168.144.%‘ identified by ‘123456‘;    //在master2上授权复制权限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;       //查看master2的服务器的状态信息
 ------------------ ---------- -------------- -------------------------- 
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
 ------------------ ---------- -------------- -------------------------- 
| mysql_bin.000001 |      410 |              | mysql,information_schema |
 ------------------ ---------- -------------- -------------------------- 
1 row in set (0.00 sec)

MariaDB [(none)]> flush privileges;     //刷新权限
Query OK, 0 rows affected (0.00 sec)

master1

MariaDB [(none)]> change master to master_host=‘192.168.144.151‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=410;       //master1同步master2服务器数据库
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

master1,master2上开启同步

MariaDB [(none)]> start slave;              //开启同步
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave statusG;       //查看同步状态信息
                         ...
                         Slave_IO_Running: Yes
                        Slave_SQL_Running: Yes
                        ...

slave1,slave2做主从同步

MariaDB [(none)]> change master to master_host=‘192.168.144.167‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245;       //在从服务器上同步master1主服务器
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;       //刷新权限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;       //开启同步
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave statusG;        //查看同步的状态信息
                             ...
                             Slave_IO_Running: Yes
                            Slave_SQL_Running: Yes
                            ...

测试主主,主从的同步状态

主服务器master1

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               |
 -------------------- 
5 rows in set (0.00 sec)

slave服务器上查看数据库

MariaDB [(none)]> show databases;    //实现主主,主从同步
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
 -------------------- 
5 rows in set (0.00 sec)

monitor服务器上配置epel-release源清空缓存,然后安装MMM

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

yum -y install epel-release

yum clean all && yum makecache

yum -y install mysql-mmm*

所有MySQL服务器上安装mmm

yum -y install mysql-mmm*

所有服务器上对mmm进行配置

 [[email protected] ~]# vim /etc/mysql-mmm/mmm_common.conf     //所有主机上都要配置,直接复制多份

……
<host default>
        cluster_interface       ens33    //修改网卡
        …
        replication_user        replication      //修改用户名
        replication_password    123456         //密码
        agent_user              mmm_agent
        agent_password          123456         //密码

<host db1>
        ip      192.168.144.167   //master1地址
        mode    master
        peer    db2
</host>

<host db2>
        ip      192.168.144.151   //master2地址
        mode    master
        peer    db1
</host>

<host db3>
        ip      192.168.144.168   //slave1地址
        mode    slave
</host>

<host db4>
        ip      192.168.144.145   //slave2地址
        mode    slave
</host>

<role writer>
        hosts   db1, db2      ##写服务器虚拟ip
        ips     192.168.144.250
        mode    exclusive
</role>

<role reader> 
        hosts   db3, db4    ##读服务器虚拟ip
        ips     192.168.144.251, 192.168.144.252
        mode    balanced
</role>

##复制到其他的服务器中
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]‘s password: 
mmm_common.conf                               100%  836   267.1KB/s   00:00    
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]‘s password: 
mmm_common.conf                               100%  836   863.2KB/s   00:00    
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]‘s password: 
mmm_common.conf                               100%  836   904.7KB/s   00:00    
[[email protected] ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/

在monitor服务器上配置

[[email protected] ~]# vim /etc/mysql-mmm/mmm_mon.conf
<host default>
        monitor_user        mmm_monitor
        monitor_password    123456    ##修改monitor的密码
</host>

在所有数据库上为mmm_agent代理授权,为mmm_monitor授权监控

MariaDB [(none)]> grant super, replication client, process on *.* to ‘mmm_agent‘@‘192.168.144.%‘ identified by ‘123456‘;     //授权代理
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication client on *.* to ‘mmm_monitor‘@‘192.168.144.%‘ identified by ‘123456‘;        //授权监控
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;       //刷新权限
Query OK, 0 rows affected (0.00 sec)

修改所有数据库的mmm_agent.conf

[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db1 //根据规划进行逐一调整
[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db2 //根据规划进行逐一调整
[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db3 //根据规划进行逐一调整
[[email protected] ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db4 //根据规划进行逐一调整
##所有数据库开启
[[email protected] ~]systemctl start mysql-mmm-agent.service        //开启代理服务
[[email protected] ~]systemctl enable mysql-mmm-agent.service        //加入开机自启动

在monitor上配置

[[email protected] ~]# vim /etc/mysql-mmm/mmm_mon.conf
<monitor>
        ip                  127.0.0.1
        pid_path            /run/mysql-mmm-monitor.pid
        bin_path            /usr/libexec/mysql-mmm
        status_path         /var/lib/mysql-mmm/mmm_mond.status
        ping_ips            192.168.144.167,192.168.144.151,192.168.144.168,192.168.144.145    //所有数据库服务器地址
        auto_set_online     10        //自动在线时间
[[email protected] ~]# systemctl stop firewalld.service        //关闭防火墙
[[email protected] ~]# setenforce 0
[[email protected] ~]# systemctl start mysql-mmm-monitor.service       /开启监控服务
[[email protected] ~]# mmm_control show            //查看主从的飘逸地址
    db1(192.168.144.167) master/ONLINE. Roles: writer(192.168.144.250)
    db2(192.168.144.151) master/ONLINE. Roles: 
    db3(192.168.144.168) slave/ONLINE. Roles: reader(192.168.144.252)
    db4(192.168.144.145) slave/ONLINE. Roles: reader(192.168.144.251)

测试漂移地址

[[email protected] ~]# systemctl stop mariadb.service     //模拟停止master1服务器

monitor服务器上查看

[[email protected] ~]# mmm_control show
    db1(192.168.144.167) master/HARD_OFFLINE. Roles: 
    db2(192.168.144.151) master/ONLINE. Roles: writer(192.168.144.250)
    db3(192.168.144.168) slave/ONLINE. Roles: reader(192.168.144.252)
    db4(192.168.144.145) slave/ONLINE. Roles: reader(192.168.144.251)
(重启master1数据库服务,虚拟地址不会被抢占到master1)
mmm_control checks all //需要各种OK
mmm_control move_role writer db1      //可以切换虚拟地址

在monitor上安装MySQL作为测试机用虚拟ip登录数据库

[[email protected] ~]# yum install mysql -y

//master1服务器上授权monitor地址访问//

MariaDB [(none)]> grant all on *.* to ‘testdba‘@‘192.168.144.164‘ identified by ‘123456‘;
##授权monitor地址访问
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;   ##刷新权限
Query OK, 0 rows affected (0.00 sec)

[[email protected] ~]# mysql -utestdba -p -h 192.168.144.250   ##使用虚拟地址即可登录数据库
Enter password: 

MariaDB [(none)]>