详解 MySQL 高可用群集,MMM搭建高可用

目录:

1·MMM 简介
2·MMM 各个角色说明
3·案例环境介绍
4·案例实施
5·总结


一:MMM 简介:

1)MMM 是什么:说得简单点,就是 MySQL 主主复制的管理器。之前的一篇文章讲述了 MySQL的主从复制 + 读写分离,其中原理都相同,有兴趣的朋友可以访问:MySQL主从复制 + 读写分离


2)还有就是,虽然是主主复制,但是在业务上的话,同一时间值额能对一个主进行写入,另一台就是备选。它可以实现故障切换,还可以实现多个 Slave 读 的负载均衡。可以自动或者手动切换主服务器,因为可以 虚拟VIP 的漂移。


3)但是 MMM 无法完全保证数据的一致性,所以它的应用场景应该是一些要求数据一致性不是很高,但是又要保证业务的可用性。


4)它的监管也需要让 MySQL 授权,以便让 MySQL 可以支持监理机的维护,授权的用户有:mmm_monitor 、mmm_agent ,如果要使用备份工具还需要添加 mmm_tools 用户的授权。


二:MMM 高可用架构角色说明:

1)mmm_mon:它是一个监控进程,负责所有的监控。决定和处理所有的角色活动,它需要在监管机上运行


2)mmm_agent : 它运行在每个MySQL 服务器上的代理进程,完成监控的探针工作和执行简单的远端服务设置。它也需要在被监管的机子上运行


3)mmm_control:一个简单的脚本,它提供 mmm_mond 进程命令


4)mysql-mmm 的监管端会提供多个虚拟 IP(VIP),包括一个可写和多个可读的 VIP,通过监管的管理,这些 IP 会绑定在可用的 MySQL 上,当一个主机宕机后,监管会将VIP迁移到其他的MySQL上。


MMM 高可用架构,如下图:
详解 MySQL 高可用群集,MMM搭建高可用


三: 案例环境介绍:

1·在这里需要声明下,mariadb 和 MySQL 是一样的,只是现在 MySQL 被oracle公司 收购后有些东西开始逐渐收费,所以这篇文章都是用 mariadb ,他和MySQL是一模一样的。


2·CenOS 默认没有mysql-mmm包,这里需要下载 epel 源,所以需要每台主机需要保持在线状态


3·这次环境使用五台服务器模拟搭建,环境表如下所示


主机 操作系统 IP 地址 主要软件
mysql-m1 主服务器 CentOS 7.4 192.168.106.154 epel-release源
mysql-m2 主服务器 CentOS 7.4 192.168.106.155 epel-release源
mysql-m3 从服务器 CentOS 7.4 192.168.106.156 epel-release源
mysql-m4 从服务器 CentOS 7.4 192.168.106.157 epel-release源
mysql-monitor 监控 CentOS 7.4 192.168.106.132 epel-release源、mysql-mmm

四:案例实施开始:

1·配置aliyun源,安装 epel-release 源,五台主机都需要安装,可同时进行

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo ---- (配置阿里云yum源)


2·安装 epel-release 源,五台主机都需要安装

[root@localhost ~]# yum -y install epel-release


3·清除缓存,建立数据源

[root@localhost ~]# yum clean all && yum makecache


4·安装mariadb-server(服务端)、mariadb (客户端),五台主机都需要装,监控机 也可以用来当客户端使用
[root@localhost ~]# yum -y install mariadb-server mariadb
[root@localhost ~]# systemctl stop firewalld -----(关闭防火墙)
[root@localhost ~]# setenforce 0


5)修改五台主机 mariadb 的主配置文件

[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
server_id=1
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid


6)以上配置需要主从服务器都修改

[root@localhost ~]# scp /etc/my.cnf [email protected]:/etc/ ----(发送给另一台主机,并且覆盖源文件)
[root@localhost ~]# scp /etc/my.cnf [email protected]:/etc/
[root@localhost ~]# scp /etc/my.cnf [email protected]:/etc/


7)这里需要说明:每天主机的server-id 不能一样,所以我们需要进入配置文件 vim /etc/my.cnf 修改server-id 的值,在使用 SCP 时需要进行身份验证, 请输入对方正确的密码


8)重启服务,查看3306端口是否启动:

[root@localhost ~]# systemctl start mariadb -----(启动服务)
[root@localhost ~]# netstat -anpt | grep 3306 ----(查看3306端口)
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2501/mysqld


9)开始配置主主复制,使两台主服务器之间能进行互相复制

MariaDB [(none)]> grant replication slave on . to 'replication' @'192.168.106.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec) -----(在第一台主服务器上授权)


MariaDB [(none)]> grant replication slave on . to 'replication' @'192.168.106.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec) -----(在第二台主服务器上授权)


10)在两台主服务器上指定 Master 的各种参数,使其能互相访问。

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 555 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
在第一台主服务器上 show master status;
需要记录 二进制日志文件、和地址


MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 555 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
在第二台主服务器上 show master status;
需要记录 二进制日志文件、和地址


change master to master_host='192.168.106.154',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=555; ----(在第一台主服务器上指定)


change master to master_host='192.168.106.154',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=555; ----(在第二台主服务器上指定)


11)开启同步、查看同步状态,主服务器都需要开启同步。

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.106.154
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000003
Read_Master_Log_Pos: 647
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 621
Relay_Master_Log_File: mysql_bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
-----两个YES说明同步成功


12)验证主主复制:

在m1 上创建一个库,在m2上查看是否复制。
MariaDB [(none)]> create database school;


详解 MySQL 高可用群集,MMM搭建高可用


13)安装 MySQL-mmm,在所有服务器上都需要安装。

[root@localhost ~]# yum -y install mysql-mmm ----(安装mmm 下所有的软件)


14)安装结束后,配置 mmm

[root@localhost ~]# vim /etc/mysql-mmm/mmm_common.conf ---(修改内容如下)


active_master_role writer

<host default>
cluster_interface ens33 ---(cenos7 的网卡名称为:ens33)
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replication ---(之前授权用户)
replication_password 123123 ----(密码)
agent_user mmm_agent ---(用户名)
agent_password 123123 -----(密码)
</host>

<host db1>
ip 192.168.106.154 ------(m1 的IP地址)
mode master
peer db2
</host>

<host db2>
ip 192.168.106.155 -----(m2 的IP地址)
mode master
peer db1
</host>

<host db3>
ip 192.168.106.156 -----(m3 的IP地址)
mode slave
</host>

<host db4>
ip 192.168.106.157 ----(m4 的ip地址)
mode slave
</host>

<role writer>
hosts db1, db2
ips 192.168.106.200 ------ (这里需要填写 两台主服务器的共同的 VIP)
mode exclusive
</role>

<role reader>
hosts db3, db4 ----(修改数字,读取池子应该使 从服务器)
ips 192.168.106.210, 192.168.106.220 (这里需要分别填写两台从服务器VIP 地址,这里需要两个VIP 地址)
mode balanced
</role>


15)远程发送mmm配置文件给所有主机:

[root@localhost ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/


[root@localhost ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/


[root@localhost ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/


[root@localhost ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/


16)配置监控服务器,最后一台服务器

[root@localhost ~]# vim /etc/mysql-mmm/mmm_mon.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.106.154,192.168.106.155,192.168.106.156,192.168.106.157 ---(需要监听主机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>

<host default>
monitor_user mmm_monitor
monitor_password 123456 ---(用户密码)
</host>

[root@localhost ~]# systemctl start mysql-mmm-monitor.service ---(启动监控)


17)在所有数据库上为mmm_agen 、mmm_moniter t授权

MariaDB [(none)]> grant super, replication client, process on . to 'mmm_agent'@'192.168.106.%' identified by '123456'; ----(所有数据库都需要授权)


MariaDB [(none)]> grant replication client on . to 'mmm_monitor'@'192.168.106.%' identified by '123456'; ---(所有数据库都需要授权)


MariaDB [(none)]> flush privileges; ----(刷新数据库)


18)修改所有 文件,指向自己是 db 1、2、3、4。

[root@localhost ~]# vim /etc/mysql-mmm/mmm_agent.conf -----(修改内容如下)
this db1 ----(根据之前代理名称的规划进行逐一调整每台服务器)


19)在所在数据库服务器上启动mysql-mmm-agent

[root@localhost ~]# systemctl start mysql-mmm-agent.service
[root@localhost ~]# systemctl enable mysql-mmm-agent.service ---(设置开机自启)


20)在监控机 mmm_monitor 上查看四台服务器状态:

mmm_control show //查看各节点的情况:
db1(192.168.235.132) master/ONLINE. Roles:writer(192.168.106.200)
db2(192.168.235.191) master/ONLINE. Roles:
db3(192.168.235.177) slave/ONLINE. Roles:reader(192.168.106.210)
db4(192.168.235.181) slave/ONLINE. Roles:reader(192.168.106.220)


21)故障切换,停止m1 服务,再次查看监控状态。停止m3 再次查看状态。使用另一台mysql登陆m1写入数据,查看是否同步!


总结:

1·需要知道其中原理的可以看看上一篇文章MySQL 主从复制+读写分离
2·mysql-mmm 适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景
3·在故障切换,和客户端写入数据时,可以看出后端的功能实现,都是走的真实IP,而客户端写入的时候或读取时都是走 VIP。

猜你喜欢

转载自blog.51cto.com/13746824/2173073