MySQL itself does not provide a replication failover solution. Server failover can be achieved through the MMM solution, thereby achieving high availability of mysql. MMM not only provides the function of floating IP, but also if the current master server hangs up, your back-end slave server will automatically be transferred to the new master server for synchronous replication without having to manually change the synchronization configuration
一, MMM introduction:
MMM is Multi-Master Replication Manager for MySQL: mysql multi-master replication manager, based on perl implementation, a set of monitoring, failover and management of mysql master-master replication configuration Scalable script suite (only one node can be written to at any time), MMM can also read load balance the slave servers, so it can be used to start virtual IPs on a group of servers for replication, among other things , it also has scripts to implement data backup and resynchronization functions between nodes. MySQL itself does not provide a replication failover solution. Server failover can be achieved through the MMM solution, thereby achieving high availability of MySQL. MMM not only provides the function of floating IP, but also if the current master server hangs up, your back-end slave server will automatically be transferred to the new master server for synchronous replication without having to manually change the synchronization configuration. This solution is currently a relatively mature solution. For details, please see the official website: http://mysql-mmm.org
##Advantages: High availability, good scalability, automatic switching in case of failure, for Master-master synchronization only provides one database write operation at the same time to ensure data consistency. When the master server hangs up, another master immediately takes over, and other slave servers can automatically switch without manual intervention.
Disadvantages: The monitor node is a single point, but you can also combine this with keepalived or haertbeat to make it highly available; there are at least three nodes, which requires the number of hosts and needs to be read and written. Separation requires writing a read-write separation program on the front end. The performance is not very stable in business systems that are very busy in reading and writing, and problems such as replication delays and switching failures may occur. The MMM solution is not very suitable for environments with high data security requirements and busy reading and writing.
Applicable scenarios:
The applicable scenarios for MMM are scenarios where the database access is large and reading and writing can be separated.The main functions of Mmm are provided by the following three scripts:
mmm_mond is the monitoring daemon process responsible for all monitoring work and determines the removal of nodes (mmm_mond process performs regular heartbeat detection, and if it fails, the write ip will be floated to another master ) etc.
mmm_agentd is an agent daemon running on the mysql server, and is provided to the monitoring node through a simple remote service set
mmm_control manages the mmm_mond process through the command line
During the entire monitoring process, you need to add it to mysql Relevant authorized users include an mmm_monitor user and an mmm_agent user. If you want to use mmm's backup tool, you must also add an mmm_tools user.
2. Deployment and implementation
1. Environment introduction OS: centos7.2 (64-bit) Database system: mysql5.7.13 Close selinux Configure ntp, synchronize timeIP | hostname | Server-id | ##Write vip | Read vip | ##Master1 |
192.168.31.83 |
master1 |
1 |
##192.168.31.2 | Master2(backup) |
|
192.168.31.141 | ##master22 | #192.168.31.3 |
192.168.31.250 | ||
slave1 |
##3 | 192.168.31.4 | Slave2 |
##192.168.31.225 |
|
4 | ##192.168.31.5 |
monitor | 192.168.31.106 | ||
none | 2. Configure the /etc/hosts file on all hosts and add the following content: 192.168.31.83 master1 Install perl, perl-develperl-CPAN libart_lgpl.x86_64 rrdtool.x86_64 rrdtool-perl.x86_64 package on all hosts Note: Use centos7 online yum source installation Install perl related libraries #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 3. Install mysql5.7 and configure replication on the master1, master2, slave1, and slave2 hosts. master1 and master2 are mutually master and slave, and slave1 and slave2 are the slaves of master1. master1 host: 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
Copy after login
After completing the modification to my.cnf, restart the mysql service through systemctl restart mysqld If you want to enable the firewall on the 4 database hosts, you must either turn off the firewall or create access rules: firewall-cmd --permanent --add-port=3306/tcp mysql> grant replication slave on *.* to rep@'192.168.31.%' identified by '123456';
Copy after login
Copy after login
mysql> grant replication slave on *.* to rep@'192.168.31.%' identified by '123456';
Copy after login
Copy after login
mysql> show master status; +------------------+----------+--------------+------------------+--------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+---------------------------------------------------+ | mysql-bin.000001 | 452 | | | | +------------------+----------+--------------+------------------+-----------------------------------------------------+
Copy after login
Execute on master2, slave1 and 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;
Copy after login
Verify master-slave replication: 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
Copy after login
Copy after login
Copy after login
slave1 host: 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
Copy after login
Copy after login
Copy after login
slave2 host: 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
Copy after login
Copy after login
Copy after login
If Slave_IO_Running and Slave_SQL_Running are both yes, then the master-slave configuration is OK mysql> show master status; +------------------+----------+--------------+------------------+--------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+---------------------------------------------------+ | mysql-bin.000001 | 452 | | | | +------------------+----------+--------------+------------------+----------------------------------------------------+
Copy after login
Execute on master1: 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;
Copy after login
Verify master-slave replication: 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
Copy after login
If Slave_IO_Running and Slave_SQL_Running are both yes, then the master-slave configuration is OK mysql> grant super,replicationclient,process on *.* to 'mmm_agent'@'192.168.31.%' identified by '123456';
Copy after login
mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.31.%' identified by '123456';
Copy after login
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.% | +-------------+------------------------------+
Copy after login
or 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.%' |
Copy after login
Note 2: 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
Copy after login
Copy after login
Install the agent on the database server (master1, master2, slave1, slave2) 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
Copy after login
Copy after login
6. Configure mmm Writing configuration files, the five hosts must be consistent:
Copy after login
hosts master1,master2#能进行写操作的服务器的host名,如果不想切换写操作这里可以只配置master,这样也可以避免因为网络延时而进行write的切换,但是一旦master出现故障那么当前的MMM就没有writer了只有对外的read操作。 #chkconfig --add mysql-mmm-agent #chkconfigmysql-mmm-agent on #/etc/init.d/mysql-mmm-agent start
Copy after login
注:添加source /root/.bash_profile目的是为了mysql-mmm-agent服务能启机自启。 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
Copy after login
解决方法: # 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
Copy after login
Copy after login
Copy after login
描述:检查周期默认为5s
Copy after login
启动报错: 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
Copy after login
解决方法:安装下列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
Copy after login
注1:无论是在db端还是在监控端如果有对配置文件进行修改操作都需要重启代理进程和监控进程。 检查集群状态: [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)
Copy after login
Copy after login
如果服务器状态不是ONLINE,可以用如下命令将服务器上线,例如: #mmm_controlset_online主机名 例如:[root@monitor1 ~]#mmm_controlset_onlinemaster1 [root@master1 ~]# ipaddr show dev eno16777736 eno16777736:
Copy after login
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)
Copy after login
Copy after login
模拟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'
Copy after login
查看群集的最新状态 [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)
Copy after login
从显示结果可以看出master1的状态有ONLINE转换为HARD_OFFLINE,写VIP转移到了master2主机上。 [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
Copy after login
从上面可以看到master1能ping通,说明只是服务死掉了。 查看master2主机的ip地址: [root@master2 ~]# ipaddr show dev eno16777736 eno16777736:
Copy after login
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
Copy after login
Copy after login
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
Copy after login
Copy after login
启动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
Copy after login
从上面可以看到master1的状态由hard_offline改变为awaiting_recovery状态 [root@monitor1 ~]#mmm_controlset_onlinemaster1
Copy after login
[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)
Copy after login
可以看到主库启动不会接管主,只到现有的主再次宕机。 附: 1、日志文件: 4、其它处理问题 If you do not want the writer to switch from master to backup (including the master-slave delay that will also cause the switch of writing VIP), you can remove
5. Summary 1. The virtual IP that provides external reading and writing is controlled by the monitor program. If the monitor is not started, the db server will not be assigned a virtual IP. However, if the virtual IP has been assigned, when the monitor program closes the originally assigned virtual IP, the external program will not be closed immediately and the external program can still be connected and accessed (as long as the network is not restarted). The advantage of this is that the reliability requirements for the monitor will be lower. However, if one of the DB servers fails at this time, it will not be able to handle the switch. That is, the original virtual IP will remain unchanged, and the DB that failed will not be able to handle the switch. The virtual IP will become inaccessible. 2. The agent program is controlled by the monitor program to handle operations such as write switching and slave library switching. If the monitor process is closed, the agent process will not play any role, and it cannot handle faults by itself. 3. The monitor program is responsible for monitoring the status of the db server, including the Mysql database, whether the server is running, whether the replication thread is normal, master-slave delay, etc.; it is also used to control the agent program to handle failures. 4. The monitor will monitor the status of the db server every few seconds. If the db server has changed from fault to normal, the monitor will automatically set it to online status after 60s (the default is 60s) Set to other values), determined by the configuration file parameter "auto_set_online" of the monitoring end. There are three statuses of the cluster server: HARD_OFFLINE→AWAITING_RECOVERY→online |
The above is the detailed content of Detailed explanation of MySQL high availability solution MMM. For more information, please follow other related articles on the PHP Chinese website!