• mmm高可用


    mmm高可用

    一.介绍

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

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

    MMM是一套灵活的脚本程序,基于perl实现,用来对 mysql replication 进行监控和故障迁移,并能管理 MySQL Master-Master 复制的配置。

    1.关于 MMM 高可用架构的说明如下:

    ●mmm_mon:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监控主机上运行。
    ●mmm_agent:运行在每个MySQL服务器上的代理进程,完成监控的探针工作和执行简单的远端服务设置。此脚本需要在被监管机上运行。
    ●mmm_control:一个简单的脚本,提供管理 mmm_mon 进程的命令。
    ●mysql-mmm 的监管端会提供多个虚拟 IP(VIP),包括一个可写 VIP,多个可读 VIP,通过监管的管理,这些 IP 会绑定在可用 MySQL 之上,当某一台 MySQL 宕机时,监管会将 VIP 迁移至其他 MySQL。

    在整个监管过程中,需要在 MySQL 中添加相关授权用户,以便让 MySQL 可以支持监控主机的维护。 授权的用户包括一个 mmm_monitor 用户和一个 mmm_agent 用户。

    二.配置实现

    master01(db1)			192.168.82.101                mysql5.7、mysql-mmm
    master02(db2)			192.168.82.100                mysql5.7、mysql-mmm
    slave01(db3)			192.168.82.102                mysql5.7、mysql-mmm
    slave02(db4)			192.168.82.103                 mysql5.7、mysql-mmm
    monitor					192.168.80.104                mysql-mmm
    
    
    systemctl stop firewalld 
    setenforce 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1.搭建 MySQL 多主多从模式

    //修改 master01 配置文件
    vim /etc/my.cnf
    ......
    [mysqld]
    basuser = mysql
    edir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    port = 3306
    character_set_server=utf8
    pid-file = /usr/local/mysql/mysqld.pid
    socket = /usr/local/mysql/mysql.sock
    server-id = 1                                               #每台 Mysql 主机的 server-id 不能相同
    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                           #允许slave从master复制数据时可以写入到自己的二进制日志
    sync_binlog=1                            #"双1设置",MySQL 在每写一次二进制日志时都会同步到磁盘中去    
    innodb_flush_log_at_trx_commit=1         #"双1设置",每次事务提交时MySQL都会把缓存的数据写入日志文件,并且刷到磁盘中去
    auto_increment_increment=2               #自增字段一次递增多少
                      #自增字段的起始值
    
    
    //把配置文件复制到其它 3 台数据库服务器上并启动服务器,注意:配置文件中的 server_id 要修改
    scp /etc/my.cnf root@192.168.82.100:/etc/
    scp /etc/my.cnf root@192.168.82.102:/etc/
    scp /etc/my.cnf root@192.168.82.103:/etc/
    
    systemctl restart mysqld
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    //配置主主复制,两台主服务器相互复制
    #在两台主服务器上都执行授予从的权限,从服务器上不需要执行
    grant replication slave on *.* to 'replication'@'192.168.82.%' identified by '123456';
    
    #在两台主服务器上查看,记录日志文件名称和同步点
    show master status;
    +-------------------+----------+--------------+------------------+
    | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +-------------------+----------+--------------+------------------+
    | master-bin.000001 |    460   |              |                  |
    +-------------------+----------+--------------+------------------+
    
    #在 master01 上配置同步
    change master to master_host='192.168.82.100',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=460;
    
    start slave;
    
    show slave status\G
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
    
    #在 master02 上配置同步
    change master to master_host='192.168.82.101',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=460;
    
    start slave;
    
    show slave status\G
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
    
    
    //配置主从复制,在两台从服务器上做
    change master to master_host='192.168.82.101',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=460;
    
    start slave;
    
    show slave status\G
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
    
    
    //测试主主、主从 同步情况
    create database db_test;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    2.安装配置 MySQL-MMM

    //在所有服务器上安装 MySQL-MMM
    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*
    
    //在 master01 上对 MySQL-MMM 进行配置
    cd /etc/mysql-mmm/
    vim 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.82.101
        mode    master
        peer    db2
    </host>
    
    <host db2>
        ip      192.168.82.100
        mode    master
        peer    db1
    </host>
    
    <host db3>
        ip      192.168.80.102
        mode    slave
    </host>
    
    <host db4>
        ip      192.168.82.103
        mode    slave
    </host>
    
    <role writer>
        hosts   db1, db2
        ips     192.168.82.188
        mode    exclusive           #只有一个 host 可以进行写操作模式
    </role>
    
    <role reader>
        hosts   db3, db4
        ips     192.168.82.198, 192.168.82.199
        mode    balanced            #多个 slave 主机可以进行读操作模式
    </role>
    
    //把配置文件复制到其它 4 台主机,所有主机该配置文件内容都是一样的
    scp mmm_common.conf root@192.168.82.100:/etc/mysql-mmm/
    scp mmm_common.conf root@192.168.82.102:/etc/mysql-mmm/
    scp mmm_common.conf root@192.168.82.103:/etc/mysql-mmm/
    scp mmm_common.conf root@192.168.82.104:/etc/mysql-mmm/
    
    
    //修改所有数据库服务器的代理配置文件 mmm_agent.conf
    vim /etc/mysql-mmm/mmm_agent.conf
    include mmm_common.conf
    this db1				#根据不同的主机分别修改为 db1,db2,db3,db4
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    //在 monitor 监控服务器上修改监控配置文件 mmm_mon.conf
    vim /etc/mysql-mmm/mmm_mon.conf
    include mmm_common.conf
    <monitor>
    .....
        ping_ips        	192.168.82.100,192.168.82.101,192.168.82.103,192.168.82.102
       #指定所有数据库服务器的 IP
    	auto_set_online		10				#指定自动上线时间
    </monitor>
    
    <host default>
        monitor_user        mmm_monitor		#指定 mmm_monitor 的用户名
        monitor_password    123456          #指定 mmm_monitor 的密码
    </host>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    //在所有数据库上为 mmm_agent(代理进程)授权
    grant super, replication client, process on *.* to 'mmm_agent'@'192.168.82.%' identified by '123456';
    
    
    //在所有数据库上为 mmm_moniter(监控进程)授权
    grant replication client on *.* to 'mmm_monitor'@'192.168.82.%' identified by '123456';
    
    flush privileges;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    //在所有数据库服务器上启动 mysql-mmm-agent
    systemctl start mysql-mmm-agent.service
    systemctl enable mysql-mmm-agent.service
    
    
    //在 monitor 服务器上启动 mysql-mmm-monitor
    systemctl start mysql-mmm-monitor.service 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    //在 monitor 服务器上测试群集
    #查看各节点的情况
    mmm_control show
       db1(192.168.82.101) master/ONLINE. Roles: writer(192.168.82.188)
      db2(192.168.82,100) master/ONLINE. Roles: 
      db3(192.168.82.102) slave/ONLINE. Roles: reader(192.168.82.198)
      db4(192.168.82.103) slave/ONLINE. Roles: reader(192.168.82.199)
    
    #检测监控功能是否都完善,需要各种OK
    mmm_control checks all
    
    #指定绑定 VIP 的主机
    mmm_control move_role writer db2
    
    
    //故障测试
    mmm_control move_role writer db1
    
    #停止 master01 确认 VIP 是否移动到 master02 上。注意:master01 主服务器恢复服务后,不会抢占
    
    mmm_control show
      db1(192.168.82.101) master/HARD_OFFLINE. Roles:
      db2(192.168.82.100) master/ONLINE. Roles: writer(192.168.80.188)
    
    #停止一台从服务器,另一台将接管两个虚拟IP,以保证业务不停止
    mmm_control show
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    //客户端测试
    #在 master01 服务器上为 monitor 服务器地址授权登录
    grant all on *.* to 'testdba'@'192.168.82.104' identified by '123456';
    flush privileges;
    
    #在 monitor 服务器上使用 VIP 登录
    yum install -y mariadb-server mariadb
    systemctl start mariadb.service
    
    mysql -utestdba -p -h 192.168.82.188
    
    #创建数据,测试同步情况
    create database testdba;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    持续集成部署-k8s-配置与存储-配置管理:SubPath
    ServletContext对象
    Gradient Harmonized Single-stage Detector
    v 3 + vite + ts 自适应布局(postcss-pxtorem)
    【计算机网络】第四章.网络层 网络层超硬核复习好物(1),考前必看!!
    【CSS】伪元素和伪类选择器区别
    depot_tools原理和实现
    操作系统小练习 任务管理器
    MIPI CSI-2笔记(7) -- Low Level Protocol(C-PHY物理层校验和生成,包间隔)
    C语言和Java中RSA算法一些注意事项
  • 原文地址:https://blog.csdn.net/q1231vev/article/details/133273390