MMM简介
MMM(MySQL主主复制管理器)是一套支持双主故障切换和双主日常管理的脚本程序。主要用来监控和管理MySQL Master-Master(双主)复制,虽然叫双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主切换时备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个Slave的read负载均衡。
MMM时一套灵活的脚本程序,基于perl实现,用来对mysql replication进行监控和故障迁移,并能管理MySQL Master-Master复制的配置,如图所示:
关于MMM高可用架构的说明如下:
- mmm_mon:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监管机上运行。
- mmm_agent:运行在每个MySQL服务器上的代理进程,完成监控的探针工作和执行简单的远端服务设置。此脚本需要在被监管机上运行。
- mmm_control:一个简单的脚本,提供管理mmm_mond进程的命令。
- mysql-mmm的监管端会提供多个虚拟虚拟IP(VIP),包括一个可写VIP,多个可读VIP,通过监管的管理,这些IP会绑定在可用MySQL之上,当某一台宕机时,监管会将VIP迁移至其他MySQL.
实验环境:(mariadb数据库是mysql的一个分支,它们的命令、操作都一样)
1.搭建mariadb多主多从模式
(1)安装mariadb
1)所有服务器都配置ALI云源,然后安装epel-release源。
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum clean all && yum makecache
2)搭建本地YUM源。
[root@localhost ~]# yum -y install mariadb-server mariadb
[root@localhost ~]# systemctl stop firewalld.service //关闭防火墙//
[root@localhost ~]# setenforce 0
3)修改mariadb-m1主配置文件。
[root@localhost ~]# vim /etc/my.cnf
[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 //开启binlog日志用于主从数据复制//
server_id=1 //每台server-id的值不要相同//
log_slave_updates=true //此数据库宕机,备用数据库接管//
sync_binlog=1
auto_increment_increment=2 //字段一次递增多少//
auto_increment_offset=1 //自增字段的起始值//
[root@localhost ~]# systemctl start mariadb.service //开启mariadb//
[root@localhost ~]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3434/mysqld
4)没有问题后,把配置文件复制到其它3台数据库服务器上。
[root@localhost ~]# scp /etc/my.cnf [email protected]:/etc/ //m2//
[root@localhost ~]# scp /etc/my.cnf [email protected]:/etc/ //m3//
[root@localhost ~]# scp /etc/my.cnf [email protected]:/etc/ //m4//
注意:每台mariadb主机的server-id不能相同,其他配置文件参数相同即可。
(2)配置mariadb-m1、mariadb-m2主主模式
1)先查看log bin和pos值的位置。
m1:
[root@localhost ~]# mysql
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
m2:
[root@localhost ~]# mysql
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
2)mariadb-m1、mariadb-m2互相提升访问权限。
m1:
MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.126.%' identified by '123456';
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> change master to master_host='192.168.126.139',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=411;
Query OK, 0 rows affected (0.18 sec)
m2:
MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.126.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> change master to master_host='192.168.126.138',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=411;
Query OK, 0 rows affected (0.03 sec)
3)分别查看mariadb-m1、mariadb-m2服务器的主从状态。
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.126.139
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000003
Read_Master_Log_Pos: 411
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql_bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主主同步配置完毕,查看同步状态Slave_IO和Slave_SQL为YES,说明主主同步成功。
4)测试主主同步,在mariadb-m2新建一个库dba。
m2:
MariaDB [(none)]> create database dba; //创建数据库//
Query OK, 1 row affected (0.09 sec)
m1:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dba | //同步成功//
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.02 sec)
(3)配置mariadb-m3和mariadb-m4作为mariadb-m1的从库
1)先查看mariadb-m1 master的状态值。
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 581 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
2)mariadb-m3、mariadb-m4分别执行。
MariaDB [(none)]> change master to master_host='192.168.126.138',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=581;
Query OK, 0 rows affected (0.01 sec)
3)分别查看mariadb-m3和mariadb-m4服务器的主从状态,结果如下:
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.126.138
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 581
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4)在mariadb-m1建立数据库,测试主从、主主、同步情况。
MariaDB [(none)]> create database dba01; //在m1创建数据库//
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases; //mariadb-m3查看结果//
+--------------------+
| Database |
+--------------------+
| information_schema |
| dba01 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.03 sec)
MariaDB [(none)]> show databases; //mariadb-m4查看结果//
+--------------------+
| Database |
+--------------------+
| information_schema |
| dba01 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.04 sec)
2.安装配置MySQL-MMM
(1)在所有服务器上安装MMM,注意,epel源要配置好。
[root@localhost ~]# yum -y install mysql-mmm*
(2)修改/etc/mysql-mmm/mmm_common.conf 配置文件,系统中所有主机的该配置文件内容一样,包括监控主机mysql-monitor。
[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# vim mmm_common.conf
active_master_role writer
cluster_interface ens33 //网卡名称//
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replication
replication_password 123456 //登录密码
agent_user mmm_agent
agent_password 123456
//主服务器m1//
ip 192.168.126.138
mode master
peer db2
//主服务器m2//
ip 192.168.126.139
mode master
peer db1
//主服务器m3//
ip 192.168.126.136
mode slave
//主服务器m4//
ip 192.168.126.137
mode slave
hosts db1, db2 //m1 m2//
ips 192.168.126.188 //虚拟IP VIP//
mode exclusive
hosts db3, db4 //m3 m4//
ips 192.168.126.190, 192.168.126.199 //虚拟IP VIP//
mode balanced
(3)在监控主机上编辑/etc/mysql-mmm/mmm_mon.conf文件。
[root@localhost ~]# vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
ip 127.0.0.1
pid_path /run/mysql-mmm-monitor.pid
include mmm_common.conf
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.126.138,192.168.126.139,192.168.126.136,192.168.126.137/监控服务器的IP/
auto_set_online 10 //自动上线时间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_user mmm_monitor //用户名//
monitor_password 123456 //密码//
(4)在所有数据库上为mmm_agent和mmm_moniter授权。
MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.126.%' identified by '123456';
MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.126.%' identified by '123456';
(5)修改所有数据库的mmm_agent.conf。
[root@localhost mysql-mmm]# vim /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 //根据规划进行逐一调整//
(6)在所有数据库服务器上启动mysql-mmm-agent。
[root@localhost mysql-mmm]# systemctl start mysql-mmm-agent.service
[root@localhost mysql-mmm]# systemctl enable mysql-mmm-agent.service //开机自启动//
(7)启动mysql-mmm-monitor监控主机。
[root@localhost ~]# systemctl start mysql-mmm-monitor.service
(8)在监控服务器上查看各节点的情况。
[root@localhost ~]# mmm_control show
db1(192.168.126.138) master/ONLINE. Roles: writer(192.168.126.188) //虚拟IP//
db2(192.168.126.139) master/ONLINE. Roles:
db3(192.168.126.136) slave/ONLINE. Roles: reader(192.168.126.190)
db4(192.168.126.137) slave/ONLINE. Roles: reader(192.168.126.199
(9)故障转移切换
1)停止m1 确认 虚拟地址 188 是否移动到 m2 上。注意:主不会抢占
[root@localhost mysql-mmm]# systemctl stop mariadb.service //停止 m1主服务器//
[root@localhost ~]# mmm_control show
db1(192.168.126.138) master/HARD_OFFLINE. Roles: //离线状态//
db2(192.168.126.139) master/ONLINE. Roles: writer(192.168.126.188)
db3(192.168.126.136) slave/ONLINE. Roles: reader(192.168.126.190)
db4(192.168.126.137) slave/ONLINE. Roles: reader(192.168.126.199)
[root@localhost mysql-mmm]# systemctl stop mariadb.service //停止 m3从服务器//
[root@localhost ~]# mmm_control show
db1(192.168.126.138) master/HARD_OFFLINE. Roles:
db2(192.168.126.139) master/ONLINE. Roles: writer(192.168.126.188)
db3(192.168.126.136) slave/HARD_OFFLINE. Roles:
db4(192.168.126.137) slave/ONLINE. Roles: reader(192.168.126.190), reader(192.168.126.199)
3)在m1服务器上为监控机地址授权登录。
MariaDB [(none)]> grant all on *.* to 'testdba'@'192.168.126.140' identified by '123456';
MariaDB [(none)]> flush privileges; //刷新//
4)在监控服务器上登录。
[root@localhost ~]# yum install mariadb-server mariadb -y
[root@localhost ~]# mysql -utestdba -p -h 192.168.126.188 //虚拟IP//
Enter password: //密码123456//
.....//省略//
MariaDB [(none)]>
5)在监控服务器上创建数据,测试同步情况。
MariaDB [(none)]> create database abc01;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases; //主服务器M1//
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc01 |
| dba01 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.03 sec)
MariaDB [(none)]> show databases; //从服务器M3//
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc01 |
| dba01 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.05 sec)