共计 5244 个字符,预计需要花费 14 分钟才能阅读完成。
这篇文章主要介绍“怎么搭建 MHA+MySQL”,在日常操作中,相信很多人在怎么搭建 MHA+MySQL 问题上存在疑惑,丸趣 TV 小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么搭建 MHA+MySQL”的疑惑有所帮助!接下来,请跟着丸趣 TV 小编一起来学习吧!
一主两从,manager 节点为 vm3
master:10.0.0.2 vm1 node
slave1: 10.0.0.3 vm2 node
slave2: 10.0.0.4 vm3 manager
OS verision 均为:CentOS release 6.5
1,从 https://code.google.com/p/mysql-master-ha/wiki/Downloads?tm= 2 下载 MHA 安装包
mha4mysql-node-0.56-0.el6.noarch.rpm
mha4mysql-manager-0.56-0.el6.noarch.rpm
node 需要安装 mha4mysql-node-0.56-0.el6.noarch.rpm 包,
manager 节点需要安装 mha4mysql-manager-0.56-0.el6.noarch.rpm 和 mha4mysql-node-0.56-0.el6.noarch.rpm
或者从 https://github.com/yoshinorim 下载源码包
2 给所有节点配置 EPEL yum 源。
rpm -ivh http://mirrors.opencas.cn/epel/6/i386/epel-release-6-8.noarch.rpm
yum clean all; yum makecache
3 为所有节点安装 mha4mysql-node-0.56-0.el6.noarch.rpm
yum -y install perl-DBD-MySQL ncftp
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
4 为 manager 节点安装 mha4mysql-manager-0.56-0.el6.noarch.rpm
yum -y install perl-Config-Tiny perl-Params-Validate perl-Log-Dispatch perl-Parallel-ForkManager
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
5 配置各节点间的 ssh 公钥信任,实现免密钥登录。(manager to node, node to node)
vm1:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.3
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.4
vm2:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.2
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.4
vm3:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.2
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.3
分别从各个节点 ssh 登陆其他节点。(必须做,头一次需要输入 yes 确认)
6 在各 mysql 节点创建 MHA 的监控用户
grant all privileges on *.* to mha_monitor @ 10.0.0.% identified by 123456
mysql select user,host from user;
+————-+———–+
| user | host |
+————-+———–+
| mha_monitor | 10.0.0.% |
| repl | 10.0.0.% |
7 配置 mha conf 文件
mkdir /etc/masterha
mkdir -p /masterha/app1
配置文件 app1.cnf 内容如下
cat /etc/masterha/app1.cnf
[server default]
manager_workdir=/masterha/app1
manager_log=/masterha/app1/manager.log
ssh_user=root ##OS ssh 登陆用户
user=mha_monitor ##mha 登陆 mysql 主从的用户
password=123456 ##mha 登陆 mysql 主从的用户密码
repl_user=repl ## 主从复制用户
repl_password=p4ssw0rd ## 主从复制用户的密码
ping_interval=1
shutdown_script=
#master_ip_failover_script= /usr/local/bin/master_ip_failover
master_ip_online_change_script=
report_script=
[server1]
hostname=10.0.0.2
master_binlog_dir= /data/mysql3306
port=3306
candidate_master=1
[server2]
hostname=10.0.0.3
master_binlog_dir= /data/mysql3306
port=3306
candidate_master=1
[server3]
hostname=10.0.0.4
master_binlog_dir= /data/mysql3306
port=3306
no_master=1
8 启动
nohup masterha_manager –conf=/etc/masterha/app1.cnf /tmp/mha_manager.log /dev/null 2 1
shutdown 掉 master 后,可以顺利切换。
但是 masterha_check_ssh 会报错,只是不影响功能:
[root@vm3 ~]# masterha_check_ssh –conf=/etc/masterha/app1.cnf
Wed Apr 6 15:56:05 2016 – [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Apr 6 15:56:05 2016 – [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Apr 6 15:56:05 2016 – [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Apr 6 15:56:05 2016 – [info] Starting SSH connection tests..
Wed Apr 6 15:56:05 2016 – [debug]
Wed Apr 6 15:56:05 2016 – [debug] Connecting via SSH from root@vm1(10.0.0.2:22) to root@vm2(10.0.0.3:22)..
Wed Apr 6 15:56:05 2016 – [debug] ok.
Wed Apr 6 15:56:05 2016 – [debug] Connecting via SSH from root@vm1(10.0.0.2:22) to root@10.0.0.4(10.0.0.4:22)..
Wed Apr 6 15:56:05 2016 – [debug] ok.
Wed Apr 6 15:56:06 2016 – [debug]
Wed Apr 6 15:56:05 2016 – [debug] Connecting via SSH from root@vm2(10.0.0.3:22) to root@vm1(10.0.0.2:22)..
Wed Apr 6 15:56:06 2016 – [debug] ok.
Wed Apr 6 15:56:06 2016 – [debug] Connecting via SSH from root@vm2(10.0.0.3:22) to root@10.0.0.4(10.0.0.4:22)..
Wed Apr 6 15:56:06 2016 – [debug] ok.
Wed Apr 6 15:56:06 2016 – [error][/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm, ln63]
Wed Apr 6 15:56:06 2016 – [debug] Connecting via SSH from root@10.0.0.4(10.0.0.4:22) to root@vm1(10.0.0.2:22)..
Warning: Permanently added 10.0.0.4 (RSA) to the list of known hosts.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
Wed Apr 6 15:56:06 2016 – [error][/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm, ln111] SSH connection from root@10.0.0.4(10.0.0.4:22) to root@vm1(10.0.0.2:22) failed!
SSH Configuration Check Failed!
at /usr/bin/masterha_check_ssh line 44 中间
9 最后 shutdown 掉 master,模拟 failover:
—– Failover Report —–
app1: MySQL Master failover vm1(10.0.0.2:3306) to vm2(10.0.0.3:3306) succeeded
Master vm1(10.0.0.2:3306) is down!
Check MHA Manager logs at vm3:/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
Selected vm2(10.0.0.3:3306) as a new master.
vm2(10.0.0.3:3306): OK: Applying all logs succeeded.
10.0.0.4(10.0.0.4:3306): OK: Slave started, replicating from vm2(10.0.0.3:3306)
vm2(10.0.0.3:3306): Resetting slave info succeeded.
主从切换成功。
##########################################
##########################################
关于 masterha_check_ssh 报错的问题,MHA 官网上发现类似问题的帖子有人给的解释如下:
#3 dba.abde…@gmail.com As mentioned here in the tutorial:
https://code.google.com/p/mysql-master-ha/wiki/Tutorial
You shouldn t add an entry for the manager host. If you have 4 hosts, 1,
2, 3 and 4 (manager), you should add host entries for the first 3 ones
only (1, 2 3) then it should work without any problems.
他的结构是:
master:10.10.105.9
slave1:10.10.105.10
slave2:10.10.105.11
slave3+mha_manage:10.10.105.12
和我的结构类似。
到此,关于“怎么搭建 MHA+MySQL”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注丸趣 TV 网站,丸趣 TV 小编会继续努力为大家带来更多实用的文章!