MySQL中MMM实现高可用群集
介绍
MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,可以说是mysql主主复制管理器。虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的负载均衡。
优点
高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证的数据的一致性。
缺点
Monitor节点是单点,可以结合Keepalived实现高可用。
实验要求
两台主服务器master
master1:192.168.177.128
master2:192.168.177.135
两台从服务器slave
slave1:192.168.177.132
slave2:192.168.177.133
监控服务器monitor
monitor:192.168.177.134
配置ALI云源,然后安装epel-release源(四台主从)
# 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源(四台主从)
# yum -y install mariadb-server mariadb
# systemctl stop firewalld.service
# setenforce 0
# systemctl start mariadb.service
修改m1主配置文件
# vim /etc/my.cnf //
删掉原来的[mysqld]9行,添加如下内容:
[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=1
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
==注意==server_id要不同
# systemctl restart mariadb.service
配置主主复制-两台主服务器相互复制
# mysql>
# show master status; //记录日志文件名称和位置值,在两台主上查看
在m1上为m2授予从的权限,在m2上也要为m1授予从的权限
# grant replication slave on *.* to 'replication'@'192.168.177.%' identified by '123456'; //m1上
# grant replication slave on *.* to 'replication'@'192.168.177.%' identified by '123456';//m2上
#change master to master_host='192.168.177.135',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;//m1上
# change master to master_host='192.168.177.128',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;//m2上
# start slave;
# show slave status\G;
在两台从上做-注意日志文件和位置参数的改变(都指向m1)
# change master to master_host='192.168.177.128',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
# start slave; //开启同步
# show slave status\G;
安装MMM(四台主从)
# yum -y install mysql-mmm*
安装结束后,对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.177.128
mode master
peer db2
</host>
<host db2>
ip 192.168.177.135
mode master
peer db1
</host>
<host db3>
ip 192.168.177.132
mode slave
</host>
<host db4>
ip 192.168.177.133
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.177.200 #主服务器虚拟IP
mode exclusive
</role>
<role reader>
hosts db3, db4
ips 192.168.177.20,192.168.177.30 #从服务器虚拟IP
mode balanced
</role>
在主1上面复制:
# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
在monitor服务器上配置
# systemctl stop firewalld.service
# setenforce 0
# 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*
# cd /etc/mysql-mmm
# vim mmm_mon.conf
<host default>
ping_ips 192.168.177.132,192.168.177.128,192.168.177.133,192.168.177.135 //四台地址
monitor_user mmm_monitor
monitor_password 123456
auto_set_online 10
</host>
在m1上:# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
在所有数据库上为mmm_agent授权-四台主从
# mysql>
# grant super, replication client, process on *.* to 'mmm_agent'@'192.168.177.%' identified by '123456';
在所有数据库上为mmm_moniter授权-四台主从
# grant replication client on *.* to 'mmm_monitor'@'192.168.177.%' identified by '123456';
# flush privileges;
修改所有数据库的mmm_agent.conf-四台主从
# vim /etc/mysql-mmm/mmm_agent.conf
this db1 //根据规划进行逐一调整
this db2
this db3
this db4
在所有数据库服务器上启动mysql-mmm-agent-四台主从
# systemctl start mysql-mmm-agent.service
# systemctl enable mysql-mmm-agent.service #开机自启动
在monitor服务器上配置
# systemctl start mysql-mmm-monitor.service
# mmm_control show
# mmm_control checks all
# mmm_control move_role writer db2 //指定db2绑定虚拟IP
故障测试
停止m1 确认 虚拟地址 200 是否移动到 m2 上。注意:主不会抢占
# systemctl stop mariadb.service //m1上
# mmm_control show
db1(192.168.177.128) master/HARD_OFFLINE. Roles:
db2(192.168.177.135) master/ONLINE. Roles: writer(192.168.177.200)