mysql中MHA如何搭建

40次阅读
没有评论

共计 4826 个字符,预计需要花费 13 分钟才能阅读完成。

这篇文章主要为大家展示了“mysql 中 MHA 如何搭建”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让丸趣 TV 小编带领大家一起研究并学习一下“mysql 中 MHA 如何搭建”这篇文章吧。

操作系统:
debian5.0.2

数据库版本:
mysql5.0.51a

结构:
manager : 192.168.1.136(M1)

master : 192.168.1.20:3306(N1)
slave1 :  192.168.1.20:3307(N2)
slave2 :  192.168.1.20:3308(N3)
slave3 :  192.168.1.20:3309(N4)

软件包准备:
mha4mysql-manager_0.55-1_all.deb(还有对应的二进制包,源码包)
mha4mysql-node_0.54-1_all.deb

准备就绪:
①安装依赖包
apt-get install libdbd-mysql-perl
apt-get install libconfig-tiny-perl
apt-get install liblog-dispatch-perl
apt-get install libparallel-forkmanager-perl
centos 版(yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager)

②安装 mha manager 包和 node 包
M1:dpkg -i mha4mysql-manager_0.55-1_all.deb
N1:dpkg -i mha4mysql-node_0.54-1_all.deb
N2:dpkg -i mha4mysql-node_0.54-1_all.deb
N3:dpkg -i mha4mysql-node_0.54-1_all.deb
N4:dpkg -i mha4mysql-node_0.54-1_all.deb

③主机信任 (由于公司环境的原因,这里使用的是 mysql 用户)
M1:
#su mysql
#ssh-keygen -t rsa
#cat ~/.ssh/id_rsa.pub
将公钥内容添加到 N1,N2,N3,N4 的对应用户 (这里是 mysql 用户) 下的./ssh/authorized_keys 下即可实现 M1 到 N1,N2,N3,N4 的免密登录。
另外,这里踩了个坑,就是免密登录的 server 端 (N1,N2,N3,N4) 即 /etc/ssh/sshd_config 文件必须开启公钥登录

免密登录的 client 端 (M1) 即 /etc/ssh/ssh_config 文件不能关闭公钥登录(这个参数设置之后在公司环境下貌似还是不能免密登录,最后用了默认配置就可以了)

N1,N2,N3,N4 节点之间也互相信任

2016-12-4 22:44:12 ADD
ssh 客户端得注释以下参数 才能免密登录
PreferredAuthentications password

③新建 mysql mha 管理账户,mysql repl 复制用户

④新建 mha 工作目录
M1:mkdir -p /opt/mha_manager/log  chown -R mysql /opt/mha_manager
N1~N4:mkdir -p /opt/mha_node    chown -R mysql /opt/mha_node

⑤配置 manager
M1:
#mkdir  -p /etc/mha
#cat /etc/mha/app1.conf

manager_workdir=/opt/mha_manager_work

manager_log=/opt/mha_manager_work/log/app1.log

remote_workdir=/opt/mha_node

对以上的配置文件更改属主为 ssh 信任账户(这里是 mysql)

另外(源码安装的 mysql 注意):

 mha 远程登录操作 mysql 的时候默认用不到 /usr/local/mysql/bin 下面的 mysql 以及 mysqlbinlog 会报相应的错误信息,

这个时候我们需要做如下操作

#ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog

#ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

④开启 manager

这里要注意的是,不要后台启动,即不要以 masterha_manager –conf=/etc/mha/app1.conf 形式启动,不然会报错退出。我们前台启动,然后 ctrl+z , bg 放入后台。

至此,整个搭建过程完毕。

注意事项:

①slave 最好设置 read_only=1 relay_log_purge=0, 利用定时任务删除 relay_log;

②mha 进行 failover 之后会在 app1.log 中记录相应的 chang master to master_xxx 等信息,因此我们可以通过这条信息手动将

所宕 master

连接到新主;

③可以使用 (masterha_master_switch –master_state=alive –conf=/etc/mha/app1.cnf) 进行手动切换主,然后将新主手动添加作为老主的从(步骤 2),注意 mha manager 必须处于关闭状态(masterha_check_status –conf=xxx);

④我们可以在 app1.conf 配置主库宕机后 vip 的飘逸脚本(

master_ip_failover_script=/usr/bin/masterha_ip_failover

),但是第一次必须得自己手动在 master 添加 vip.

附 masterha_ip_failover 脚本

点击 (此处) 折叠或打开

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.

#

#  This program is free software; you can redistribute it and/or modify

#  it under the terms of the GNU General Public License as published by

#  the Free Software Foundation; either version 2 of the License, or

# (at your option) any later version.

#

#  This program is distributed in the hope that it will be useful,

#  but WITHOUT ANY WARRANTY; without even the implied warranty of

#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

#  GNU General Public License for more details.

#

#  You should have received a copy of the GNU General Public License

#   along with this program; if not, write to the Free Software

#  Foundation, Inc.,

#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

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 = 192.168.1.111/24

my $key = 0

my $ssh_start_vip = sudo /sbin/ifconfig eth2:$key $vip

my $ssh_stop_vip = sudo /sbin/ifconfig eth2:$key down

#my $ssh_Bcast_arp = /usr/bin/arping -c 3 -A 10.1.99.233 #ARP 回复模式,更新邻居。要是不加则服务器会自动等到 vip 缓存失效,期间 VIP 会有一定时间的不可用。

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();

 # start_arp();

 $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 -p56000 $ssh_user\@$new_master_host \ $ssh_start_vip \

}

#sub start_arp() {

#    `ssh $ssh_user\@$new_master_host \ $ssh_Bcast_arp \

#}

sub stop_vip() {

 `ssh -p56000 $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 中 MHA 如何搭建”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计4826字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)