很早之前写过MHA的文章,但是常常在技术群看到有同学问MHA搭建的问题,不是权限问题就是配置问题,我在这里就再次一写下配置过程以及快速的搭建。如果想知道更多的细节与原理,请参考:MySQL高可用架构之MHA

环境:

1主1从,manager放在从库。

主库:192.168.0.10

从库:192.168.0.20

两台机器的mysql安装完成初始化以后进行复制搭建,首先登录主库(192.168.0.10),查看pos点:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

然后在主库(192.168.0.10)
添加复制账号以及mha用的账号

mysql> grant replication slave on *.* to 'repl'@'192.168.0.10'  identified by 'repl';
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant replication slave on *.* to 'repl'@'192.168.0.20' identified by 'repl';
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all on *.* to 'root'@'192.168.0.20' identified by '';
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all on *.* to 'root'@'192.168.0.10' identified by '';
Query OK, 0 rows affected, 1 warning (0.01 sec) mysql>

从库(192.168.0.10 )change到mysql-bin.000001,pos点154

 CHANGE MASTER TO  MASTER_HOST='192.168.0.10',MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154; 
mysql> start slave;
Query OK, 0 rows affected (0.15 sec) mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1344
Relay_Log_File: relaylog.000002
Relay_Log_Pos: 1510
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:

复制到这来就搭建完成了,然后配置192.168.0.10和192.168.0.20 ssh互信。(两台机器都执行)

ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub '-p 22 192.168.0.10'
ssh-copy-id -i /root/.ssh/id_rsa.pub '-p 22 192.168.0.20'

安装MHA软件,首先安装epel源。(2台机器)

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

安装依赖包

yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y

安装MHA软件(两台机器)

tar xf mha4mysql-node-0.56.tar.gz
cd mha4mysql-node-0.56
perl Makefile.PL
make && make install tar xf mha4mysql-manager-0.56.tar.gz
cd mha4mysql-manager-0.56
perl Makefile.PL
make && make install

在从库(192.168.0.20)创建目录:

mkdir /data/mha//log
cd /data/mha//touch mha.cnf

mha.cnf配置文件内容如下:

[server default]
client_bindir=/usr/local/mysql/bin/
manager_log=/data/mha//log/manager.log
manager_workdir=/data/mha//log
master_binlog_dir=/data/mysql//binlog/
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script= /usr/local/bin/master_ip_online_change
report_script=/usr/local/bin/send_report
init_conf_load_script=/usr/local/bin/load_cnf
remote_workdir=/data/mysql//mysqltmp
#secondary_check_script= /usr/local/bin/masterha_secondary_check -s 192.168.0.30 -s 192.168.0.40
user=root
ping_interval=
repl_user=repl
ssh_port=
ssh_user=root
max_ping_errors= [server1]
hostname=192.168.0.10
port= [server2]
candidate_master=
check_repl_delay=
hostname=192.168.0.20
port=

编辑文件 /usr/local/bin/load_cnf 里面的密码修改成对应的密码

#!/usr/bin/perl

  print "password=123\n";
print "repl_password=repl\n";

执行chek命令查看复制是否正常:

[root@dbserver-yayun- ]# masterha_check_repl --conf=/data/mha//mha.cnf
Mon Mar :: - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Mar :: - [info] Reading application default configuration from /data/mha//mha.cnf..
Mon Mar :: - [info] Updating application default configuration from /usr/local/bin/load_cnf..
Mon Mar :: - [info] Reading server configuration from /data/mha//mha.cnf..
Mon Mar :: - [info] Setting max_ping_errors to , ping_interval to .
Mon Mar :: - [info] MHA::MasterMonitor version 0.56.
Mon Mar :: - [info] GTID failover mode =
Mon Mar :: - [info] Dead Servers:
Mon Mar :: - [info] Alive Servers:
Mon Mar :: - [info] 192.168.0.10(192.168.0.10:)
Mon Mar :: - [info] 192.168.0.20(192.168.0.20:)
Mon Mar :: - [info] Alive Slaves:
Mon Mar :: - [info] 192.168.0.20(192.168.0.20:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Mon Mar :: - [info] Replicating from 192.168.0.10(192.168.0.10:)
Mon Mar :: - [info] Primary candidate for the new Master (candidate_master is set)
Mon Mar :: - [info] Current Alive Master: 192.168.0.10(192.168.0.10:)
Mon Mar :: - [info] Checking slave configurations..
Mon Mar :: - [info] read_only= is not set on slave 192.168.0.20(192.168.0.20:).
Mon Mar :: - [warning] relay_log_purge= is not set on slave 192.168.0.20(192.168.0.20:).
Mon Mar :: - [info] Checking replication filtering settings..
Mon Mar :: - [info] binlog_do_db= , binlog_ignore_db=
Mon Mar :: - [info] Replication filtering check ok.
Mon Mar :: - [info] GTID (with auto-pos) is not supported
Mon Mar :: - [info] Starting SSH connection tests..
Mon Mar :: - [info] All SSH connection tests passed successfully.
Mon Mar :: - [info] Checking MHA Node version..
Mon Mar :: - [info] Version check ok.
Mon Mar :: - [info] Checking SSH publickey authentication settings on the current master..
Mon Mar :: - [info] HealthCheck: SSH to 192.168.0.10 is reachable.
Mon Mar :: - [info] Master MHA Node version is 0.56.
Mon Mar :: - [info] Checking recovery script configurations on 192.168.0.10(192.168.0.10:)..
Mon Mar :: - [info] Executing command: save_binary_logs --command=test --start_pos= --binlog_dir=/data/mysql//binlog/ --output_file=/data/mysql//mysqltmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.
Mon Mar :: - [info] Connecting to root@192.168.0.10(192.168.0.10:)..
Creating /data/mysql//mysqltmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /data/mysql//binlog/, up to mysql-bin.
Mon Mar :: - [info] Binlog setting check done.
Mon Mar :: - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Mon Mar :: - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=192.168.0.20 --slave_ip=192.168.0.20 --slave_port= --workdir=/data/mysql//mysqltmp --target_version=5.7.-log --manager_version=0.56 --client_bindir=/usr/local/mysql/bin/ --relay_dir=/data/mysql//relaylog --current_relay_log=relaylog. --slave_pass=xxx
Mon Mar :: - [info] Connecting to root@192.168.0.20(192.168.0.20:)..
Checking slave recovery environment settings..
Relay log found at /data/mysql//relaylog, up to relaylog.
Temporary relay log file is /data/mysql//relaylog/relaylog.
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Mon Mar :: - [info] Slaves settings check done.
Mon Mar :: - [info]
192.168.0.10(192.168.0.10:) (current master)
+--192.168.0.20(192.168.0.20:) Mon Mar :: - [info] Checking replication health on 192.168.0.20..
Mon Mar :: - [info] ok.
Mon Mar :: - [info] Checking master_ip_failover_script status:
Mon Mar :: - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.0.10 --orig_master_ip=192.168.0.10 --orig_master_port= IN SCRIPT TEST====/sbin/ifconfig eth1: down==/sbin/ifconfig eth1: 192.168.0.88/=== Checking the Status of the script.. OK
Mon Mar :: - [info] OK.
Mon Mar :: - [warning] shutdown_script is not defined.
Mon Mar :: - [info] Got exit code (Not master dead). MySQL Replication Health is OK.

主库(192.168.0.10)执行命令,启动vip:

/sbin/ifconfig eth1: 192.168.0.88/

在线切换,把主库切到192.168.0.20

masterha_master_switch --master_state=alive --conf=/data/mha//mha.cnf --new_master_host=192.168.0.20 --new_master_port= --orig_master_is_new_slave

输出如下:

Mon Mar  ::  - [info] MHA::MasterRotate version 0.56.
Mon Mar :: - [info] Starting online master switch..
Mon Mar :: - [info]
Mon Mar :: - [info] * Phase : Configuration Check Phase..
Mon Mar :: - [info]
Mon Mar :: - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Mar :: - [info] Reading application default configuration from /data/mha//mha.cnf..
Mon Mar :: - [info] Updating application default configuration from /usr/local/bin/load_cnf..
Mon Mar :: - [info] Reading server configuration from /data/mha//mha.cnf..
Mon Mar :: - [info] Setting max_ping_errors to , ping_interval to .
Mon Mar :: - [info] GTID failover mode =
Mon Mar :: - [info] Current Alive Master: 192.168.0.10(192.168.0.10:)
Mon Mar :: - [info] Alive Slaves:
Mon Mar :: - [info] 192.168.0.20(192.168.0.20:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Mon Mar :: - [info] Replicating from 192.168.0.10(192.168.0.10:)
Mon Mar :: - [info] Primary candidate for the new Master (candidate_master is set) It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.0.10(192.168.0.10:)? (YES/no): yes
Mon Mar :: - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Mon Mar :: - [info] ok.
Mon Mar :: - [info] Checking MHA is not monitoring or doing failover..
Mon Mar :: - [info] Checking replication health on 192.168.0.20..
Mon Mar :: - [info] ok.
Mon Mar :: - [info] 192.168.0.20 can be new master.
Mon Mar :: - [info]
From:
192.168.0.10(192.168.0.10:) (current master)
+--192.168.0.20(192.168.0.20:) To:
192.168.0.20(192.168.0.20:) (new master)
+--192.168.0.10(192.168.0.10:) Starting master switch from 192.168.0.10(192.168.0.10:) to 192.168.0.20(192.168.0.20:)? (yes/NO): yes
Mon Mar :: - [info] Checking whether 192.168.0.20(192.168.0.20:) is ok for the new master..
Mon Mar :: - [info] ok.
Mon Mar :: - [info] 192.168.0.10(192.168.0.10:): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Mon Mar :: - [info] 192.168.0.10(192.168.0.10:): Resetting slave pointing to the dummy host.
Mon Mar :: - [info] ** Phase : Configuration Check Phase completed.
Mon Mar :: - [info]
Mon Mar :: - [info] * Phase : Rejecting updates Phase..
Mon Mar :: - [info]
Mon Mar :: - [info] Executing master ip online change script to disable write on the current master:
Mon Mar :: - [info] /usr/local/bin/master_ip_online_change --command=stop --orig_master_host=192.168.0.10 --orig_master_ip=192.168.0.10 --orig_master_port= --orig_master_user='root' --orig_master_password='' --new_master_host=192.168.0.20 --new_master_ip=192.168.0.20 --new_master_port= --new_master_user='root' --new_master_password='' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave
Mon Mar :: Set read_only on the new master.. ok.
Mon Mar :: Drpping app user on the orig master..
Mon Mar :: Set read_only= on the orig master.. ok.
Mon Mar :: Killing all application threads..
Mon Mar :: done.
Mon Mar :: - [info] ok.
Mon Mar :: - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Mon Mar :: - [info] Executing FLUSH TABLES WITH READ LOCK..
Mon Mar :: - [info] ok.
Mon Mar :: - [info] Orig master binlog:pos is mysql-bin.:.
Mon Mar :: - [info] Waiting to execute all relay logs on 192.168.0.20(192.168.0.20:)..
Mon Mar :: - [info] master_pos_wait(mysql-bin.:) completed on 192.168.0.20(192.168.0.20:). Executed events.
Mon Mar :: - [info] done.
Mon Mar :: - [info] Getting new master's binlog name and position..
Mon Mar :: - [info] mysql-bin.:
Mon Mar :: - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.0.20', MASTER_PORT=, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=, MASTER_USER='repl', MASTER_PASSWORD='xxx';
Mon Mar :: - [info] Executing master ip online change script to allow write on the new master:
Mon Mar :: - [info] /usr/local/bin/master_ip_online_change --command=start --orig_master_host=192.168.0.10 --orig_master_ip=192.168.0.10 --orig_master_port= --orig_master_user='root' --orig_master_password='' --new_master_host=192.168.0.20 --new_master_ip=192.168.0.20 --new_master_port= --new_master_user='root' --new_master_password='' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave
Mon Mar :: Set read_only= on the new master.
Mon Mar :: Creating app user on the new master..
Mon Mar :: - [info] ok.
Mon Mar :: - [info]
Mon Mar :: - [info] * Switching slaves in parallel..
Mon Mar :: - [info]
Mon Mar :: - [info] Unlocking all tables on the orig master:
Mon Mar :: - [info] Executing UNLOCK TABLES..
Mon Mar :: - [info] ok.
Mon Mar :: - [info] Starting orig master as a new slave..
Mon Mar :: - [info] Resetting slave 192.168.0.10(192.168.0.10:) and starting replication from the new master 192.168.0.20(192.168.0.20:)..
Mon Mar :: - [info] Executed CHANGE MASTER.
Mon Mar :: - [info] Slave started.
Mon Mar :: - [info] All new slave servers switched successfully.
Mon Mar :: - [info]
Mon Mar :: - [info] * Phase : New master cleanup phase..
Mon Mar :: - [info]
Mon Mar :: - [info] 192.168.0.20: Resetting slave info succeeded.
Mon Mar :: - [info] Switching master to 192.168.0.20(192.168.0.20:) completed successfully.

关于配置文件中的参数:
max_ping_errors=40,这个是修改了源码,增加了检测次数的定义,默认是3次,太容易误切换。

启动管理进程:

/usr/bin/nohup /usr/local/bin/masterha_manager --conf=/data/mha//mha.cnf --ignore_last_failover > /data/mha//log/manager.log >& &

在主库(192.168.0.10)封掉ip,可以看到日志输出。

iptables -I INPUT -s 192.168.0.20 -j DROP
Mon Mar  ::  - [warning] Got error on MySQL connect:  (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))
Mon Mar :: - [warning] Connection failed time(s)..
Mon Mar :: - [warning] Got error on MySQL connect: (Can't connect to MySQL server on '192.168.0.10' (4))

修改以后的软件包下载地址:链接:http://pan.baidu.com/s/1c3qg70 密码:f9u2

MHA快速搭建的更多相关文章

  1. MySQL高可用之MHA的搭建 转

     http://www.cnblogs.com/muhu/p/4045780.html http://www.cnblogs.com/gomysql/p/3675429.html http://www ...

  2. MySQL高可用之MHA的搭建

    MySQL MHA架构介绍: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Face ...

  3. Nginx学习笔记--001-Nginx快速搭建

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...

  4. Github pages + jekyll 博客快速搭建

    Github pages + jekyll 博客快速搭建 寻找喜欢的模版 https://github.com/jekyll/jekyll/wiki/sites http://jekyllthemes ...

  5. NodeJS 最快速搭建一个HttpServer

    最快速搭建一个HttpServer 在目录里放一个index.html cd D:\Web\InternalWeb start http-server -i -p 8081

  6. 利用yeoman快速搭建React+webpack+es6脚手架

    自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...

  7. 基于Docker快速搭建多节点Hadoop集群--已验证

    Docker最核心的特性之一,就是能够将任何应用包括Hadoop打包到Docker镜像中.这篇教程介绍了利用Docker在单机上快速搭建多节点 Hadoop集群的详细步骤.作者在发现目前的Hadoop ...

  8. 基于 Jenkins 快速搭建持续集成环境

      什么是持续集成 随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.尤其是近些年来,敏捷(Agile) 在软件工程领域越来越红火 ...

  9. bootstrap快速搭建属于自己的后台模板库

    不论做什么项目,我们都以快速搭建为主,设计师固然重要,但是,我们前端开发的也必须能给出自己以前做过什么样的模板,自己收藏的模板,或者我们弹框的形式,我的提示框的形式,我用的下拉框的插件,日历的插件,我 ...

随机推荐

  1. 用js如何获取file是否存在

    其实注意点就可以知道了. 举个例子 firebug看出这代码: <div id="SWFUpload_0_0" class="uploadify-queue-ite ...

  2. 计算n的阶乘有多少个尾随零

    思路一: 计算出n!= nValue,然后 nValue % 10 == 0 则nCount自增1,nValue /= 10 直到条件为否,最后nCount就是我们想要的结果,代码如下: int Co ...

  3. 痞子衡嵌入式:开源软件协议(MIT/BSD/Apache/LGPL/MPL/GPL)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是关于开源软件协议基本知识. 牛顿曾说过:"如果我比别人看得更远,那是因为我站在巨人的肩上".在软件开发中如果说也存在巨 ...

  4. 从2PC到Paxos

    在分布式系统中,一个事务可能涉及到集群中的多个节点.单个节点很容易知道自己执行的事务成功还是失败,但因为网络不可靠难以了解其它节点的执行状态(可能事务执行成功但网络访问超时). 若部分节点事务执行失败 ...

  5. linux 常用命令集合-命令导图

    这几天画了几张导图,自己熟悉命令,并记录总结一下,还有很多没写上去,在慢慢完善把. 1.帮助命令 2.文件搜索命令 3.用户管理 4.权限管理 5.文件处理类 6.压缩解压 7.网络配置类 8.关机重 ...

  6. WPF Grid布局

    本节讲述布局,顺带加点样式给大家看看~单纯学布局,肯定是枯燥的~哈哈 那如上界面,该如何设计呢? 1.一些布局元素经常用到.Grid StackPanel Canvas WrapPanel等.如上这种 ...

  7. vue IE 报错 引用babel-polyfill

    一.vue 项目报错 vuex requires a Promise polyfill in this browser     在网上找到下面三篇文章,然而和我的项目都不太一样. 我的项目基于 基础模 ...

  8. C#一个窗体调用另一个窗体的方法

    一个窗体调用另一个窗体的方法:例如:窗体B要调用窗体A中的方法1.首先在窗体A中将窗体A设为静态窗体public static  FormA   m_formA; //设此窗体为静态,其他窗体可调用此 ...

  9. [android] 帧布局

    /*******************2016年5月3日 更新**************************************/ 知乎:如何理解andriod中的View和framela ...

  10. 在CentOS下面安装hue时报的错

    说明:我的系统为CentOS 7 ,系统自带的python版本为2.7.5. 安装hue时,推荐使用2.7.0以上的版本,可以自己查看自己系统自带的版本 若是版本不对,要升级为2.7的版本,这里不再说 ...