MHA架构部署
主机名 | IP地址 | 角色 |
---|---|---|
manager | 10.108.26.10 | manager |
server01 | 10.108.26.209 | master |
server02 | 10.108.26.210 | slave |
server03 | 10.108.26.211 | slave |
10.108.26.214 | 虚拟VIP |
基础配置
关闭防火墙
#所有服务器
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
配置主机名及域名解析
hostnamectl set-hostname server01
hostnamectl set-hostname server02
hostnamectl set-hostname server03
hostnamectl set-hostname manager
#manager主机不需要配置
vim /etc/hosts
10.108.26.10 manager
10.108.26.209 server01
10.108.26.210 server02
10.108.26.211 server03
同步时间
#所有节点
yum install ntpdate -y
ntpdate ntp1.aliyun.com
配置主从
#Master节点
vim /etc/my.cnf
server-id = 1
systemctl restart mysqld
#Slave1节点
vim /etc/my.cnf
server-id = 2
systemctl restart mysqld
#Slave2节点
vim /etc/my.cnf
server-id = 3
systemctl restart mysqld
#在 Master、Slave1、Slave2 节点上都创建两个软链接
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
#在主库上创建copy用户及root监控用户
#修改validate_password_policy参数的值
set global validate_password_policy=0;
#修改密码的长度
set global validate_password_length=1;
create user "copy"@"%" identified by "root1234";
grant replication slave on *.* to "copy"@"%";
flush privileges;
#创建mha管理员账号
grant all on *.* to "mha_user"@"10.108.26.%" identified by 'MHA_passwrod@123';
flush privileges;
#在主库上查看master信息
show master status;
#在 Slave1、Slave2 节点执行同步操作
change master to master_host='10.108.26.209',master_user='copy',master_password='root1234',master_port=3306,master_auto_position=1;
start slave;
#查看出现两个yes即成功
show slave status\G
配置无密码登录
#在所有服务器配置无密码登录
#在manager节点配置到所有数据节点的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.108.26.209
ssh-copy-id 10.108.26.210
ssh-copy-id 10.108.26.211
#在server01上配置到数据库节点server02和server03的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.108.26.210
ssh-copy-id 10.108.26.211
#在server02上配置到数据库节点server01和server03的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.108.26.209
ssh-copy-id 10.108.26.211
#在server03上配置到数据库节点server01和server02的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.108.26.209
ssh-copy-id 10.108.26.210
安装 MHA 软件
#安装epel源
yum -y install epel-release
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
#安装node包(所有节点,包括manager)
wget https://qiniu.wsfnk.com/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
#安装manager包(manager节点,其他不需要)
wget https://qiniu.wsfnk.com/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
配置VIP脚本
#编辑VIP切换脚本(manager节点)
vim /usr/local/bin/master_ip_failover #只需要修改VIP地址跟网卡信息即可!!!
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
use MHA::NodeUtil;
use Time::HiRes qw( sleep gettimeofday tv_interval );
use Data::Dumper;
my $_tstart;
my $_running_interval = 0.1;
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,
);
############################添加内容部分#########################################
my $vip = '10.108.26.214/24'; #修改成自己的VIP地址
my $ifdev = 'ens192'; #网卡名称
my $key = '0';
# 更换ip后,一定要执行下arping
my $ssh_start_vip = "/sbin/ifconfig $ifdev:$key $vip;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip";
my $ssh_stop_vip = "/sbin/ifconfig $ifdev:$key down";
my $exit_code = 0;
###############################################################################
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 current_time_us {
my ( $sec, $microsec ) = gettimeofday();
my $curdate = localtime($sec);
return $curdate . " " . sprintf( "%06d", $microsec );
}
sub sleep_until {
my $elapsed = tv_interval($_tstart);
if ( $_running_interval > $elapsed ) {
sleep( $_running_interval - $elapsed );
}
}
sub get_threads_util {
my $dbh = shift;
my $my_connection_id = shift;
my $running_time_threshold = shift;
my $type = shift;
$running_time_threshold = 0 unless ($running_time_threshold);
$type = 0 unless ($type);
my @threads;
my $sth = $dbh->prepare("SHOW PROCESSLIST");
$sth->execute();
while ( my $ref = $sth->fetchrow_hashref() ) {
my $id = $ref->{Id};
my $user = $ref->{User};
my $host = $ref->{Host};
my $command = $ref->{Command};
my $state = $ref->{State};
my $query_time = $ref->{Time};
my $info = $ref->{Info};
$info =~ s/^\s*(.*?)\s*$/$1/ if defined($info);
next if ( $my_connection_id == $id );
next if ( defined($query_time) && $query_time < $running_time_threshold );
next if ( defined($command) && $command eq "Binlog Dump" );
next if ( defined($user) && $user eq "system user" );
next
if ( defined($command)
&& $command eq "Sleep"
&& defined($query_time)
&& $query_time >= 1 );
if ( $type >= 1 ) {
next if ( defined($command) && $command eq "Sleep" );
next if ( defined($command) && $command eq "Connect" );
}
if ( $type >= 2 ) {
next if ( defined($info) && $info =~ m/^select/i );
next if ( defined($info) && $info =~ m/^show/i );
}
push @threads, $ref;
}
return @threads;
}
sub main {
if ( $command eq "stop" ) {
## Gracefully killing connections on the current master
# 1. Set read_only= 1 on the new master
# 2. DROP USER so that no app user can establish new connections
# 3. Set read_only= 1 on the current master
# 4. Kill current queries
# * Any database access failure will result in script die.
my $exit_code = 1;
eval {
## Setting read_only=1 on the new master (to avoid accident)
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error(die_on_error)_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
print current_time_us() . " Set read_only on the new master.. ";
$new_master_handler->enable_read_only();
if ( $new_master_handler->is_read_only() ) {
print "ok.\n";
}
else {
die "Failed!\n";
}
$new_master_handler->disconnect();
# Connecting to the orig master, die if any database error happens
my $orig_master_handler = new MHA::DBHelper();
$orig_master_handler->connect( $orig_master_ip, $orig_master_port,
$orig_master_user, $orig_master_password, 1 );
## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
#$orig_master_handler->disable_log_bin_local();
#print current_time_us() . " Drpping app user on the orig master..\n";
#FIXME_xxx_drop_app_user($orig_master_handler);
## Waiting for N * 100 milliseconds so that current connections can exit
my $time_until_read_only = 15;
$_tstart = [gettimeofday];
my @threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
while ( $time_until_read_only > 0 && $#threads >= 0 ) {
if ( $time_until_read_only % 5 == 0 ) {
printf
"%s Waiting all running %d threads are disconnected.. (max %d milliseconds)\n",
current_time_us(), $#threads + 1, $time_until_read_only * 100;
if ( $#threads < 5 ) {
print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
foreach (@threads);
}
}
sleep_until();
$_tstart = [gettimeofday];
$time_until_read_only--;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
}
## Setting read_only=1 on the current master so that nobody(except SUPER) can write
print current_time_us() . " Set read_only=1 on the orig master.. ";
$orig_master_handler->enable_read_only();
if ( $orig_master_handler->is_read_only() ) {
print "ok.\n";
}
else {
die "Failed!\n";
}
## Waiting for M * 100 milliseconds so that current update queries can complete
my $time_until_kill_threads = 5;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
if ( $time_until_kill_threads % 5 == 0 ) {
printf
"%s Waiting all running %d queries are disconnected.. (max %d milliseconds)\n",
current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
if ( $#threads < 5 ) {
print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
foreach (@threads);
}
}
sleep_until();
$_tstart = [gettimeofday];
$time_until_kill_threads--;
@threads = get_threads_util( $orig_master_handler->{dbh},
$orig_master_handler->{connection_id} );
}
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
## Terminating all threads
print current_time_us() . " Killing all application threads..\n";
$orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
print current_time_us() . " done.\n";
#$orig_master_handler->enable_log_bin_local();
$orig_master_handler->disconnect();
## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
## Activating master ip on the new master
# 1. Create app user with write privileges
# 2. Moving backup script if needed
# 3. Register new master's ip to the catalog database
# We don't return error even though activating updatable accounts/ip failed so that we don't interrupt slaves' recovery.
# If exit code is 0 or 10, MHA does not abort
my $exit_code = 10;
eval {
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
## Set read_only=0 on the new master
#$new_master_handler->disable_log_bin_local();
print current_time_us() . " Set read_only=0 on the new master.\n";
$new_master_handler->disable_read_only();
## Creating an app user on the new master
#print current_time_us() . " Creating app user on the new master..\n";
#FIXME_xxx_create_app_user($new_master_handler);
#$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();
## Update master ip on the catalog database, etc
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
# do nothing
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_online_change --command=start|stop|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --orig_master_user=user --orig_master_password=password --orig_master_ssh_user=sshuser --new_master_host=host --new_master_ip=ip --new_master_port=port --new_master_user=user --new_master_password=password --new_master_ssh_user=sshuser \n";
die;
}
配置在线切换脚本
#编辑在线切换脚本
vim /usr/local/bin/master_ip_online_change #只需要修改VIP地址跟网卡信息即可!!!
#!/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 = '10.108.26.214/24'; #VIP地址
my $ifdev = 'ens192'; #网卡名称
my $key = '0';
# 更换ip后,一定要执行下arping
my $ssh_start_vip = "/sbin/ifconfig $ifdev:$key $vip;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip";
my $ssh_stop_vip = "/sbin/ifconfig $ifdev:$key down";
##################################################################################
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 \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`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";
}
脚本授权
chmod +x /usr/local/bin/master_ip_failover
chmod +x /usr/local/bin/master_ip_online_change
配置MHA脚本
#创建目录(manager操作)
mkdir -p /etc/mha
cd /etc/mha/
#创建app1.cnf(manager操作)
注:app1.conf修改完记得删除后面注释,不然会报错
vim app1.conf
[server default]
user=mha_user #mha管理用户
password=MHA_password@123 #mha管理用户密码
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root #ssh用户
repl_user=copy #主从复制用户
repl_password=root1234 #主从复制用户密码
ping_interval=3
# 增加切换后 VIP自动切换的功能
master_ip_failover_script=/usr/local/bin/master_ip_failover
# 增加手动switch 切换后VIP自动切换功能
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
[server1]
hostname=10.108.26.209
candidate_master=1
master_binlog_dir=/usr/local/mysql/logs
[server2]
hostname=10.108.26.210
candidate_master=1
master_binlog_dir=/usr/local/mysql/logs
#server03设置了不切换master,server01挂掉后只会切换到server02,如果需要server03也切换master,将no_master=1替换成candidate_master=1即可。
[server3]
hostname=10.108.26.211
no_master=1
master_binlog_dir=/usr/local/mysql/logs
初始绑定VIP
#主节点执行(209)
ifconfig ens192:0 10.108.26.214/24 up
#查看VIP地址是否绑定成功
ip a
MHA检测
ssh互通检查
masterha_check_ssh --conf=/etc/mha/app1.conf
#没有报错显示OK即可
主从关系检查
masterha_check_repl --conf=/etc/mha/app1.conf
#没有报错显示OK即可
启动manager
masterha_manager --conf=/etc/mha/app1.conf --ignore_last_failover
主从切换验证
停止server01的MySQL服务,在server02查看slave信息show slave status\G,在server02查看VIP地址是否漂移成功。
server01修复后需重新加入主从
安装supervisor,设置MHA自启
#安装supervisor
yum -y install supervisor
#启动
systemctl start supervisor
systemctl enable supervisor
#编辑mha.ini
cd /etc/supervisord.d
vim mha.ini
[program:mha]
#脚本目录
directory=/etc/mha
#脚本执行命令
command=/usr/bin/masterha_manager --conf=/etc/mha/app1.conf --ignore_last_failover
#supervisor启动的时候是否随着同时启动,默认True
autostart=true
#当程序exit的时候,这个program不会自动重启,默认unexpected,设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
autorestart=false
#这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startsecs=1
#脚本运行的用户身份
user = root
#日志输出
stderr_logfile=/var/log/supervisor/blog_stderr.log
stdout_logfile=/var/log/supervisor/blog_stdout.log
#把stderr重定向到stdout,默认 false
redirect_stderr = true
#stdout日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20MB
#stdout日志文件备份数
stdout_logfile_backups = 20
#使用supervisor启动mha
supervisorctl start all
#检查MHA进程
ps -ef | grep mha
错误总结
#数据库初始化失败执行这条命令
yum -y install numactl