基于MMM实现MariaDB的高可用

1、MMM
mysql

一、简介sql

MMM即Master-Master Replication Manager for MySQL(mysql主主复制管理器),是关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任什么时候候只有一个节点能够被写入),这个套件也能基于标准的主从配置的任意数量的从服务器进行读负载均衡,因此你能够用它来在一组居于复制的服务器启动虚拟ip,除此以外,它还有实现数据备份、节点之间从新同步功能的脚本。
MySQL自己没有提供replication failover的解决方案,经过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。
数据库

二、MMM的功能vim

MMM主要功能由下面三个脚本提供
mmm_mond    :负责全部的监控工做的监控守护进程,决定节点的移除等等
mmm_agentd  :运行在mysql服务器上的代理守护进程,经过简单远程服务集提供给监控节点安全

mmm_control :经过命令行管理mmm_mond进程bash

三、MMM的优缺点及应用场景服务器

优势:安全性、稳定性高,可扩展性好,当主服务器挂掉之后,另外一个主当即接管,其余的从服务器能自动切换,不用人工干预。架构

缺点:至少三个节点,对主机的数量有要求,须要实现读写分离,能够在程序扩展上比较难实现。同时对主从(双主)同步延迟要求比较高!所以不适合数据安全很是严格的场合。负载均衡

应用场所:高访问量,业务增加快,而且要求实现读写分离的场景。ide

2、MMM架构原理图

wKioL1Ngw7KzkqVEAAA4RTFe4wQ449.png

3、资源配置

一、服务器列表

服务器 IP 主机名 server id
monitoring host 172.16.7.100 monitor -
master 1 172.16.7.200 db1 1
master 2 172.16.7.201 db2 2
master 3 172.16.7.202 db3 3

二、虚拟IP列表

IP role description
172.16.7.1 write 对就用程序链接的VIP进行写操做
172.16.7.2 read


对就用程序链接的VIP进行读操做

172.16.7.3 read

4、MMM的实现

一、配置master 1

(1)修改/etc/my.cnf配置文件

server-id       = 1
datadir = /mydata/data
log-bin = /mydata/binglogs/master-bin
relay_log = /mydata/relaylogs/relay
binlog_format=mixed
thread_concurrency = 4
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment-offset=1

(2)为master2 和 slave 受权复制用户

MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.201' identified by 'repluser';
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.202' identified by 'repluser';

(3)查看状态信息,从服务器链接主服务器时使用

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      755 |              |                  |
+-------------------+----------+--------------+------------------+

二、配置master 2

(1)修改/etc/my.cnf配置文件

server-id       = 2
datadir = /mydata/data
log-bin = /mydata/binglogs/master-bin
relay_log = /mydata/relaylogs/relay
binlog_format=mixed
thread_concurrency = 4
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment-offset=2

(2)为master1 和 slave 受权复制用户

MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.200' identified by 'repluser';
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.202' identified by 'repluser';

(3)查看状态信息,从服务器链接主服务器时使用

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      755 |              |                  |
+-------------------+----------+--------------+------------------+

(4)master 2 链接 master 1

MariaDB [(none)]> change master to master_host='172.16.7.200',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755;
MariaDB [(none)]>
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.7.200
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 755
               Relay_Log_File: relay.000003
                Relay_Log_Pos: 536
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 755
              Relay_Log_Space: 823
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:

(5)master 1 链接 master 2

MariaDB [(none)]> change master to master_host='172.16.7.201',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.7.201
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 327
               Relay_Log_File: db1-relay-bin.000003
                Relay_Log_Pos: 615
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 327
              Relay_Log_Space: 954
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 2
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:

(6)测试master 1 与master 2 是否能够正常主从同步

=========== master 1 ================
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> create database hlbr;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| hlbr               |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
=========== master 2 ================
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| hlbr               |    #同步成功
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.02 sec)

(7)测试master 2 与master 1 是否能够正常主从同步

   我在master 2 上建立了一个bynr库,能够同步到master 1 上,为了减少篇幅,这里就不贴图了。

   通过(6),(7)的测试,说明双主已然成功

三、配置slave

(1)修改/etc/my.cnf配置文件

server-id       = 3
datadir = /mydata/data
log-bin = /mydata/binglogs/master-bin
relay_log = /mydata/relaylogs/relay
binlog_format=mixed
thread_concurrency = 4
log-slave-updates
sync_binlog=1

(2)slave链接master 1

MariaDB [(none)]> change master to master_host='172.16.7.200',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755;
Query OK, 0 rows affected (0.05 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.7.200
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1261
               Relay_Log_File: relay.000002
                Relay_Log_Pos: 1042
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1261
              Relay_Log_Space: 1329
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:

(3)再次测试,在master 1 上建立数据库,去slave上查看

====================== master 1 =====================
MariaDB [(none)]> create database hhht;
Query OK, 1 row affected (0.02 sec)
======================== slave ======================
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| bynr               |
| hhht               |
| hlbr               |    #全部的所有同步成功
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

四、安装配置mysql-mmm

   由于monitor主机负责全部的监控工做,决定节点的移除等等,因此它必需要获得全部master和slave的受权;且每台数据库服务器上都须要安装mysql-mmm-agent,经过简单远程服务集提供给监控节点

(1)在3台mysql服务器上安装mysql-mmm-agent

由于有好多依赖关系,因此选择yum安装

[root@db1 ~]# yum -y install mysql-mmm-agent

(2)在3台mysql服务器上为monitor受权

用户 description privileges
monitor user 用于mmm检查mysql服务器健康情况的用户 replication client
agent user 用于mmm代理为只读模式和复制等的用户 super,replication client,process
replication user 复制的用户 replication slave

接下来要作的就是为上表中的用户受权给监控主机,因为咱们的主主、主从已经作好了,权限也已经授过了,因此上表中的第三个就不需重复操做了,并且咱们只需在主服务器上受权剩下的两个就行了,它会自动同步到另外两台mysql服务器上的

MariaDB [(none)]> grant replication client on *.* to 'monitor'@'172.16.7.100' identified by 'monitor';
MariaDB [(none)]> grant super,replication client,process on *.* to 'agent'@'172.16.7.100' identified by 'agent';
MariaDB [(none)]> flush privileges;

(3)在monitor节点上安装mmm

[root@monitor ~]# yum -y install mysql-mmm*

 ①、配置/etc/mysql-mmm/mmm_common.conf

[root@monitor ~]# vim /etc/mysql-mmm/mmm_common.conf
active_master_role      writer
<host default>
    cluster_interface       eth0
    pid_path                /var/run/mysql-mmm/mmm_agentd.pid
    bin_path                /usr/libexec/mysql-mmm/
    replication_user        repluser    #受权复制用户
    replication_password    repluser    #密码
    agent_user              agent       #代理用户
    agent_password          agent       #代理用户密码
</host>
<host db1>
    ip      172.16.7.200
    mode    master            #主的
    peer    db1
</host>
<host db2>
    ip      172.16.7.201
    mode    master
    peer    db2
</host>
<host db3>
    ip      172.16.7.202
    mode    slave              #从的
</host>
<role writer>
    hosts   db1, db2
    ips     172.16.7.1
    mode    exclusive           #exclusive表示排它
</role>
<role reader>
    hosts   db2, db3
    ips     172.16.7.2, 172.16.7.3
    mode    balanced            #balanced表示均衡
</role>

  ②、拷贝此文件到3台mysql服务器

   这个不是数据库中的数据,不能同步了,只能手动来了

[root@monitor ~]# scp /etc/mysql-mmm/mmm_common.conf root@172.16.7.202:/etc/mysql-mmm/   #3个节点都需这么作

  ③、在monitor上修改mmm_mon.conf文件

[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
    ip                  172.16.7.100    #监控主机的ip
    pid_path            /var/run/mysql-mmm/mmm_mond.pid
    bin_path            /usr/libexec/mysql-mmm
    status_path         /var/lib/mysql-mmm/mmm_mond.status
    ping_ips            172.16.7.200, 172.16.7.201, 172.16.7.202 #各数据库服务器的ip
    auto_set_online     60
    # 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        monitor    #监控用户的用户名
    monitor_password    monitor    #密码
</host>
debug 0     #若是程序无序正常监控,可以使用debug 1进行排查

(4)在每一个DB上修改mmm_agent.conf

[root@db1 ~]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf    #调用此文件
# The 'this' variable refers to this server.  Proper operation requires
# that 'this' server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this db1    #这一行是标记此主机的角色(引用mmm_common.conf中的host段),当前主机是db几,这里就要改成this db几

五、启动MMM并测试

(1)启动mysql-mmm-agent

[root@db1 ~]# service mysql-mmm-agent start
Starting MMM Agent Daemon:                                 [  OK  ]
#每一个DB上的mysql-mmm-agent都要启动

(2)启动monitor监控程序

[root@monitor ~]# chkconfig mysql-mmm-monitor on
[root@monitor ~]# service mysql-mmm-monitor start
Starting MMM Monitor Daemon:                               [  OK  ]

(3)在监控主机查看监控状态

[root@monitor ~]# service mysql-mmm-monitor status
mmm_mond (pid  1586) is running...
[root@monitor ~]# mmm_control show
  db1(172.16.7.200) master/ONLINE. Roles: writer(172.16.7.1)
  db2(172.16.7.201) master/ONLINE. Roles: reader(172.16.7.3)
  db3(172.16.7.202) slave/ONLINE. Roles: reader(172.16.7.2)

(4)设置db1离线,再查看状态

[root@monitor ~]# mmm_control set_offline db1    #设置db1离线
OK: State of 'db1' changed to ADMIN_OFFLINE. Now you can wait some time and check all roles!
[root@monitor ~]# mmm_control show
  db1(172.16.7.200) master/ADMIN_OFFLINE. Roles:     #db1已经离线,VIP转移到了db2上
  db2(172.16.7.201) master/ONLINE. Roles: reader(172.16.7.3), writer(172.16.7.1)
  db3(172.16.7.202) slave/ONLINE. Roles: reader(172.16.7.2)

在开始的时候,咱们设置了一个写IP:172.16.7.1和两个读IP:172.16.7.二、172.16.7.3,由(3)和(4)能够看出,在三个节点都正常的状况下,写IP是在master 1上,当master 1挂了的话,写IP就飘到master 2上了,这就解决了mysql数据库的单点故障,经过MMM实现了mysql数据库的高可用

相关文章
相关标签/搜索