前言
工作原理:
master1和master2之间双向复制,同时Master1和Slave1与slave2之间是主从复制。这样整个体系中存在两个Master,正常情况下只有一个master对外提供写服务。如果对外提供服务的master意外宕机了,这是MySQL本身并不具备failover切换的能力,尽管集群中仍然有一个正常的master节点,但应用仍不可用。mysql-mmm就是为了解决这个问题诞生的。
MySQL-MMM是Master-Master Replication Manager for MySQL(mysql主主复制管理器)的简称,是Google的开源项目(Perl脚本),主要用来监控mysql主主复制并做失败转移。
其原理是将真实数据库节点的IP(RIP)映射为虚拟IP(VIP)集,在这个虚拟的IP集中,有一个专用于write的IP,多个用于read的IP,这个用于Write的VIP映射着数据库集群中的两台master的真实IP(RIP),以此来实现Failover的切换,其他read的VIP可以用来均衡读(balance)。
一、实验环境
二、实验步骤
1.搭建主主同步
2.搭建主从同步
3.安装配置mysql-mmm
4.配置monitor监控
5.测试
三、实验内容
-------------------------------搭建主主同步-------------------
配置db1/db2/db3/db4服务器的配置文件
vim /etc/my.cnf [mysqld] log-bin=mysql_bin //开启log-bin server-id = 11 //每台的server-id不可相同 binlog-ignore-db=mysql,infomation_schema //不进行同步的库 log-slave-updates=true //允许slave进行同步 sync_binlog=1 auto_increment_increment=2 auto_increment_offset=1 systemctl restart mysqld.service systemctl stop firewalld.service setenforce 0
注:配置四台服务器时server-id必须互不相同
配置主主模式:db1/db2
db1:192.168.218.131
mysql -uroot -pgrant replication slave on *.* to 'replication'@'192.168.218.%' identified by 'abc123'; flush privileges; show master status; change master to master_host='192.168.218.137',master_user='replication',master_password='abc123',master_log_file='mysql_bin.000001',master_log_pos=426;
db2:192.168.218.137
mysql -uroot -pgrant replication slave on *.* to 'replication'@'192.168.218.%' identified by 'abc123'; flush privileges; show master status; change master to master_host='192.168.218.131',master_user='replication',master_password='abc123',master_log_file='mysql_bin.000002',master_log_pos=426;
配置完成后db1与db2均开启slave:
start slave; show slave status \G; Slave_IO_Running: Yes Slave_SQL_Running: Yes //查看二者需均为yes Last_IO_Errno: 0 //若不为yes可查看error信息 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error:
-------------------------------搭建主从同步-------------------
db3:192.168.218.145、db4:192.168.218.139
mysql -uroot -p change master to master_host='192.168.218.131',master_user='replication',master_password='abc123',master_log_file='mysql_bin.000002',master_log_pos=426; start slave; show slave status \G; Slave_IO_Running: Yes //二者均为yes Slave_SQL_Running: Yes
注:每次change master时均需要去db1 master服务器show master status
----------------------------安装配置mysql-mmm-------------------------
安装在五台服务器
wget -O /etc/yum.repos.d/CenOS-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*
db1:192.168.218.131
vim /etc/mysql-mmm/mmm_common.confcluster_interface pid_path /run/mysql-mmm-agent.pid bin_path /usr/libexec/mysql-mmm/ replication_user replicant replication_password abc123 agent_user mmm_agent agent_password abc123 ip 192.168.218.131 mode master peer db2 ip 192.168.218.137 mode master peer db1 ip 192.168.218.145 mode slave ip 192.168.218.139 mode slave hosts db1, db2 ips 192.168.218.250 mode exclusive hosts db3, db4 ips 192.168.218.251, 192.168.218.252 mode balanced
cd /etc/mysql-mmm/
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/ scp mmm_common.conf [email protected]:/etc/mysql-mmm/
在所有数据库上为mmm_monitor、mmm-agant授权
grant super, replication client, process on *.* to 'mmm_agent'@'192.168.218.%' identified by 'abc123'; grant replication client on *.* to 'mmm_monitor'@'192.168.218.%' identified by 'abc123'; flush privileges;
修改所有数据库的mmm_agent.conf
vim /etc/mysql-mmm/mmm_agent.conf this db1 //根据实验环境修改db后的数值
在所有数据库上启动mysql-mmm-agent
systemctl start mysql-mmm-agent.service systemctl enable mysql-mmm-agent.service
---------------------------------配置monitor监控-------------------------
monitor:192.168.218.130
vim /etc/mysql-mmm/mmm_mon.conf systemctl start mysql-mmm-monitor.service
监控显示结果OK
-------------------------------测试-----------------------------------
将db3 mysql宕掉,使reader vip均移至db4
db3:systemctl stop mysqld
测试无异常,实验搭建成功。