您的位置:首页 > 数据库 > MariaDB

MySQL/MariaDB基于MMM实现读写分离及高可用

2015-06-23 15:15 746 查看
前言
MMM(Master-Master replication managerfor Mysql,Mysql主主复制管理器)是一套灵活的脚本程序,基于perl实现,用来对mysql replication进行监控和故障迁移,并能管理mysql Master-Master复制的配置(同一时间只有一个节点是可写的)。
MMM优缺点优点:高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证的数据的一致性。缺点:Monitor节点是单点,可以结合Keepalived实现高可用。工作原理mysql-mmm的监管端会提供多个虚拟IP(VIP),包括一个可写VIP,多个可读VIP,通过监管的管理,这些IP会绑定在可用服务器之上,当某一台服务器宕机时,监管会将VIP迁移至其他服务器。实现过程实验拓扑


#注:系统环境CentOS6.6
#VIP172.16.10.30为可写VIP,其他三组为可读VIP
#可写VIP只能在Master之间切换,可读VIP可在Master和Slave之间切换
#前端应用可连接任意可读VIP进行数据读取,连接可写VIP进行数据写入
双主一从配置
DB1配置
[root@node1 ~]# vim /etc/mysql/my.cnf

datadir = /mydata/data
log-bin=/mydata/binlogs/master-bin       #二进制文件位置
relay-log=/mydata/relaylogs/relay-bin    #中继日志位置
binlog_format=mixed
server-id       = 1
auto_increment_offset=1        #双主复制中自增长字段的起始值
auto_increment_increment=2     #双主复制中自增长字段的步长
log_slave_updates = 1          #开启从库更新操作写入二进制日志功能
sync_binlog = 1                #可保证事务日志及时写入磁盘文件
skip-name-resolve              #禁用DNS反向解析,如不写此项,则需要在各节点基于主机名通信
read_only       = 1

[root@node1 ~]# service mysqld start
授权可用复制用户记录二进制日志位置


DB2配置
[root@node2 ~]# vim /etc/mysql/my.cnf

datadir = /mydata/data
log-bin=/mydata/binlogs/master-bin
relay-log=/mydata/relaylogs/relay-bin
binlog_format=mixed
server-id       = 11
auto_increment_offset=2
auto_increment_increment=2
log_slave_updates = 1
sync_binlog = 1
skip-name-resolve
read_only       = 1

[root@node2 ~]# service mysqld start
授权可用复制用户记录二进制日志位置


#因为实验之前都做了完整初始化,这里二进制文件位置一致
DB3配置
[root@scholar ~]# vim /etc/mysql/my.cnf

datadir = /mydata/data
relay-log=/mydata/relaylogs/relay-bin
server-id       = 111
read_only       = 1
skip-name-resolve

[root@scholar ~]# service mysqld start
连接各服务器
DB1连接DB2

DB2连接DB1

DB3连接DB1

主从测试
#DB1创建数据库
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.14 sec)

#DB2创建表
MariaDB [(none)]> create table testdb.tb1(name char(20) not null);
Query OK, 0 rows affected (0.19 sec)

#DB3查看数据
MariaDB [(none)]> show tables in testdb;
+------------------+
| Tables_in_testdb |
+------------------+
| tb1              |
+------------------+
1 row in set (0.13 sec)
数据一致,主从复制部分完成MMM配置
安装所需程序包
[root@node1 ~]# yum install mysql-mmm* -y

#在所有节点安装mysql-mmm包组
授权监控及代理用户

#DB节点全部需要授权,在一个节点授权即可
Monitor配置
[root@scholar ~]# 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    replpass        #复制用户密码
agent_user              mmm_agent       #代理用户
agent_password          agent_passw     #代理用户密码
</host>

<host db1>
ip      172.16.10.123
mode    master
peer    db2
</host>

<host db2>
ip      172.16.10.124
mode    master
peer    db1
</host>

<host db3>
ip      172.16.10.125
mode    slave
</host>

<role writer>           #可写节点
hosts   db1, db2
ips     172.16.10.30
mode    exclusive   #互斥角色只有一个ip,同一时间只能分配给一个主机
</role>

<role reader>           #可读节点
hosts   db1, db2, db3
ips     172.16.10.31,172.16.10.32,172.16.10.33
mode    balanced    #负载均衡角色可以有多个IP,这些IP被均衡的分配给多个主机
</role>
#将以上文件同步到DB1-DB3节点
[root@scholar ~]# scp /etc/mysql-mmm/mmm_common.conf 172.16.10.123:/etc/mysql-mmm/
mmm_common.conf                                          100%  776     0.8KB/s   00:00
[root@scholar ~]# scp /etc/mysql-mmm/mmm_common.conf 172.16.10.124:/etc/mysql-mmm/
mmm_common.conf                                          100%  776     0.8KB/s   00:00
[root@scholar ~]# scp /etc/mysql-mmm/mmm_common.conf 172.16.10.125:/etc/mysql-mmm/
mmm_common.conf                                          100%  776     0.8KB/s   00:00
[root@scholar ~]# vim /etc/mysql-mmm/mmm_mon.conf

include mmm_common.conf

<monitor>
ip                  127.0.0.1
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.10.123,172.16.10.124,172.16.10.125 #监控主机
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        mmm_monitor    #监控用户
monitor_password    monitor_pass   #监控密码
</host>

debug 0
DB1-DB3配置
[root@node1 ~]# 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

#DB1-DB3根据自身实际角色修改此配置文件(db1,db2,db3)
启动服务
#在各监控节点启动mmm-agent服务
[root@node1 ~]# service mysql-mmm-agent start
Starting MMM Agent Daemon:                                 [  OK  ]
#在Monitor上启动mmm-monitor服务
[root@scholar ~]# service mysql-mmm-monitor start
Starting MMM Monitor Daemon:                               [  OK  ]
查看当前状态


查看各节点VIP状态
#以db1为例


高可用测试模拟db1故障,查看节点状态


再看db1的VIP状态


VIP已被转移到其他节点,其他方面有兴趣请自行测试,这里就不一一展示了The endMySQL/MariaDB基于MMM实现读写分离及高可用实验就先说到这里了,有兴趣的朋友可以继续探究基于Keepalived实现Monitor的高可用,这里就不做深究了,实验过程中遇到问题可留言交流。以上仅为个人学习整理,如有错漏,大神勿喷~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息