标签:监控 not lin source 工作原理 format 授权登录 策略 技术
简介MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡。由于MMM无法完全的保证数据一致性,所以MMM适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景。对于那些对数据的一致性要求很高的业务,非常不建议采用MMM这种高可用架构。
MMM(Master-Master replication managerfor Mysql,Mysql主主复制管理器)是一套灵活的脚本程序,基于perl实现,用来对mysql replication进行监控和故障迁移,并能管理mysql Master-Master复制配置。如图所示:
mmm_mond:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监管机上运行。
mmm_agent:运行在每个mysql服务器上的代理进程,完成监控的探针工作和执行简单的远端服务设置。此脚本需要在被监管机上运行。
mmm_control:一个简单的脚本,提供管理mmm_mond进程的命令。
在整个监管过程中,需要在mysql中添加相关授权用户,以便让mysql可以支持监理机的维护。授权的用户包括一个mmm_monitor用户和一个mmm_agent用户,如果想使用mmm的备份工具则还要添加一个mmm_tools用户。
准备五台服务器模拟搭建,环境如表所示:
主机 | 操作系统 | IP地址 | 主要软件 |
---|---|---|---|
mysql-m1服务器 | CentOS7.4 x86_64 | 192.168.113.160 | MySQL5.7、MySQL-MMM |
mysql-m2服务器 | CentOS7.4 x86_64 | 192.168.113.161 | MySQL5.7、MySQL-MMM |
mysql-m3服务器 | CentOS7.4 x86_64 | 192.168.113.162 | MySQL5.7、MySQL-MMM |
mysql-m4服务器 | CentOS7.4 x86_64 | 192.168.113.165 | MySQL5.7、MySQL-MMM |
mysql-monitor | CentOS7.4 x86_64 | 192.168.113.156 | mariadb、MySQL-MMM |
监控主机也作为客户端使用
虚拟IP地址(VIP):
IP | Role |
---|---|
192.168.113.100 | writer |
192.168.113.210 | reader |
192.168.113.220 | reader |
sysemctl stop firewalld.service
setenforce 0
安装步骤详细参考Linux平台上安装MySQL服务。
[root@mysql-m1 ~]# vim /etc/my.cnf
[mysqld] //添加以下内容
binlog-ignore-db=mysql,information_schema //不需要同步的数据库名称
character_set_server=utf8
log_bin=mysql_bin //开启binlog日志用于主从数据复制
server_id=1 //每台server-id的值不要相同
log_slave_updates=true //此数据库宕机,备用数据库接管
sync_binlog=1 //每条自动更新,安全性高,默认是0
auto_increment_increment=2 //字段一次递增多少
auto_increment_offset=1 //自增字段的起始值:1,3,5,7...等奇数ID
每台MySQL主机server-id不能相同,其他配置文件参数相同即可,可以把配置文件复制到其它3台数据库服务器上,修改一下server-id就行。
scp /etc/my.cnf root@192.168.113.161:/etc/
scp /etc/my.cnf root@192.168.113.162:/etc/
scp /etc/my.cnf root@192.168.113.165:/etc/
systemctl start mysqld.service
mysql-m1:
[root@mysql-m1 ~]# mysql -uroot -pabc123
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql_bin.000007 | 154 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
mysql-m2:
[root@mysql-m2 ~]# mysql -uroot -pabc123
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql_bin.000013 | 154 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.01 sec)
mysql-m1:
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.113.%‘ identified by ‘123456‘; //给m2授予从的权限
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=‘192.168.113.161‘,
-> master_user=‘replication‘,
-> master_password=‘123456‘,
-> master_log_file=‘mysql_bin.000013‘,
-> master_log_pos=154; //配置同步
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql-m2:
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.113.%‘ identified by ‘123456‘; //给m1授予从的权限
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=‘192.168.113.161‘,
-> master_user=‘replication‘,
-> master_password=‘123456‘,
-> master_log_file=‘mysql_bin.000007‘,
-> master_log_pos=154; //配置同步
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave; //启动同步
mysql> show slave status\G;
结果如图所示:
mysql-m1的状态:
mysql-m2的状态:
主主同步配置完毕,查看同步状态Slave_IO和Slave_SQL为YES,说明主主同步成功。
mysql> create database kgc;
Query OK, 1 row affected (0.01 sec)
回到mysql-m1数据库服务器,查看刚才创建的库kgc,同步已经完成。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kgc |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.09 sec)
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql_bin.000007 | 460 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
mysql> change master to
-> master_host=‘192.168.113.161‘,
-> master_user=‘replication‘,
-> master_password=‘123456‘,
-> master_log_file=‘mysql_bin.000007‘,
-> master_log_pos=460;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave; //启动同步
mysql> show slave status\G;
在mysql-m3和mysql-m4可以看到主从配置复制成功。
CentOS默认没有mysql-mmm软件包,官方推荐使用epel源,五台主机都要安装epel源和MMM。
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*
-------------为mmm-agent授权--------------------
mysql> grant super, replication client, process on *.* to ‘mmm_agent‘@‘192.168.113.%‘ identified by ‘123456‘;
-------------为mmm-monitor授权--------------------
mysql> grant replication client on *.* to ‘mmm_monitor‘@‘192.168.113.%‘ identified by ‘123456‘;
系统中所有主机的配置文件内容都是一样的,包括监控主机mysql-monitor。
vim /etc/mysql-mmm/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.113.160 //mysql-m1主机
mode master
peer db2
</host>
<host db2>
ip 192.168.113.161 //mysql-m2主机
mode master
peer db1
</host>
<host db3>
ip 192.168.113.163 //mysql-m3主机
mode slave
</host>
<host db4>
ip 192.168.113.165 //mysql-m4主机
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.113.100 //writer 虚拟IP
mode exclusive //只有一个host可以进行写操作模式
</role>
<role reader>
hosts db3, db4
ips 192.168.113.210,192.168.113.220 //reader 虚拟IP
mode balanced //多个slave主机可以进行读操作模式
</role>
通过scp命令传送到其他四台:
scp /etc/mysql-mmm/mmm_common.conf root@192.168.113.161:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf root@192.168.113.162:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf root@192.168.113.165:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf root@192.168.113.156:/etc/mysql-mmm/
vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db1 //分别修改为db1、db2、db3和db4
vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /run/mysql-mmm-monitor.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond.status
ping_ips 192.168.113.160,192.168.113.161,192.168.113.162,192.168.113.165 //监控服务器的ip
auto_set_online 10
# 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 //mmm_monitor 用户名
monitor_password 123456 //mmm_monitor 密码
</host>
debug 0
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service
systemctl start mysql-mmm-monitor.service
[root@monitor ~]# cd /etc/mysql-mmm/
[root@mysql-mmm ~]# mmm_control show
db1(192.168.113.160) master/ONLINE. Roles: writer(192.168.113.100)
db2(192.168.113.161) master/ONLINE. Roles:
db3(192.168.113.162) slave/ONLINE. Roles: reader(192.168.113.220)
db4(192.168.113.165) slave/ONLINE. Roles: reader(192.168.113.210)
后面的虚拟IP是真正来访问MySQL数据库的。
yum -y install mariadb-server mariadb
systemctl start mariadb
mysql> grant all on *.* to ‘testdba‘@‘192.168.113.156‘ identified by ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.05 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
[root@monitor ~]# cd /etc/mysql-mmm/
[root@mysql-mmm ~]# mmm_control show
db1(192.168.113.160) master/HARD_OFFLINE. Roles:
db2(192.168.113.161) master/ONLINE. Roles: writer(192.168.113.100) //虚拟IP转移
db3(192.168.113.162) slave/ONLINE. Roles: reader(192.168.113.220)
db4(192.168.113.165) slave/ONLINE. Roles: reader(192.168.113.210)
宕掉db1数据库后,虚拟IP转移到另一台正常的数据库db2上
在监控机上用虚拟IP连接数据库,进行创建数据库测试。
[root@monitor ~]# mysql -utestdba -p -h 192.168.113.100
Enter password: //输入授权的密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 14304
Server version: 5.7.17-log Source distribution
MySQL [(none)]> //成功连接上数据库
MySQL [(none)]> create database test;
Query OK, 1 row affected (0.01 sec)
接下来回到mysql-m2数据库服务器上查看,创建的test数据库是否在到mysql-m2数据库上。
启动主db1数据库后,可以看到mysql-m1数据库已恢复在线状态,不过虚拟IP不会转移(可以执行命令mmm_control move_role writer db1手动转移)。
[root@monitor ~]# cd /etc/mysql-mmm/
[root@mysql-mmm ~]# mmm_control show
db1(192.168.113.160) master/ONLINE. Roles: //恢复在线状态
db2(192.168.113.161) master/ONLINE. Roles: writer(192.168.113.100) //虚拟IP转移
db3(192.168.113.162) slave/ONLINE. Roles: reader(192.168.113.220)
db4(192.168.113.165) slave/ONLINE. Roles: reader(192.168.113.210)
这时进入mysql-m1数据库查看主主同步是否正常,并查看数据是否正常。
由此可看,mysql-mmm故障切换正常。
模拟主从故障也是如此,宕掉mysql-m4数据库,虚拟IP会全部转移到另一台正常数据库上。
[root@monitor ~]# cd /etc/mysql-mmm/
[root@mysql-mmm ~]# mmm_control show
db1(192.168.113.160) master/ONLINE. Roles:
db2(192.168.113.161) master/ONLINE. Roles: writer(192.168.113.100)
db3(192.168.113.162) slave/ONLINE. Roles: reader(192.168.113.210), reader(192.168.113.220) //虚拟IP转移
db4(192.168.113.165) slave/HARD_OFFLINE. Roles:
标签:监控 not lin source 工作原理 format 授权登录 策略 技术
原文地址:http://blog.51cto.com/11134648/2145031