MMM
MMM简介
针对企业的现况,纵然有着mysql主从复制和读写分离的技术,倘如主mysql损坏,那从mysql并没有复制功能,无法进行正常的工作。因此MMM的原理就是拥有两台主服务器,一旦主1服务器down掉后,主2服务器可以立马衔接上。
MMM搭建材料
准备材料:
2主MySQL,2从mysql,1代理mysql服务
配备好网址后,以下是服务器IP地址:
主1服务器:192.168.53.123
主2服务器:192.168.53.77
从1服务器:192.168.53.170
从2服务器:192.168.53.44
mysql代理服务器(monitor+atlas):192.168.53.66
MMM步骤
1)拥有5台服务器后,配备确定他们各自的网址,接着进入各自的my.cnf文件中进行配置
主服务器1:
主服务器2:
从服务器:
从服务器命令:
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
log-slave-updates = true
主1服务器命令:
log_bin=master_bin
log-slave-updates=true
auto-increment-increment = 2
auto-increment-offset = 1
主2服务器命令:
log_bin=master_bin
log-slave-updates=true
auto-increment-increment = 2
auto-increment-offset = 2
2)修改完成后,四台服务器均重启
3)进入主1,主2服务器中,进行放权,关联对方的主ip:
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.53.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
change master to master_host='192.168.53.77',master_user='myslave',master_password='123456',master_log_file='master_bin.000007',master_log_pos=120;
mysql> start slave;
change master to master_host='192.168.53.123',master_user='myslave',master_password='123456',master_log_file='master_bin.000005',master_log_pos=284;
mysql> start slave;
4)主开启ntpd,让从服务器下载ntpdate使得使用ntpdate +主服务器ip地址的延迟控制在0.01以下,之后设置成永久开启,而后查看两台主服务器输入show slave hosts;命令查看是否有关联成功:
主1:
主2:
再确认服务器show slave status\G;命令是否为两个yes
备注:从服务器也要进行关联,但是只需要关联主1服务器即可:
change master to master_host='192.168.53.123',master_user='myslave',master_password='123456',master_log_file='master_bin.000005',master_log_pos=284;
mysql> start slave;
此时,已经可以进行在主数据库创库删库的验证
5)5台机器均下载mmm
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install mmm*
6)主从服务器进入数据库进行mmm的放权
grant replication client on *.* to 'mmm_monitor'@'192.168.53.%' identified by 'monitor';
grant super,replication client,process on *.* to 'mmm_agent'@'192.168.53.%' identified by 'agent';
FLUSH PRIVILEGES;
7)vim /etc/mysql-mmm/mmm_common.conf (五台都需要)
active_master_role writer
<host default>
cluster_interface ens33
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replicant
replication_password slave
agent_user mmm_agent
agent_password RepAgent
</host>
<host db1>
ip 192.168.53.123
mode master
peer db2
</host>
<host db2>
ip 192.168.53.77
mode master
peer db1
</host>
<host db3>
ip 192.168.53.170
mode slave
</host>
<host db4>
ip 192.168.53.44
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.53.250
mode exclusive
</role>
<role reader>
hosts db3, db4
ips 192.168.53.251, 192.168.53.252
mode balanced
</role>
8)在四台主从mysql服务器上修改
/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,db2,db3,db4
哪台是之前在配置文件中设定ip地址的主从那就哪台是db1,依次命名
9)代理服务器修改
vim /etc/mysql-mmm/mmm_mon.conf
10)启动代理
主1:
systemctl start mysql-mmm-agent
systemctl enable mysql-mmm-agent
主2:
systemctl start mysql-mmm-agent
systemctl enable mysql-mmm-agent
从1:
systemctl start mysql-mmm-agent
systemctl enable mysql-mmm-agent
从2:
systemctl start mysql-mmm-agent
systemctl enable mysql-mmm-agent
代理服务器:
systemctl start mysql-mmm-monitor
systemctl enable mysql-mmm-monitor
11)下载atlas(主从god主从动静分离有),主从服务器上进行放权:
grant all on . to test@‘192.168.53.%’ identified by ‘123’;
flush privileges;
12)然后修改里面配置文件
13)重启读写分离
/usr/local/mysql-proxy/bin/mysql-proxyd test restart
14)重启过后,可以输入mmm_control show进行测试
15)验证
用虚拟IP连接数据库
mysql -h192.168.53.250 -utest -p123尝试是否可以用虚拟ip地址登录数据库
使用select @@server_id;命令来查看当前正在运行的主服务器:
当停掉主1 mysql时在输入 select @@server_id; 命令