注:原理来源于http://blog.itpub.net/20625855/viewspace-1649629/和百度百科
一:mha是干啥的?
MySQL MHA架构介绍:
MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。
在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。
目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从。(出自:《深入浅出MySQL(第二版)》)
在网上找了一个好看的图解释
MHA工作原理总结为以下几条:
(1)从宕机崩溃的master保存二进制日志事件(binlog events);
(2)识别含有最新更新的slave;
(3)应用差异的中继日志(relay log) 到其他slave;
(4)应用从master保存的二进制日志事件(binlog events);
(5)提升一个slave为新master;
(6)使用其他的slave连接新的master进行复制。
目前高可用方案可以一定程度上实现数据库的高可用,在进行高可用方案选择时,主要是看业务还有对数据一致性方面的要求。最后出于对数据库的高可用和数据一致性的要求,推荐使用MHA架构。
优点:
MHA的一个主要的目标就是实现主从的自动快速切换(10-30s),并且切换过程
不会导致数据不一致问题,同时部署MHA不会导致新增加机器,不需要改变原来的部署结构,不会有性能损失
并且易于安装.
它同时提供了在线切换功能(计划内): 从旧master节点安全的切换到新的master节点,只需要很短的时间
(0.5-2s),并且这个过程中只是阻塞写,不会影响读操作
具体功能点:
1、自动主库监控和failover
MHA具有监控复制环境的中master状态的功能,可以发现master是否不可用,并且完成自动故障切换。在多个
slave情况下,MHA会自动的找到跟最接近主库(the latest)的slave的不同的relay log事件,然后把这些events
应用到其他的slave上,最终所有的slave达到数据一致。 正常情况MHA在数秒内能够完成failover(9-12s确认master
不可用,7-10s(可选择)关闭原master所在机器来防止脑裂, 几秒钟来应用不同的relay log,总时间需要10-30s)
另外,可以通过配置文件指定一个候选master。因为MHA会完成slave之间的一致,所以你可以提升任何一个slave
为新的master,并且不会导致不一致问题
2、交互式master failover
可以用MHA只做failover,而不监控master。 MHA提供交换式的master failover
3、非交互式master failover
非交互的master failover在你已经有监控mysql master软件的情况还是有用的,
比如你可以用(Pacemaker)来监控 master状态和vip接管, 然后使用MHA做failover和slave提升
4、online switchover
数据库升级,服务器硬件升级等场景
有点总结:
1、master failover和 slave promotion操作会非常迅速。
9-12s发现故障,可选择的7-10s的主机power off,数秒的应用差异relay log。从新选举新的master
后,MHA并行恢复剩余的slave,不管你有1台或者10台,对恢复时间几乎没影响,都能很快完成
2、master crash掉不会导致数据不一致
MHA处理slave间差异的中继日志,达到最终一致,结合半同步复制,几乎
可以认为数据不会丢失
3、无需修改当前的mysql配置(MHA支持mysql5.0+)
MHA的启停、升级降级、安装卸载都不需要停数据库,只是replace就ok了
4、不需要增加大量服务器
MHA由MHA Manager和MHA Node组成,MHA Node在mysql服务器上部署运行,
不需要额外的服务;MHA Manager正常情况运行在专用服务器上,但是一个
MHA Manager 可以监控大量(100+)的master。 并且MHA Manager 也可以
运行在slave 上,这样根本不需要增加额外的机器
5、无性能损失
MHA可以在常规的异步复制或半同步复制下工作,它默认3s会向master发送
ping包,没有复杂查询。几乎对性能无影响
6、支持任何存储引擎
slave的中继日志里,master的二进制日志的位置被标记“end_log_pos”,通过对比
slave之间的end_log_pos值来确定哪些中继日志没有被全员应用。MHA内部通过这个
原理来修复slaves之间的一致性,在这个基本原理上,MHA做了一些优化和发展, 比如
MHA结构包括两个部分:
1、MHA Manager:监控master,控制master failover,扩展的script等
2、MHA Node:解析二进制日志和中继日志,确认差异中继日志,应用差异中继日志等
当MHA Manager执行failover,MHA manger 通过ssh连接MHA Node,调用需要的MHA Node
命令做操作
扩展定制
MHA有很多的扩展点:比如使用MHA更新master的IP(更新全局目录库信息,更新vip等)
如何处理IP有用户自己决定,MHA没有明确强制使用什么方式
三、支持的复制架构:
【1】管理节点的部署:
1、专用管理节点服务器,管理多组服务器:MHA Manager只消耗很少的CPU和内存,可以单个MHA
Manager管理上百组服务器
2、部署到一个slave节点上,节省服务器
【3】 master IP的管理:
方法一:使用vip管理软件,当数据库挂掉,vip自动切换到从库
方法二:使用一个全局目录数据库,当主库切换的时候,对保存的
信息做更新
【4】结合半同步复制:
虽然MHA可以从挂掉的master节点获取二进制日志,但是当master节点
无法连通的时候,MHA无法获取这些可能只存在于master上events,这时候
的failover就会造成数据丢失
半同步复制可以大大降低这种风险,半同步复制能够确保至少有一个
slave获得到了最新的二进制日志,这样MHA即使无法登陆master节点也能
获得到几乎所有的events日志,保证数据的不会丢失
masterha_check_ssh 检查MHA的SSH配置状况 masterha_check_repl 检查MySQL复制状况 masterha_manger 启动MHA
masterha_check_status 检测当前MHA运行状态 masterha_master_monitor 检测master是否宕机 masterha_master_switch 控制故障转移(自动或者手动) masterha_conf_host 添加或删除配置的server信息
rhel6.5
manager:172.25.67.1 server1 mha
master:172.25.67.2 server2 mysql1
slave:172.25.67.3 server3 mysql2
哈哈哈哈
1:
数据包
[root@server2 tmp]# ls
mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
2:安装:
[root@server2 tmp]# rpm -ivh *
warning: mysql-community-client-5.7.17-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-common ########################################### [ 20%]
2:mysql-community-libs ########################################### [ 40%]
3:mysql-community-client ########################################### [ 60%]
4:mysql-community-server ########################################### [ 80%]
5:mysql-community-libs-co########################################### [100%]
3:配置主目录/etc/ my.cnf
[root@server2 /]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql # 从给定目录读取数据库文件
socket=/var/lib/mysql/mysql.sock # 为MySQL客户程序与服务器之间的本地通信指定一个套接字文件(Linux下默认是/var/lib/mysql/mysql.sock文件)
server_id=2 #设置 server id
gtid_mode=ON #开启gtid模式
enforce_gtid_consistency=true #强制gtid一致性,开启后对于特定create table不被支持
log-bin=binlog #二进制日志
binlog-do-db=test #同步test库
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
4:开启mysql
[root@server2 mysql]# /etc/init.d/mysqld start
初始化 MySQL 数据库: 2017-10-18T05:44:11.769470Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-10-18T05:44:11.838924Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2017-10-18T05:44:11.838963Z 0 [ERROR] Aborting
[失败
上述开启mysql失败,检查/var/lib/mysql库发现有未知文件删去即可开启
[root@server2 mysql]# ls
ib_buffer_pool
[root@server2 mysql]# rm -fr *
[root@server2 mysql]# /etc/init.d/mysqld start
初始化 MySQL 数据库: [确定]
Installing validate password plugin: [确定]
正在启动 mysqld: [确定]
查看初始化密码
[
root@server2 mysql]# cat /var/log/mysqld.log |grep password
2017-10-18T05:45:35.090024Z 1 [Note] A temporary password is generated for root@localhost: d2v_uQgu5UMa
2017-10-18T05:46:15.805574Z 0 [Note] Execution of init_file '/var/lib/mysql/install-validate-password-plugin.Iaql7S.sql' started.
2017-10-18T05:46:15.880425Z 0 [Note] Execution of init_file '/var/lib/mysql/install-validate-password-plugin.Iaql7S.sql' ended.
2017-10-18T05:46:17.736801Z 0 [Note] Shutting down plugin 'sha256_password'
2017-10-18T05:46:17.736803Z 0 [Note] Shutting down plugin 'mysql_native_password'
2017-10-18T05:46:19.715825Z 3 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
初始化
[root@server2 mysql]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
Sorry, passwords do not match.
New password:
Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
... skipping.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
... skipping.
All done!
上述所有步骤在 server3的master上进行一遍,配置文件相同,只需要改sever-id 和此台服务器不同就可以了
5:配置基于GTID的复制
在主上配置基于GTID的复制
server2:
[root@server2 mysql]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave on *.* to 'backup'@'172.25.67.%' identified by 'ZHUpy.001';
Query OK, 0 rows affected, 1 warning (0.10 sec)
mysql> show master status; #可以看到复制工作已经开始且正常
+---------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+----------------------------------------+
| binlog.000002 | 449 | test | | 8fc98373-b3c7-11e7-9b00-525400f826b5:1 |
+---------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)
server3
使用change master 更新主从配置;
mysql> change master to master_host='172.25.67.2',master_user='backup',master_password='ZHUpy.001',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.20 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.67.2
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 194
Relay_Log_File: server3-relay-bin.000004
Relay_Log_Pos: 401
Relay_Master_Log_File: binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 194
Relay_Log_Space: 1112
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: 8fc98373-b3c7-11e7-9b00-525400f826b5
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 8fc98373-b3c7-11e7-9b00-525400f826b5:1
Executed_Gtid_Set: 8fc98373-b3c7-11e7-9b00-525400f826b5:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
6:在master端创建监控用户
mysql> grant replication slave on *.* to 'backup'@'172.25.67.%' identified by 'ZHUpy.001';
Query OK, 0 rows affected, 1 warning (0.10 sec)
7:设置slave的模式为只读
mysql> set global read_only=1;#注:只读模式建议不要在文件中写入,当切换主机后会出现问题;
Query OK, 0 rows affected (0.00 sec)
好了到此mysql主从复制数据库就配置好了
即在互相连接的时候不需要输入密码
server1[root@server1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
d0:cb:e8:e4:f8:e2:4a:18:1a:62:64:7f:9c:c5:52:59 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| .oE |
| oo |
| o ..o. |
|o . . ++ . |
|+. . +o S |
|++ .= |
|o . . o |
| . .. |
| .o... |
+-----------------+
查看生成文件
[root@server1 /]# ls ~/.ssh/
id_rsa id_rsa.pub known_hosts
注:此处known_hosts储存的是连接过的服务器,现象是在一般ssh的时候,我们第一次要输入yes,之后就不用了,原因就在于此文件
上面的id_rsa.pubid_rsa.pub为公钥,是个文件,将其内容加到其他用户的 ~/.ssh/authorized_keys文件里后,连接此电脑的时候与本地id_rsa配对,即可免密
注意的是在进行三台电脑互相免密通信的时候,authorized_keys为文件,我们应该做的是将id_rsa.pub文件内容追加到authorized_keys文件后,而不是cp
网上好多文章都写错了,一定要注意;
server1
[root@server1 .ssh]# scp ~/.ssh/id_rsa.pub [email protected]:/
[root@server1 .ssh]# scp ~/.ssh/id_rsa.pub [email protected]:/
[root@server2 ~]# cat /id_rsa.pub >>/root/.ssh/authorized_keys
[root@server3 ~]# cat /id_rsa.pub >>/root/.ssh/authorized_keys
server1与2其他人免密就完成拉
server2 server3同样
先生成密钥
然后发送密钥到对方服务器端
最后将密钥追加到对方authorized_keys文件后
1:在所有的节点安装mha node:(server1 server2 server3)
在安装的过程需要依赖
yum install perl-DBD-MySQL -y
[root@server1/2/3 ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]
[root@server2 ~]#
注:第一步所有节点安装
我们可以查看到生成以下文件
[root@server2 ~]# rpm -ql mha4mysql-node-0.56-0.el6.noarch
/usr/bin/apply_diff_relay_logs
/usr/bin/filter_mysqlbinlog
/usr/bin/purge_relay_logs
/usr/bin/save_binary_logs
/usr/share/man/man1/apply_diff_relay_logs.1.gz
/usr/share/man/man1/filter_mysqlbinlog.1.gz
/usr/share/man/man1/purge_relay_logs.1.gz
/usr/share/man/man1/save_binary_logs.1.gz
/usr/share/perl5/vendor_perl/MHA/BinlogHeaderParser.pm
/usr/share/perl5/vendor_perl/MHA/BinlogManager.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFindManager.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinder.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinderElp.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinderXid.pm
/usr/share/perl5/vendor_perl/MHA/NodeConst.pm
/usr/share/perl5/vendor_perl/MHA/NodeUtil.pm
/usr/share/perl5/vendor_perl/MHA/SlaveUtil.pm
MHA Manager中主要包括了几个管理员的命令行工具,例如master_manger,master_master_switch等。MHA Manger也依赖于perl模块,具体如下:
perl-Log-Dispatch-2.27-1.el6.noarch.rpm perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
同样
[root@server1 mnt]# rpm -ivh perl-*
warning: perl-Config-Tiny-2.12-7.1.el6.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
warning: perl-Log-Dispatch-2.27-1.el6.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
package perl-MIME-Types-1.28-2.el6.noarch is already installed
package perl-Mail-Sendmail-0.79-12.el6.noarch is already installed
package perl-Mail-Sender-0.8.16-3.el6.noarch is already installed
package perl-Email-Date-Format-1.002-5.el6.noarch is already installed
package perl-MIME-Lite-3.027-2.el6.noarch is already installed
package perl-Log-Dispatch-2.27-1.el6.noarch is already installed
package perl-Parallel-ForkManager-0.7.9-1.el6.noarch is already installed
package perl-Config-Tiny-2.12-7.1.el6.noarch is already installed
我这里是已经安装了所以这样显示
下来 下来 安装最最最重要的东西拉
[root@server1 mnt]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
[root@server1 mnt]# rpm -ql mha4mysql-manager-0.56-0.el6.noarch
/usr/bin/masterha_check_repl
/usr/bin/masterha_check_ssh
/usr/bin/masterha_check_status
/usr/bin/masterha_conf_host
/usr/bin/masterha_manager
/usr/bin/masterha_master_monitor
/usr/bin/masterha_master_switch
/usr/bin/masterha_secondary_check
/usr/bin/masterha_stop
/usr/share/man/man1/masterha_check_repl.1.gz
/usr/share/man/man1/masterha_check_ssh.1.gz
/usr/share/man/man1/masterha_check_status.1.gz
/usr/share/man/man1/masterha_conf_host.1.gz
/usr/share/man/man1/masterha_manager.1.gz
/usr/share/man/man1/masterha_master_monitor.1.gz
/usr/share/man/man1/masterha_master_switch.1.gz
/usr/share/man/man1/masterha_secondary_check.1.gz
/usr/share/man/man1/masterha_stop.1.gz
/usr/share/perl5/vendor_perl/MHA/Config.pm
/usr/share/perl5/vendor_perl/MHA/DBHelper.pm
/usr/share/perl5/vendor_perl/MHA/FileStatus.pm
/usr/share/perl5/vendor_perl/MHA/HealthCheck.pm
/usr/share/perl5/vendor_perl/MHA/ManagerAdmin.pm
/usr/share/perl5/vendor_perl/MHA/ManagerAdminWrapper.pm
/usr/share/perl5/vendor_perl/MHA/ManagerConst.pm
/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm
/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm
/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm
/usr/share/perl5/vendor_perl/MHA/MasterRotate.pm
/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm
/usr/share/perl5/vendor_perl/MHA/Server.pm
/usr/share/perl5/vendor_perl/MHA/ServerManager.pm
可以看到安装manager管理节点后会生成这些文件
首先说一下,mha和其他软件不一样,他在主配置文件是靠–conf调用的,在/etc/masterha_default.cnf.为其global
配置文件,因为mha管理集群是可以管理多个集群组的,所以在这里面定义的为全局变量。当然也可以写在自己定义的配置文件中;
第二,mha和mysql一样,主配置文件是通过调用一些东西实现的比如bin-log等
好了,我开始配置文件了
先建立一些我这个集群的文件目录
mkdir -p /mha/log ---这里是存放日志的文件夹路径
mkdir -p /etc/mha ---这里是存放配置文件的路径
touch /etc/mha/mha.conf
[root@server1 mnt]# vim /etc/mha/mha.conf
[server default]
manager_log=/mha/log/mha.log //设置manager的日志
manager_workdir=/mha/ //设置manager的工作目录
master_binlog_dir=/var/lib/mysql/ //设置master 保存binlog的位置,以便MHA可以找到master的日志,我这里的也就是mysql的数据目录
master_ip_failover_script=/usr/local/bin/master_ip_failover //设置自动failover时候的切换脚本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change_script //设置手动切换时候的切换脚本
user=mha //设置监控用户
password='ZHUpy.001' //设置mysql中mha用户的密码,这个密码是前文中创建监控用户的那个密码
ping_interval=1 //设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行railover
remote_workdir=/var/tmp //设置远端mysql在发生切换时binlog的保存位置
repl_password='ZHUpy.001' //设置复制用户的密码
repl_user=backup //设置复制环境中的复制用户名
ssh_user=root //设置ssh的登录用户名
[server1]
candidate_master=1
hostname=172.25.67.2
master_binlog_dir=/var/lib/mysql/
[server2]
candidate_master=1 #设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中事件的slave
hostname=172.25.67.3
master_binlog_dir=/var/lib/mysql/
MHA在发生切换的过程中,从库的恢复过程中依赖于relay log的相关信息,所以这里要将relay log的自动清除设置为OFF,采用手动清除relay log的方式。在默认情况下,从服务器上的中继日志会在SQL线程执行完毕后被自动删除。但是在MHA环境中,这些中继日志在恢复其他从服务器时可能会被用到,因此需要禁用中继日志的自动删除功能。定期清除中继日志需要考虑到复制延时的问题。在ext3的文件系统下,删除大的文件需要一定的时间,会导致严重的复制延时。为了避免复制延时,需要暂时为中继日志创建硬链接,因为在Linux系统中通过硬链接删除大文件速度会很快。(在mysql数据库中,删除大表时,通常也采用建立硬链接的方式)
1:检查ssh
[root@server1 ~]# masterha_check_ssh --conf=/etc/mha/mha.conf
Wed Oct 18 20:27:45 2017 - [info] Reading default configuration from /etc/masterha_default.cnf..
Wed Oct 18 20:27:45 2017 - [info] Reading application default configuration from /etc/mha/mha.conf..
Wed Oct 18 20:27:45 2017 - [info] Reading server configuration from /etc/mha/mha.conf..
Wed Oct 18 20:27:45 2017 - [info] Starting SSH connection tests..
Wed Oct 18 20:27:45 2017 - [debug]
Wed Oct 18 20:27:45 2017 - [info] All SSH connection tests passed successfully.
[root@server1 ~]#
[root@server1 ~]# masterha_check_repl --conf=/etc/mha/mha.conf ##查看集群状态,即mysql是否正常
[root@server1 ~]# nohup masterha_manager --conf=/etc/mha/mha.conf --remove_dead_master_conf --ignore_last_failover < /dev/null > /mha/log/mha.log 2>&1 &
[3] 3918
[root@server1 ~]# masterha_check_status --conf=/etc/mha/mha.conf ##查看监控的状态
mha (pid:1745) is running(0:PING_OK), master:172.25.67.2
启动参数介绍:
–remove_dead_master_conf 该参数代表当发生主从切换后,老的主库的ip将会从配置文件中移除。
–manger_log 日志存放位置
–ignore_last_failover 在缺省情况下,如果MHA检测到连续发生宕机,且两次宕机间隔不足8小时的话,则不会进行Failover,之所以这样限制是为了避免ping-pong效应。该参数代表忽略上次MHA触发切换产生的文件,默认情况下,MHA发生切换后会在日志目录,也就是上面我设置的/data产生app1.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,这里设置为–ignore_last_failover。
关闭监控:
[root@server1 ~]# masterha_stop --conf=/etc/mha/mha.conf
Stopped mha successfully.
可关可不关,我们下来配置keepalived使他提供vip
1:安装(server2/server3都装)
[root@server2 /]# tar zxf keepalived-1.2.20.tar.gz
[root@server2 /]# cd keepalived-1.2.20
[root@server2 keepalived-1.2.20]# ls
AUTHOR ChangeLog config.status configure.ac COPYING genhash install-sh keepalived.spec lib Makefile.in TODO
bin config.log configure CONTRIBUTORS doc INSTALL keepalived keepalived.spec.in Makefile README VERSION
[root@server2 keepalived-1.2.20]# ./configure --prefix=/usr/local/keepalived/
[root@server2 keepalived-1.2.20]# make
[root@server2 keepalived-1.2.20]# make install
2:将源码安装生成的安装包放到指定位置(server2/server3都执行)
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
cp /usr/local/keepalived/sbin/ /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
3:配置vip(server2的virtual_router_id比server3高,其他配置相同)
[root@server2 /]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 150 #注:在slave端要低于此数
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.67.100/24
}
}
注:两台服务器的状态都是backup 因为backup模式下当主机宕机恢复后不会抢占资源节点,而master状态下只要宕机后恢复会抢回来vip
4:启动(server2/server3都执行)
[root@server2 keepalived-1.2.20]# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@server2 keepalived-1.2.20]# ip a
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:f8:26:b5 brd ff:ff:ff:ff:ff:ff
inet 172.25.67.2/24 brd 172.25.67.255 scope global eth0
inet 172.25.67.100/32 scope global eth0
inet6 fe80::5054:ff:fef8:26b5/64 scope link
valid_lft forever preferred_lft forever
在/etc/mha/mha.conf 文件中我门有以下两行:
master_ip_failover_script=/usr/local/bin/master_ip_failover //设置自动failover时候的切换脚本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change_script //设置手动切换时候的切换脚本
此处即为宕机时通过vip控制的脚本
此脚本来自网上:我把他直接 复制到这里,需要主要的是更改里面的vip以及控制keepalived的开启或者关闭命令
[root@server1 ~]# cat /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = '172.25.67.100/24';
#my $key = '1';
my $ssh_start_vip = "/etc/init.d/keepalived start";
my $ssh_stop_vip = "/etc/init.d/keepalived stop";
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
[root@server1 ~]# cat /usr/local/bin/master_ip_online_change_script
#!/usr/bin/env perl
use strict;
use warnings FATAL =>'all';
use Getopt::Long;
my $vip = '172.25.67.100/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $exit_code = 0;
my (
$command, $orig_master_is_new_slave, $orig_master_host,
$orig_master_ip, $orig_master_port, $orig_master_user,
$orig_master_password, $orig_master_ssh_user, $new_master_host,
$new_master_ip, $new_master_port, $new_master_user,
$new_master_password, $new_master_ssh_user,
);
GetOptions(
'command=s' => \$command,
'orig_master_is_new_slave' => \$orig_master_is_new_slave,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'orig_master_user=s' => \$orig_master_user,
'orig_master_password=s' => \$orig_master_password,
'orig_master_ssh_user=s' => \$orig_master_ssh_user,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
'new_master_user=s' => \$new_master_user,
'new_master_password=s' => \$new_master_password,
'new_master_ssh_user=s' => \$new_master_ssh_user,
);
exit &main();
sub main {
#print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
my $exit_code = 1;
eval {
print "\n\n\n***************************************************************\n";
print "Disabling the VIP - $vip on old master: $orig_master_host\n";
print "***************************************************************\n\n\n\n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
print "\n\n\n***************************************************************\n";
print "Enabling the VIP - $vip on new master: $new_master_host \n";
print "***************************************************************\n\n\n\n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
`ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
好了我们所有的配置就到这里了,我们现在重新打开监控和mysql进行测试;
[root@server1 ~]# nohup masterha_manager --conf=/etc/mha/mha.conf --remove_dead_master_conf --ignore_last_failover < /dev/null > /mha/log/mha.log 2>&1 &
[1] 3982
[root@server2 /]# /etc/init.d/mysqld stop
[root@server3 keepalived-1.2.20]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show slave status;
Empty set (0.00 sec)
mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+------------------------------------------+
| binlog.000002 | 154 | test | | 8fc98373-b3c7-11e7-9b00-525400f826b5:1-3 |
+---------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
由上可以查看到server3已经成了master
问题:现在我还不太明白的是当主备切换后,当时授权复制用户是在master端授权的所以如果切换后怎样再进行复制,后续处理;;;