MySQL基操---高可用架构之MMM搭建与测试

MMM介绍

MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡。

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

MySQL-MMM优缺点

优点:高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证的数据的一致性。

缺点:Monitor节点是单点,可以结合Keepalived实现高可用。

MySQL-MMM工作原理

MMM(Master-Master replication managerfor Mysql,Mysql主主复制管理器)是一套灵活的脚本程序,基于perl实现,用来对mysql replication进行监控和故障迁移,

并能管理mysql Master-Master复制的配置(同一时间只有一个节点是可写的)。

mmm_mond:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监管机上运行。

mmm_agentd:运行在每个mysql服务器上(Master和Slave)的代理进程,完成监控的探针工作和执行简单的远端服务设置。此脚本需要在被监管机上运行。

mmm_control:一个简单的脚本,提供管理mmm_mond进程的命令。

mysql-mmm的监管端会提供多个虚拟IP(VIP),包括一个可写VIP,多个可读VIP,通过监管的管理,这些IP会绑定在可用mysql之上,当某一台mysql宕机时,监管会将VIP迁移

至其他mysql。在整个监管过程中,需要在mysql中添加相关授权用户,以便让mysql可以支持监理机的维护。授权的用户包括一个mmm_monitor用户和一个mmm_agent用户,如果

想使用mmm的备份工具则还要添加一个mmm_tools用户。

实验基本环境

实验系统:CentOS 7_x86_64

实验前提:防火墙和selinux都关闭!

实验说明:本实验共有5台主机,IP分配如表

实验软件:mariadb     mysql-mmm mysql-mmm-monitor mysql-mmm-agent

功能,IP地址与别名分配表

功能 IP id
主服务器一 192.168.137.10 M1
主服务器二 192.168.137.11 M2
从服务器一 192.168.137.12 s1
从服务器二 192.168.137.13 s2
mmm监控 192.168.137.14 mo

实验描述拓扑图

10907200502.jpeg

操作!

所有机器中!

关闭所有机器的防火墙和安全策略
systemctl stop firewalld.service 

setenforce 0获取阿里云的yum基础配置

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

安装epel源

yum -y install epel-release

清空yum缓存,重新建立元数据

yum clean all && yum makecache

安装mariadb,MMM的客户端和服务端

yum install mariadb-server mariadb –y
yum install mysql-mmm* -y


M1中

修改m1配置文件,配置mariadb
# 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
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1log_slave_updates=true              //允许slave更新日志
sync_binlog=1auto_increment_increment=2
auto_increment_offset=1
启动服务
systemctl start mariadb.service



使用scp命令把my.cnf这个配置文件同步复制到其他的三台mysql主机上,除了监控机器MO。
scp /etc/my.cnf [email protected]:/etc/
scp /etc/my.cnf [email protected]:/etc/
scp /etc/my.cnf [email protected]:/etc/

务必单独修改每台机器/etc/my.cnf中
server_id=1这里M2改成了
server_id=2以此类推

逐个验证时候成功安装

[root@cent ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2Server version: 5.5.60-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)]>

主从复制,主主复制

M1,M2中

查看日志以及位置参数

MariaDB [(none)]> show master status;

两台得出的结果是相同的
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

M1,M2互相授权

M1,M2:
MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.137.%' identified by '123456';

同步
M1:
change master to master_host='192.168.137.11',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;


M2:
change master to master_host='192.168.137.10',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;


S1,S2中的同步,这里都以M1为同步对象。

change master to master_host='192.168.137.10',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
flush privileges;   //刷新

验证salve

启动salve
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
查看slave状态
MariaDB [(none)]> show slave status\G
看到这两项为yes即成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

配置MMM

随便一台机器

修改如下

vim /etc/mysql-mmm/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 db1>
    ip      192.168.137.10                     //M1地址
    mode    master
    peer    db2                                //指向M2
    </host>
    
    <host db2>
    ip      192.168.137.11                     //M2地址
    mode    master
    peer    db1                                //指向M1
    </host>
    
    <host db3>
    ip      192.168.137.12                     //S1地址
    mode    slave
    </host>
    
    <host db4>
    ip      192.168.137.13                    //S2地址
    mode    slave
    </host>
    
    <role writer>
    hosts   db1, db2                         //M1与M2
    ips     192.168.137.100                  //M1与M2共同虚拟一个IP
    mode    exclusive
    </role>
    
    <role reader>
    hosts   db3, db4                                //S1与S2
    ips     192.168.137.200, 192.168.137.150   //S1,S2的虚拟IP
    mode    balanced</role>
    
复制到其他四台主机,包括MO监控机   
scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/

MO监控机操作

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.137.10,192.168.137.11,192.168.137.12,192.168.137.13,192.168.137.14//列出所有数据库IP
    auto_set_online     10     //链接等待时间
    
    # The kill_host_bin does not exist by default, though the monitor will
    # throw a warning about it missing.  See the section 5.10 "Kill Host
    # Functionality" in the PDF documentation.
    #
    # kill_host_bin     /usr/libexec/mysql-mmm/monitor/kill_host
    #</monitor>
    
    <host default>
    monitor_user        mmm_monitor
    monitor_password    123456        //密码
    </host>
    debug 0


再MySQL中给MMM授权,每台机器都要

grant super, replication client,process on *.* to 'mmm_agent'@'192.168.137.%' identified by '123456';
grant replication client on *.* to 'mmm_monitor'@'192.168.137.%' identified by '123456';
flush privileges;         //刷新


更新每台机器的server名,不能相同,我设置了12345。

vim /etc/mysql-mmm/mmm_agent.conf
修改如下
this db1

设置五台!


启动服务

M1,M2,S1,S2启动agent
systemctl start mysql-mmm-agent.service

systemctl enable mysql-mmm-agent.service

监控机启动监控服务

systemctl start mysql-mmm-monitor.service

监控服务的查验,如下一切正常!

查看被监控的主机
[root@zcent4 mysql-mmm]# mmm_control show
  db1(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100)
  db2(192.168.137.11) master/ONLINE. Roles: 
  db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.200)
  db4(192.168.137.13) slave/ONLINE. Roles: reader(192.168.137.150)

切换虚拟ip绑定的主机
[root@zcent4 mysql-mmm]# mmm_control move_role writer db2
OK: Role 'writer' has been moved from 'db1' to 'db2'. Now you can wait some time and check new roles info![root@zcent4 mysql-mmm]# mmm_control show
  db1(192.168.137.10) master/ONLINE. Roles: 
  db2(192.168.137.11) master/ONLINE. Roles: writer(192.168.137.100)
  db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.200)
  db4(192.168.137.13) slave/ONLINE. Roles: reader(192.168.137.150)

检测监控功能完善性
[root@zcent4 mysql-mmm]# mmm_control checks alldb4  ping         [last change: 2018/09/07 23:02:12]  OK
db4  mysql        [last change: 2018/09/07 23:03:06]  OK
db4  rep_threads  [last change: 2018/09/07 23:11:57]  OK
db4  rep_backlog  [last change: 2018/09/07 23:11:52]  OK: Backlog is nulldb2  ping         [last change: 2018/09/07 22:57:09]  OK
db2  mysql        [last change: 2018/09/07 22:57:09]  OK
db2  rep_threads  [last change: 2018/09/07 22:57:09]  OK
db2  rep_backlog  [last change: 2018/09/07 22:57:09]  OK: Backlog is nulldb3  ping         [last change: 2018/09/07 23:02:09]  OK
db3  mysql        [last change: 2018/09/07 23:02:50]  OK
db3  rep_threads  [last change: 2018/09/07 23:11:42]  OK
db3  rep_backlog  [last change: 2018/09/07 23:11:36]  OK: Backlog is nulldb1  ping         [last change: 2018/09/07 22:57:09]  OK
db1  mysql        [last change: 2018/09/07 22:57:09]  OK
db1  rep_threads  [last change: 2018/09/07 22:57:09]  OK
db1  rep_backlog  [last change: 2018/09/07 22:57:09]  OK: Backlog is null



故障测试!


首先M1,M2授权给测试机器

MariaDB [(none)]> grant all on *.* to 'testdb'@'192.168.137.14' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

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


用监控机当客户机测试,成功登陆

[root@zcent4 mysql-mmm]# mysql -utestdb -p123456 -h 192.168.137.100
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2430Server version: 5.5.60-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 teest;
Query OK, 1 row affected (0.00 sec)

M1:
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mood               |
| mysql              |
| performance_schema |
| teest              |
| test               |
+--------------------+
6 rows in set (0.00 sec)

模拟M2掉线,查看是否自动切换主服务器的虚拟IP

M2:
[root@cent ~]# systemctl stop mariadb.service 

监控机:
[root@zcent4 mysql-mmm]# mmm_control show
  db1(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100)
  db2(192.168.137.11) master/HARD_OFFLINE. Roles: 
  db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.200)
  db4(192.168.137.13) slave/ONLINE. Roles: reader(192.168.137.150)

显然是可以的!读写复制正常

上线M2,但是注意!M2并不会抢占,不带这个功能!

模拟S2掉线,观察情况

S2:[root@cent ~]# systemctl stop mariadb.service

监控机:[root@zcent4 mysql-mmm]# mmm_control show
  db1(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100)
  db2(192.168.137.11) master/ONLINE. Roles: 
  db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.150), reader(192.168.137.200)
  db4(192.168.137.13) slave/HARD_OFFLINE. Roles:
  实行了顶替,期间读写复制正常


总结


1.monitor程序负责监控db服务器的状态,包括Mysql数据库、服务器是否运行、复制线程是否正常、主从延时等;它还用于控制agent程序处理故障。
2.如果master-db1主库宕机,master-db2复制应用又落后于master-db1时就变成了主可写状态,这时的数据主无法保证一致性。
3.monitor会每隔几秒钟监控db服务器的状态,如果db服务器已经从故障变成了正常,那么monitor会自动在60s之后将其设置为online状态
(默认是60s可以设为其它的值),我上面改成了10.
4.实验过程中遇到从服务器无法启动agent,重启解决了,原因不详。



猜你喜欢

转载自blog.51cto.com/13706064/2171908