一、Mysql-mmm集群技术概述;
概述:MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,可以说是mysql主主复制管理器。
虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡。
MMM提供了自动和手动两种方式移除一组服务器中复制延迟较高的服务器的虚拟ip,同时它还可以备份数据,实现两节点之间的数据同步等。由于MMM无法完全的保证数据一致性,所以MMM适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景。
二、Mysql-mmm优缺点;
三、Mysql-mmm内部工作架构;
进程类型:
mmm_mond:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监管机上运行;
mmm_agentd:运行在每个mysql服务器上(Master和Slave)的代理进程,完成监控的探针工作和执行简单的远端服务设置。此脚本需要在被监管机上运行;
mmm_control:一个简单的脚本,提供管理mmm_mond进程的命令;
[root@master1 ~]# cat <>/etc/hosts
192.168.100.208 master1
192.168.100.209 master2
192.168.100.210 slave1
192.168.100.211 slave2
192.168.100.212 monitor
END
####一共要配置四个服务器,剩余一个用来安装mmm
在每个/etc/hostsname文件中写入:master1 ,master2 ,slave1 , slave2 ,monitor
[root@master2 ~]# yum -y install ntpdate
[root@master2 ~]# /usr/sbin/ntpdate 192.168.100.101
ech 9 Aug 18:04:38 ntpdate[1106]: adjust time server 192.168.100.101 offset 0.299673 sec
[root@master2 ~]# echo "/usr/sbin/ntpdate 192.168.100.101">>/etc/rc.local
[root@master2 ~]# chmod +x /etc/rc.local
[root@master1 ~]# cat <>/etc/my.cnf
server-id=1
log-bin=mysql-bin
log-slave-updates ##开启二进制日志配置
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
relay-log=relay1-log-bin
relay-log-index=slave-relay1-bin.index ##开启二进制中继日志
END
注解:
sync_binlog=1 ##主机每次提交事务的时候把二进制日志的内容同步到磁盘上,所以即使服务器崩溃,也会把时间写入到日志中;
auto_increment_increment=2 ##以下两参数用于主主复制中,用于错开增值,防止键值冲突
[root@master1 ~]# systemctl restart mysqld
[root@master1 ~]# mysql -uroot -p123123
mysql> show master status;
+------------------+----------+--------------+------------------+---------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+---------------------------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+---------------------------------------+
mysql> grant replication slave on *.* to 'master'@'192.168.100.%' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='对方ip',master_user='授权的用户(master)',master_password='123123',master_log_file='mysql-bin.000001',master_log_pos=查看日志的状态码;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
在master2主机中做同样的配置(开启二进制文件和中继日志,查看日志编码,授权,change链接)
1.两个从服务器开启中继日志
[root@slave1 ~]# cat <>/etc/my.cnf
server-id=4
relay-log=relay4-log-bin
relay-log-index=slave-relay4-bin.index
END
[root@slave1 ~]# systemctl restart mysqld
[root@slave1 ~]# mysql -uroot -p123123
mysql> change master to master_host='192.168.100.101',master_user='master',master_password='123123',master_log_file='mysql-bin.000001',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.101
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 421
Relay_Log_File: relay3-log-bin.000003
Relay_Log_Pos: 584
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@master1 ~]# mysql -uroot -p123123
##############创建一个表格查看从服务器是否同步
mysql> create database linuxfan;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> exit
[root@master2 ~]# mysql -uroot -p123123
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> exit
[root@slave1 ~]# mysql -uroot -p123123
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> exit
配置本地yum源,上传Mysql_mmm_rpm文件夹到master1的/root下
scp -r Mysql_mmm_rpm [email protected]:/root/
scp -r Mysql_mmm_rpm [email protected]:/root/
scp -r Mysql_mmm_rpm [email protected]:/root/
scp -r Mysql_mmm_rpm [email protected]:/root/
配置本地yum源
cat /etc/yum.repos.d/dgf.repo
[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0
[Mysql_mmm_rpm]
name=Mysql_mmm_rpm
baseurl=file:///root/Mysql_mmm_rpm
enabled=1
gpgcheck=0
[root@master1 ~]# cd /etc/yum.repos.d/
scp dgf.repo [email protected]:/etc/yum.repos.d/
scp dgf.repo [email protected]:/etc/yum.repos.d/
scp dgf.repo [email protected]:/etc/yum.repos.d/
scp dgf.repo [email protected]:/etc/yum.repos.d/
[root@master1 ~]# yum -y install mysql-mmm mysql-mmm-agent mysql-mmm-tools
[root@master1 ~]# vi /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
:wq
[root@master2 ~]# egrep -v "^#|^$" /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db2
[root@slave1 ~]# egrep -v "^#|^$" /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db3
[root@slave2 ~]# egrep -v "^#|^$" /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db4
[root@master1 ~]# mysql -uroot -p123123
mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.100.%' identified by 'monitor';
Query OK, 0 rows affected (0.00 sec)
mysql> grant super,replication client,process on *.* to 'mmm_agent'@'192.168.100.%' identified by 'agent';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
权限注解:
replication client:权限用于执行show master status等命令。这些命令是用来查看复制状态的;
replication slave:是用于连接主库从库进行读取二进制文件进而实现复制的;
super:杀死mysql中连接的进程,设置全局变量,重置主从配置的权限;
process:具有查看当前运行的sql的权限 ,以及explain执行计划;
[root@monitor ~]# yum -y install mysql-mmm mysql-mmm-tools mysql-mmm-monitor
配置 monitor节点上的mysql-mmm的配置文件并且复制到各个mysql节点;
[root@monitor ~]# vi /etc/mysql-mmm/mmm_common.conf
active_master_role writer ##指定活跃角色为写角色
cluster_interface eth0 ##承载的网卡$$$$修改为自己的网卡
pid_path /var/run/mysql-mmm/mmm_agentd.pid
bin_path /usr/libexec/mysql-mmm/ ##可执行文件路径
replication_user master ##mysql集群主从复制账户
replication_password 123123 $$$自己的授权时的密码
agent_user mmm_agent ##monitor连接集群的账户
agent_password agent
$$$$$修改为自己的IP
ip 192.168.100.101
mode master
peer db2 $$$$$主服务器的从服务器 ##指定对立的节点名
ip 192.168.100.102 $$$$$修改为自己的IP
mode master
peer db1 $$$$$主服务器的从服务器
ip 192.168.100.103 $$$$$修改为自己的IP
mode slave
ip 192.168.100.104 $$$$$修改为自己的IP
mode slave
hosts db1, db2
ips 192.168.100.250
mode exclusive ##同一时间存在单个主
hosts db3, db4
ips 192.168.100.251, 192.168.100.252
mode balanced ##轮询
for i in 101 102 103 104;do scp /etc/mysql-mmm/mmm_common.conf [email protected].$i:/etc/mysql-mmm/; done ##将配置文件复制到mysql节点
[root@monitor ~]# vi /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
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 192.168.100.101, 192.168.100.102, 192.168.100.103, 192.168.100.104 ##指定监听的所有节点ip(添加时记得都好分隔)
auto_set_online 60 ##判定其online的时间,超过60s认为其down
monitor_user mmm_monitor ##monitor的工作用户
monitor_password monitor
debug 0
重载磁盘,开启(所有服务器)服务,查看端口号是否开启
[root@master1 ~]# systemctl daemon-reload
[root@master1 ~]# systemctl start mysql-mmm-agent
[root@master1 ~]# netstat -utpln |grep mmm
tcp 0 0 192.168.100.101:9989 0.0.0.0:* LISTEN
[root@monitor ~]# mmm_control show
# Warning: agent on host db4 is not reachable
db1(192.168.100.101) master/ONLINE. Roles: writer(192.168.100.250)
db2(192.168.100.102) master/ONLINE. Roles:
db3(192.168.100.103) slave/ONLINE. Roles: reader(192.168.100.251)
db4(192.168.100.104) slave/ONLINE. Roles: reader(192.168.100.252)
[root@monitor ~]# mmm_control show
# Warning: agent on host db4 is not reachable
db1(192.168.100.101) master/HARD_OFFLINE. Roles:
db2(192.168.100.102) master/ONLINE. Roles: writer(192.168.100.250)
db3(192.168.100.103) slave/ONLINE. Roles: reader(192.168.100.251)
db4(192.168.100.104) slave/ONLINE. Roles:
可以发现,服务器vip自动飘到了第二个上面