扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
如何搭建MySQL高可用MMM架构?相信大部分人都还没学会这个技能,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。
专注于为中小企业提供网站设计、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业沙河口免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,可以说是mysql主主复制管理器。虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡。关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写入),这个套件也能对居于标准的主从配置的任意数量的从服务器进行读负载均衡,所以你可以用它来在一组居于复制的服务器启动虚拟ip,除此之外,它还有实现数据备份、节点之间重新同步功能的脚本。
MMM提供了自动和手动两种方式移除一组服务器中复制延迟较高的服务器的虚拟ip,同时它还可以备份数据,实现两节点之间的数据同步等。由于MMM无法完全的保证数据一致性,所以MMM适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景。MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。对于那些对数据的一致性要求很高的业务,非常不建议采用MMM这种高可用架构。
MMM这种模式,master,slaver容易被控死,两个就两个
MHA模式,有高可扩展性,一主双备,一个区域。再扩展一主双备,一个区域
都要安装node
主服务器1 192.168.136.191 db1
主服务器2 192.168.136.168 db2
从服务器1 192.168.136.185 db3
从服务器2 192.168.136.184 db4
监控服务器 192.168.136.135
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 install mariadb-server mariadb -y`
vim /etc/my.cnf
9dd
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queries.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1 #注意每台id都要是不一样的
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
systemctl stop firewalld.service
setenforce 0
systemctl start mariadb.service
[root@localhost ~]# mysql #进入M1数据库
进入M1服务器查看日志文件的名称和位置值
MariaDB [(none)]> show master status; #查看日志文件的名称和位置值
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
grant replication slave on *.* to 'replication'@'192.168.136.%' identified by '123456';`
change master to master_host='192.168.136.168',master_user='relication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
change master to master_host='192.168.136.167',master_user='relication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
#两台服务器都执行开启同步数据
MariaDB [(none)]> slave start;
#两台主服务器都执行,查看同步数据的状态
MariaDB [(none)]> show slave status\G;
#看到下面的IO线程和状态都是YES就是正确了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#我们测试一下两个主服务器是否能同步数据
M1创建数据一个数据库
MariaDB [(none)]> create database myschool;
Query OK, 1 row affected (0.00 sec)
M2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myschool |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
#s1和s2从服务器
[root@localhost ~]# mysql
#都指向M1主服务器的地址,日志文件和参数
MariaDB [(none)]> change master to master_host='192.168.136.191',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
#在M1创建一个数据库
MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.00 sec)
#其他三台服务器都有这个数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
#所有服务器都要装mmm源码包
yum -y install mysql-mmm*
#到第一台主服务器开始配置
[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# ls
mmm_agent.conf mmm_common.conf mmm_mon.conf mmm_mon_log.conf mmm_tools.conf
[root@localhost mysql-mmm]# vim mmm_common.conf
cluster_interface ens33 #网卡是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 #代理密码
ip 192.168.136.191 #主服务器1地址
mode master
peer db2
ip 192.168.136.168 #主服务器2地址
mode master
peer db1
ip 192.168.136.185 #从服务器1地址
mode slave
ip 192.168.136.184 #从服务器2地址
mode slave
hosts db1, db2
ips 192.168.136.200 #主服务的虚拟地址,可以漂移
mode exclusive
hosts db3, db4
ips 192.168.136.210, 192.168.136.220 #从服务器1和2的虚拟地址
mode balanced
scp mmm_common.conf root@192.168.136.168:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.185:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.184:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.135:/etc/mysql-mmm/
[root@localhost mysql-mmm]# vim mmm_mon.conf
ping_ips 192.168.136.191,192.168.136.168,192.168.136.185,192.168.136.184 输入你所有的地址
auto_set_online 10 #自带上线时间为10s
monitor_password 123456 #修改密码为123456
在所有数据库给mmm_agent授权
grant super, replication client, process on *.* to 'mmm_agent'@'192.168.136.%' identified by '123456';
grant replication client on *.* to 'mmm_monitor'@'192.168.136.%' identified by '123456';
[root@localhost mysql-mmm]# vim mmm_agent.conf
this db2
this db3
this db4
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service
#回到监控服务器
systemctl start mysql-mmm-monitor.service
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles: writer(192.168.136.200)
db2(192.168.136.168) master/ONLINE. Roles:
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#更改绑定的虚拟地址
[root@localhost mysql-mmm]# mmm_control move_role writer db2
#测试监控服务器功能是否完善
[root@localhost mysql-mmm]# mmm_control checks all
db4 ping [last change: 2019/11/25 16:38:25] OK
db4 mysql [last change: 2019/11/25 16:38:25] OK
db4 rep_threads [last change: 2019/11/25 16:38:25] OK
db4 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
db2 ping [last change: 2019/11/25 16:38:25] OK
db2 mysql [last change: 2019/11/25 16:38:25] OK
db2 rep_threads [last change: 2019/11/25 16:38:25] OK
db2 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
db3 ping [last change: 2019/11/25 16:38:25] OK
db3 mysql [last change: 2019/11/25 16:38:25] OK
db3 rep_threads [last change: 2019/11/25 16:38:25] OK
db3 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
db1 ping [last change: 2019/11/25 16:38:25] OK
db1 mysql [last change: 2019/11/25 16:38:25] OK
db1 rep_threads [last change: 2019/11/25 16:38:25] OK
db1 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
[root@localhost mysql-mmm]# mmm_control move_role writer db1
OK: Role 'writer' has been moved from 'db2' to 'db1'. Now you can wait some time and check new roles info!
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles: writer(192.168.136.200)
db2(192.168.136.168) master/ONLINE. Roles:
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#第一台主服务器关闭数据库模拟故障
[root@localhost mysql-mmm]# systemctl stop mariadb.service
#回到监控服务器测试,虚拟网址就变更了
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/HARD_OFFLINE. Roles:
db2(192.168.136.168) master/ONLINE. Roles: writer(192.168.136.200)
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#再把第一台主服务器开启数据库
[root@localhost mysql-mmm]# systemctl start mariadb.service
#在回到监控服务器查看主服务器状态
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles:
db2(192.168.136.168) master/ONLINE. Roles: writer(192.168.136.200)
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#监控服务器
[root@localhost mysql-mmm]# yum install mariadb-server mariadb -y
#再M1服务器为监控器地址授权登录
MariaDB [(none)]> grant all on *.* to 'testba'@'192.168.136.135' identified by '123456';
MariaDB [(none)]> flush privileges;
mysql -utestdba -p -h 192.168.136.200
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2562
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> create database chen;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| chen |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
6 rows in set (0.01 sec)
到此为止,MySQL高可用MMM架构就搭建成功了,如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流