MySQL本身並沒有提供replication failover的解決方案,透過MMM方案能實現伺服器的故障轉移,從而實現mysql的高可用。 MMM不僅能提供浮動IP的功能,如果目前的主伺服器掛掉後,會將你後端的從伺服器自動轉向新的主伺服器進行同步複製,不用手動更改同步配置
一、MMM簡介:
MMM即Multi-Master Replication Manager for MySQL:mysql多主複製管理器,基於perl實作,關於mysql主主複製配置的監控、故障轉移和管理的一套可伸縮的腳本套件(在任何時候只有一個節點可以被寫入),MMM也能對從伺服器進行讀取負載平衡,所以可以用它來在一組用於複製的伺服器啟動虛擬ip,除此之外,它還有實作資料備份、節點之間重新同步功能的腳本。 MySQL本身並沒有提供replication failover的解決方案,透過MMM方案能實現伺服器的故障轉移,從而實現mysql的高可用。 MMM不僅能提供浮動IP的功能,如果目前的主伺服器掛掉後,會將你後端的從伺服器自動轉向新的主伺服器進行同步複製,不用手動更改同步配置。這個方案是目前比較成熟的解決方案。詳情請看官網:http://mysql-mmm.org
#優點:高可用性,擴充性好,故障自動切換,對於主主同步,在同一時間只提供一台資料庫寫入操作,保證的資料的一致性。當主伺服器掛掉以後,另一個主立即接管,其他的從伺服器能自動切換,不用人工幹預。
缺點:monitor節點是單點,不過這個你也可以結合keepalived或haertbeat做成高可用;至少三個節點,對主機的數量有要求,需要實作讀寫分離,還需要在前端編寫讀寫分離程式。在讀寫非常繁忙的業務系統下表現不是很穩定,可能會出現複製延遲、切換失效等問題。 MMM方案並不太適應於對資料安全性要求很高,並且讀取、寫入繁忙的環境中。
適用場景:
MMM的適用場景為資料庫存取量大,並且能實現讀寫分離的場景。
Mmm主要功能由下面三個腳本提供:
mmm_mond 負責所有的監控工作的監控守護進程,決定節點的移除(mmm_mond進程定時心跳檢測,失敗則將write ip浮動到另外一台master )等等
mmm_agentd 運行在mysql伺服器上的代理守護進程,透過簡單遠端服務集提供給監控節點
mmm_control 透過命令列管理mmm_mond進程
在整個監管過程中,需要在mysql中添加相關授權用戶,授權的用戶包括一個mmm_monitor用戶和一個mmm_agent用戶,如果想使用mmm的備份工具則還要添加一個mmm_tools用戶。
二、部署實作
1、環境介紹
OS:centos7.2(64位元)資料庫系統:mysql5.7.13
#關閉selinux
設定ntp,同步時間
#角色 |
hostname |
Server-id |
Write vip |
Read vip |
|
Master1 |
#192.168.31.83 |
||||
1 |
192.168.31.2 |
||||
#Master2(backup) | 192.168.31.141 | master2 | |||
#192.168.31.3 | ##Slave1 | 192.168.31.250 |
#slave1 |
3 |
|
#Slave2 |
##192.168.31.225 | slave2 | 4 |
2、在所有主機上設定/etc/hosts文件,新增以下內容:
192.168.31.83 master1
192.168.31.141 master2
192.168.31.250 slave1##192
192.168.31.250 slave1##1921.16251. slave2
192.168.31.106 monitor1
在所有主機上安裝perl、perl-develperl-CPAN libart_lgpl.x86_64 rrdtool.x86_64 rrdtool-perl.x86_64 rrdtool.x86_64 rrdtool-perl.x86_64*#cll .x86_64 rrdtool.x86_64 rrdtool-perl.x86_64
附註:使用centos7線上yum來源安裝
#安裝perl的相關函式庫##cpan -#安裝perl的相關函式庫
##cpan -
#安裝perl的相關函式庫
#cpan - i Algorithm::Diff Class::Singleton DBI DBD::mysql Log::Dispatch Log::Log4perl Mail::Send Net::Ping Proc::Daemon Time::HiRes Params::Validate Net::ARP
在每個mysql的設定檔/etc/my.cnf中加入以下內容, 注意server_id不能重複。
master1主機:
log-bin = mysql-bin binlog_format = mixed server-id = 1 relay-log = relay-bin relay-log-index = slave-relay-bin.index log-slave-updates = 1 auto-increment-increment = 2 auto-increment-offset = 1 master2主机: log-bin = mysql-bin binlog_format = mixed server-id = 2 relay-log = relay-bin relay-log-index = slave-relay-bin.index log-slave-updates = 1 auto-increment-increment = 2 auto-increment-offset = 2 slave1主机: server-id = 3 relay-log = relay-bin relay-log-index = slave-relay-bin.index read_only = 1 slave2主机: server-id = 4 relay-log = relay-bin relay-log-index = slave-relay-bin.index read_only = 1在完成了對my.cnf的修改後,透過systemctl restart mysqld重新啟動mysql服務
4台資料庫主機若要開啟防火牆,或關閉防火牆或建立存取規則:
firewall-cmd --reload
主從配置(master1和master2配置成主主,slave1和slave2配置成master1的從):
在master1上授權:
mysql> grant replication slave on *.* to rep@'192.168.31.%' identified by '123456';
在master2上授權:
mysql> grant replication slave on *.* to rep@'192.168.31.%' identified by '123456';
mysql> show master status; +------------------+----------+--------------+------------------+--------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+---------------------------------------------------+ | mysql-bin.000001 | 452 | | | | +------------------+----------+--------------+------------------+-----------------------------------------------------+在master2、slave1和slave2執行
mysql> change master to master_host='192.168.31.83',master_port=3306,master_user='rep',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=452; mysql>slave start;驗證主從複製:
master2主機:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.31.83 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.31.83 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.31.83 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show master status; +------------------+----------+--------------+------------------+--------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+---------------------------------------------------+ | mysql-bin.000001 | 452 | | | | +------------------+----------+--------------+------------------+----------------------------------------------------+
mysql> change master to master_host='192.168.31.141',master_port=3306,master_user='rep',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=452; mysql> start slave;驗證主從複製:
master1主機:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.31.141 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes如果Slave_IO_Running和Slave_SQL_Running都為yes,那麼主從就已經配置OK了
4、mysql-mmm配置:
在4台mysql節點上建立使用者
建立代理帳號:
mysql> grant super,replicationclient,process on *.* to 'mmm_agent'@'192.168.31.%' identified by '123456';
建立監控帳號:
mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.31.%' identified by '123456';
mysql> select user,host from mysql.user where user in ('mmm_monitor','mmm_agent'); +-------------+----------------------------+ | user | host | +-------------+----------------------------+ | mmm_agent | 192.168.31.% | | mmm_monitor | 192.168.31.% | +-------------+------------------------------+或
mysql> show grants for 'mmm_agent'@'192.168.31.%'; +-----------------------------------------------------------------------------------------------------------------------------+ | Grants for mmm_agent@192.168.31.% | +-----------------------------------------------------------------------------------------------------------------------------+ | GRANT PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'mmm_agent'@'192.168.31.%' | +-----------------------------------------------------------------------------------------------------------------------------+ mysql> show grants for 'mmm_monitor'@'192.168.31.%'; +-----------------------------------------------------------------------------------------------------------------------------+ | Grants for mmm_monitor@192.168.31.% | +-----------------------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.31.%' |註2:
mmm_monitor用戶: mmm監控用於對mysql伺服器進程健康檢查mmm_agent用戶:mmm代理用來更改唯讀模式,複製的主伺服器等5、mysql-mmm安裝
在monitor主機(192.168.31.106)上安裝監控程式cd /tmp wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz tar -zxf mysql-mmm-2.2.1.tar.gz cd mysql-mmm-2.2.1 make install
cd /tmp wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz tar -zxf mysql-mmm-2.2.1.tar.gz cd mysql-mmm-2.2.1 make install#####6、設定mmm#### #####編寫設定文件,五台主機必須一致:###完成安裝後,所有的設定檔都放到了/etc/mysql-mmm/下面。管理伺服器和資料庫伺服器上都要包含一個共同的檔案mmm_common.conf,內容如下:###active_master_rolewriter#積極的master角色的標示,所有的db伺服器要開啟read_only參數,對於writer伺服器監控代理程式會自動將read_only屬性關閉。 ######
<host default> cluster_interfaceeno16777736#群集的网络接口 pid_path /var/run/mmm_agentd.pid#pid路径 bin_path /usr/lib/mysql-mmm/#可执行文件路径 replication_user rep#复制用户 replication_password 123456#复制用户密码 agent_usermmm_agent#代理用户 agent_password 123456#代理用户密码 </host> <host master1>#master1的host名 ip 192.168.31.83#master1的ip mode master#角色属性,master代表是主 peer master2#与master1对等的服务器的host名,也就是master2的服务器host名 </host> <host master2>#和master的概念一样 ip 192.168.31.141 mode master peer master1 </host> <host slave1>#从库的host名,如果存在多个从库可以重复一样的配置 ip 192.168.31.250#从的ip mode slave#slave的角色属性代表当前host是从 </host> <host slave2>#和slave的概念一样 ip 192.168.31.225 mode slave </host> <role writer>#writer角色配置
hosts master1,master2#能进行写操作的服务器的host名,如果不想切换写操作这里可以只配置master,这样也可以避免因为网络延时而进行write的切换,但是一旦master出现故障那么当前的MMM就没有writer了只有对外的read操作。
ips 192.168.31.2#对外提供的写操作的虚拟IP
mode exclusive#exclusive代表只允许存在一个主,也就是只能提供一个写的IP
adb36db4876d5ae8ea335a15e6246fd6
53f263d2cc20f60e093642c0c2da6c77#read角色配置
hosts master2,slave1,slave2#对外提供读操作的服务器的host名,当然这里也可以把master加进来
ips 192.168.31.3, 192.168.31.4, 192.168.31.5#对外提供读操作的虚拟ip,这三个ip和host不是一一对应的,并且ips也hosts的数目也可以不相同,如果这样配置的话其中一个hosts会分配两个ip
mode balanced#balanced代表负载均衡
adb36db4876d5ae8ea335a15e6246fd6
同时将这个文件拷贝到其它的服务器,配置不变
#for host in master1 master2 slave1 slave2 ; do scp /etc/mysql-mmm/mmm_common.conf $host:/etc/mysql-mmm/ ; done
代理文件配置
编辑 4台mysql节点机上的/etc/mysql-mmm/mmm_agent.conf
在数据库服务器上,还有一个mmm_agent.conf需要修改,其内容是:
includemmm_common.conf
this master1
注意:这个配置只配置db服务器,监控服务器不需要配置,this后面的host名改成当前服务器的主机名。
启动代理进程
在 /etc/init.d/mysql-mmm-agent的脚本文件的#!/bin/sh下面,加入如下内容
source /root/.bash_profile
添加成系统服务并设置为自启动
#chkconfig --add mysql-mmm-agent #chkconfigmysql-mmm-agent on #/etc/init.d/mysql-mmm-agent start
注:添加source /root/.bash_profile目的是为了mysql-mmm-agent服务能启机自启。
自动启动和手动启动的唯一区别,就是激活一个console 。那么说明在作为服务启动的时候,可能是由于缺少环境变量
服务启动失败,报错信息如下:
Daemon bin: '/usr/sbin/mmm_agentd' Daemon pid: '/var/run/mmm_agentd.pid' Starting MMM Agent daemon... Can't locate Proc/Daemon.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/sbin/mmm_agentd line 7. BEGIN failed--compilation aborted at /usr/sbin/mmm_agentd line 7. failed
解决方法:
# cpanProc::Daemon # cpan Log::Log4perl # /etc/init.d/mysql-mmm-agent start Daemon bin: '/usr/sbin/mmm_agentd' Daemon pid: '/var/run/mmm_agentd.pid' Starting MMM Agent daemon... Ok # netstat -antp | grep mmm_agentd tcp 0 0 192.168.31.83:9989 0.0.0.0:* LISTEN 9693/mmm_agentd 配置防火墙 firewall-cmd --permanent --add-port=9989/tcp firewall-cmd --reload 编辑 monitor主机上的/etc/mysql-mmm/mmm_mon.conf includemmm_common.conf
<monitor> ip 127.0.0.1##为了安全性,设置只在本机监听,mmm_mond默认监听9988 pid_path /var/run/mmm_mond.pid bin_path /usr/lib/mysql-mmm/ status_path/var/lib/misc/mmm_mond.status ping_ips192.168.31.83,192.168.31.141,192.168.31.250,192.168.31.225#用于测试网络可用性 IP 地址列表,只要其中有一个地址 ping 通,就代表网络正常,这里不要写入本机地址 auto_set_online 0#设置自动online的时间,默认是超过60s就将它设置为online,默认是60s,这里将其设为0就是立即online </monitor>
<check default> check_period 5 trap_period 10 timeout 2 #restart_after 10000 max_backlog 86400 </check> check_period
描述:检查周期默认为5s
默认值:5s
trap_period
描述:一个节点被检测不成功的时间持续trap_period秒,就慎重的认为这个节点失败了。
默认值:10s
timeout
描述:检查超时的时间
默认值:2s
restart_after
描述:在完成restart_after次检查后,重启checker进程
默认值:10000
max_backlog
描述:记录检查rep_backlog日志的最大次数
默认值:60
<host default> monitor_usermmm_monitor#监控db服务器的用户 monitor_password 123456#监控db服务器的密码 </host> debug 0#debug 0正常模式,1为debug模式 启动监控进程: 在 /etc/init.d/mysql-mmm-agent的脚本文件的#!/bin/sh下面,加入如下内容 source /root/.bash_profile 添加成系统服务并设置为自启动 #chkconfig --add mysql-mmm-monitor #chkconfigmysql-mmm-monitor on #/etc/init.d/mysql-mmm-monitor start
启动报错:
Starting MMM Monitor daemon: Can not locate Proc/Daemon.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/sbin/mmm_mond line 11. BEGIN failed--compilation aborted at /usr/sbin/mmm_mond line 11. failed
解决方法:安装下列perl的库
#cpanProc::Daemon #cpan Log::Log4perl [root@monitor1 ~]# /etc/init.d/mysql-mmm-monitor start Daemon bin: '/usr/sbin/mmm_mond' Daemon pid: '/var/run/mmm_mond.pid' Starting MMM Monitor daemon: Ok [root@monitor1 ~]# netstat -anpt | grep 9988 tcp 0 0 127.0.0.1:9988 0.0.0.0:* LISTEN 8546/mmm_mond
注1:无论是在db端还是在监控端如果有对配置文件进行修改操作都需要重启代理进程和监控进程。
注2:MMM启动顺序:先启动monitor,再启动 agent
检查集群状态:
[root@monitor1 ~]# mmm_control show master1(192.168.31.83) master/ONLINE. Roles: writer(192.168.31.2) master2(192.168.31.141) master/ONLINE. Roles: reader(192.168.31.5) slave1(192.168.31.250) slave/ONLINE. Roles: reader(192.168.31.4) slave2(192.168.31.225) slave/ONLINE. Roles: reader(192.168.31.3)
如果服务器状态不是ONLINE,可以用如下命令将服务器上线,例如:
#mmm_controlset_online主机名
例如:[root@monitor1 ~]#mmm_controlset_onlinemaster1
从上面的显示可以看到,写请求的VIP在master1上,所有从节点也都把master1当做主节点。
查看是否启用vip
[root@master1 ~]# ipaddr show dev eno16777736 eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000 link/ether 00:0c:29:6d:2f:82 brdff:ff:ff:ff:ff:ff inet 192.168.31.83/24 brd 192.168.31.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.31.2/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe6d:2f82/64 scope link valid_lft forever preferred_lft forever [root@master2 ~]# ipaddr show dev eno16777736 eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000 link/ether 00:0c:29:75:1a:9c brdff:ff:ff:ff:ff:ff inet 192.168.31.141/24 brd 192.168.31.255 scope global dynamic eno16777736 valid_lft 35850sec preferred_lft 35850sec inet 192.168.31.5/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe75:1a9c/64 scope link valid_lft forever preferred_lft forever [root@slave1 ~]# ipaddr show dev eno16777736 eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000 link/ether 00:0c:29:02:21:19 brdff:ff:ff:ff:ff:ff inet 192.168.31.250/24 brd 192.168.31.255 scope global dynamic eno16777736 valid_lft 35719sec preferred_lft 35719sec inet 192.168.31.4/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe02:2119/64 scope link valid_lft forever preferred_lft forever [root@slave2 ~]# ipaddr show dev eno16777736 eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000 link/ether 00:0c:29:e2:c7:fa brdff:ff:ff:ff:ff:ff inet 192.168.31.225/24 brd 192.168.31.255 scope global dynamic eno16777736 valid_lft 35930sec preferred_lft 35930sec inet 192.168.31.3/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fee2:c7fa/64 scope link valid_lft forever preferred_lft forever 在master2,slave1,slave2主机上查看主mysql的指向 mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.31.83 Master_User: rep Master_Port: 3306 Connect_Retry: 60
MMM高可用性测试:
服务器读写采有VIP地址进行读写,出现故障时VIP会漂移到其它节点,由其它节点提供服务。
首先查看整个集群的状态,可以看到整个集群状态正常
[root@monitor1 ~]# mmm_control show master1(192.168.31.83) master/ONLINE. Roles: writer(192.168.31.2) master2(192.168.31.141) master/ONLINE. Roles: reader(192.168.31.5) slave1(192.168.31.250) slave/ONLINE. Roles: reader(192.168.31.4) slave2(192.168.31.225) slave/ONLINE. Roles: reader(192.168.31.3)
模拟master1宕机,手动停止mysql服务,观察monitor日志,master1的日志如下:
[root@monitor1 ~]# tail -f /var/log/mysql-mmm/mmm_mond.log 2017/01/09 22:02:55 WARN Check 'rep_threads' on 'master1' is in unknown state! Message: UNKNOWN: Connect error (host = 192.168.31.83:3306, user = mmm_monitor)! Can't connect to MySQL server on '192.168.31.83' (111) 2017/01/09 22:02:55 WARN Check 'rep_backlog' on 'master1' is in unknown state! Message: UNKNOWN: Connect error (host = 192.168.31.83:3306, user = mmm_monitor)! Can't connect to MySQL server on '192.168.31.83' (111) 2017/01/09 22:03:05 ERROR Check 'mysql' on 'master1' has failed for 10 seconds! Message: ERROR: Connect error (host = 192.168.31.83:3306, user = mmm_monitor)! Can't connect to MySQL server on '192.168.31.83' (111) 2017/01/09 22:03:07 FATAL State of host 'master1' changed from ONLINE to HARD_OFFLINE (ping: OK, mysql: not OK) 2017/01/09 22:03:07 INFO Removing all roles from host 'master1': 2017/01/09 22:03:07 INFO Removed role 'writer(192.168.31.2)' from host 'master1' 2017/01/09 22:03:07 INFO Orphaned role 'writer(192.168.31.2)' has been assigned to 'master2'
查看群集的最新状态
[root@monitor1 ~]# mmm_control show master1(192.168.31.83) master/HARD_OFFLINE. Roles: master2(192.168.31.141) master/ONLINE. Roles: reader(192.168.31.5), writer(192.168.31.2) slave1(192.168.31.250) slave/ONLINE. Roles: reader(192.168.31.4) slave2(192.168.31.225) slave/ONLINE. Roles: reader(192.168.31.3)
从显示结果可以看出master1的状态有ONLINE转换为HARD_OFFLINE,写VIP转移到了master2主机上。
检查所有的db服务器群集状态
[root@monitor1 ~]# mmm_control checks all master1 ping [last change: 2017/01/09 21:31:47] OK master1 mysql [last change: 2017/01/09 22:03:07] ERROR: Connect error (host = 192.168.31.83:3306, user = mmm_monitor)! Can't connect to MySQL server on '192.168.31.83' (111) master1 rep_threads [last change: 2017/01/09 21:31:47] OK master1 rep_backlog [last change: 2017/01/09 21:31:47] OK: Backlog is null slave1 ping [last change: 2017/01/09 21:31:47] OK slave1mysql [last change: 2017/01/09 21:31:47] OK slave1 rep_threads [last change: 2017/01/09 21:31:47] OK slave1 rep_backlog [last change: 2017/01/09 21:31:47] OK: Backlog is null master2 ping [last change: 2017/01/09 21:31:47] OK master2 mysql [last change: 2017/01/09 21:57:32] OK master2 rep_threads [last change: 2017/01/09 21:31:47] OK master2 rep_backlog [last change: 2017/01/09 21:31:47] OK: Backlog is null slave2 ping [last change: 2017/01/09 21:31:47] OK slave2mysql [last change: 2017/01/09 21:31:47] OK slave2 rep_threads [last change: 2017/01/09 21:31:47] OK slave2 rep_backlog [last change: 2017/01/09 21:31:47] OK: Backlog is null
从上面可以看到master1能ping通,说明只是服务死掉了。
查看master2主机的ip地址:
[root@master2 ~]# ipaddr show dev eno16777736 eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000 link/ether 00:0c:29:75:1a:9c brdff:ff:ff:ff:ff:ff inet 192.168.31.141/24 brd 192.168.31.255 scope global dynamic eno16777736 valid_lft 35519sec preferred_lft 35519sec inet 192.168.31.5/32 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.31.2/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe75:1a9c/64 scope link valid_lft forever preferred_lft forever
slave1主机:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.31.141 Master_User: rep Master_Port: 3306
slave2主机:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.31.141 Master_User: rep Master_Port: 3306
启动master1主机的mysql服务,观察monitor日志,master1的日志如下:
[root@monitor1 ~]# tail -f /var/log/mysql-mmm/mmm_mond.log 2017/01/09 22:16:56 INFO Check 'mysql' on 'master1' is ok! 2017/01/09 22:16:56 INFO Check 'rep_backlog' on 'master1' is ok! 2017/01/09 22:16:56 INFO Check 'rep_threads' on 'master1' is ok! 2017/01/09 22:16:59 FATAL State of host 'master1' changed from HARD_OFFLINE to AWAITING_RECOVERY
从上面可以看到master1的状态由hard_offline改变为awaiting_recovery状态
用如下命令将服务器上线:
[root@monitor1 ~]#mmm_controlset_onlinemaster1
查看群集最新状态
[root@monitor1 ~]# mmm_control show master1(192.168.31.83) master/ONLINE. Roles: master2(192.168.31.141) master/ONLINE. Roles: reader(192.168.31.5), writer(192.168.31.2) slave1(192.168.31.250) slave/ONLINE. Roles: reader(192.168.31.4) slave2(192.168.31.225) slave/ONLINE. Roles: reader(192.168.31.3)
可以看到主库启动不会接管主,只到现有的主再次宕机。
总结
(1)master2备选主节点宕机不影响集群的状态,就是移除了master2备选节点的读状态。
(2)master1主节点宕机,由master2备选主节点接管写角色,slave1,slave2指向新master2主库进行复制,slave1,slave2会自动change master到master2.
(3)如果master1主库宕机,master2复制应用又落后于master1时就变成了主可写状态,这时的数据主无法保证一致性。
如果master2,slave1,slave2延迟于master1主,这个时master1宕机,slave1,slave2将会等待数据追上db1后,再重新指向新的主node2进行复制操作,这时的数据也无法保证同步的一致性。
(4)如果采用MMM高可用架构,主,主备选节点机器配置一样,而且开启半同步进一步提高安全性或采用MariaDB/mysql5.7进行多线程从复制,提高复制的性能。
附:
1、日志文件:
日志文件往往是分析错误的关键,所以要善于利用日志文件进行问题分析。
db端:/var/log/mysql-mmm/mmm_agentd.log
监控端:/var/log/mysql-mmm/mmm_mond.log
2、命令文件:
mmm_agentd:db代理进程的启动文件
mmm_mond:监控进程的启动文件
mmm_backup:备份文件
mmm_restore:还原文件
mmm_control:监控操作命令文件
db服务器端只有mmm_agentd程序,其它的都是在monitor服务器端。
3、mmm_control用法
mmm_control程序可以用于监控群集状态、切换writer、设置online\offline操作等。
Valid commands are:
help - show this message #帮助信息
ping - ping monitor #ping当前的群集是否正常
show - show status #群集在线状态检查
checks [f7e6dec31ab1a0471d06c55afaca8d77|all [268cfb9ae487ce9877c28672167a818c|all]] - show checks status#执行监控检查操作
set_onlinef7e6dec31ab1a0471d06c55afaca8d77 - set host f7e6dec31ab1a0471d06c55afaca8d77 online #将host设置为online
set_offlinef7e6dec31ab1a0471d06c55afaca8d77 - set host f7e6dec31ab1a0471d06c55afaca8d77 offline #将host设置为offline
mode - print current mode. #打印输出当前的mode
set_active - switch into active mode.
set_manual - switch into manual mode.
set_passive - switch into passive mode.
move_role [--force] 3b3677fa5ae28346828080dc6d333550f7e6dec31ab1a0471d06c55afaca8d77 - move exclusive role 3b3677fa5ae28346828080dc6d333550 to host f7e6dec31ab1a0471d06c55afaca8d77 #移除writer服务器为指定的host服务器(Only use --force if you know what you are doing!)
set_ipfb7c3ed00d0ce5f01877a916db4eae14f7e6dec31ab1a0471d06c55afaca8d77 - set role with ipfb7c3ed00d0ce5f01877a916db4eae14 to host f7e6dec31ab1a0471d06c55afaca8d77
检查所有的db服务器群集状态:
[root@monitor1 ~]# mmm_control checks all
检查项包括:ping、mysql是否正常运行、复制线程是否正常等
检查群集环境在线状况:
[root@monitor1 ~]# mmm_control show
对指定的host执行offline操作:
[root@monitor1 ~]# mmm_controlset_offline slave2
对指定的host执行onine操作:
[root@monitor1 ~]# mmm_controlset_online slave2
执行write切换(手动切换):
查看当前的slave对应的master
[root@slave2 ~]# mysql -uroot -p123456 -e 'show slave status\G;'
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.141
writer切换,要确保mmm_common.conf文件中的writer属性有配置对应的host,否则无法切换
[root@monitor1 ~]# mmm_controlmove_role writer master1
OK: Role 'writer' has been moved from 'master2' to 'master1'. Now you can wait some time and check new roles info!
[root@monitor1 ~]# mmm_control show
master1(192.168.31.83) master/ONLINE. Roles: writer(192.168.31.2)
master2(192.168.31.141) master/ONLINE. Roles: reader(192.168.31.5)
slave1(192.168.31.250) slave/ONLINE. Roles: reader(192.168.31.4)
slave2(192.168.31.225) slave/ONLINE. Roles: reader(192.168.31.3)
save从库自动切换到了新的master
[root@slave2 ~]# mysql -uroot -p123456 -e 'show slave status\G;'
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.83
4、其它处理问题
如果不想讓writer從master切換到backup(包含主從的延遲也會導致寫VIP的切換),那麼可以在設定/etc/mysql-mmm/mmm_common.conf時,去掉3612df8997eca9306e1f789dddf71f78中的backup
7d838a82ef25e8e553cd0c3c3318349b#writer角色配置
hosts master1 #這裡只配置一個Hosts
ips 192.168.31.2#對外提供的寫入操作的虛擬IP
mode exclusive #exclusive代表代表只允許存在一個主,也就是只能提供一個寫的IP
adb36db4876d5ae8ea335a15e6246fd6
這樣的話當master1故障了writer寫操作不會切換到master2伺服器,並且slave也不會指向新的master,此時當前的MMM之前對外提供寫入服務。
5、總結
1.對外提供讀寫的虛擬IP是由monitor程式控制。如果monitor沒有啟動那麼db伺服器不會被指派虛擬ip,但是如果已經指派了虛擬ip,當monitor程式關閉了原先指派的虛擬ip不會立即關閉外部程式還可以連線存取(只要不重新啟動網路),這樣的好處就是對於monitor的可靠性要求就會低一些,但是如果這個時候其中的某一個db伺服器故障了就無法處理切換,也就是原先的虛擬ip還是維持不變,掛掉的那台DB的虛擬ip會變的不可訪問。
2.agent程式受monitor程式的控制處理write切換,從庫切換等操作。如果monitor進程關閉了那麼agent進程就起不到什麼作用,它本身就不能處理故障。
3.monitor程式負責監控db伺服器的狀態,包括Mysql資料庫、伺服器是否運作、複製執行緒是否正常、主從延遲等;它也用於控制agent程式處理故障。
4.monitor會每隔幾秒鐘監控db伺服器的狀態,如果db伺服器已經從故障變成了正常,那麼monitor會自動在60s之後將其設定為online狀態(預設是60s可以設為其它的值),有監控端的設定檔參數「auto_set_online」決定,群集伺服器的狀態有三種分別是:HARD_OFFLINE→AWAITING_RECOVERY→online
5.預設monitor會控制mmm_agent會將writer dbread_only修改為##5.預設monitor會控制mmm_agent會將writer dbread_only修改為OFF,其它的db伺服器read_only修改為ON,所以為了嚴謹可以在所有的伺服器的my.cnf檔案中加入read_only=1由monitor控制來控制writer和read,root使用者和複製使用者不受read_only參數的影響。
以上是MySQL高可用解決方案MMM詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!