MHA配置
MHA安装
1.下载mysql
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.35-el7-x86_64.tar.gz
mha4mysql-manager-0.58.tar.gz
mha4mysql-node-0.58.tar.gz
2.mysql安装
yum install -y libaio
useradd mysql -s /sbin/nologin
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
tar -xzvf mysql-5.7.35-el7-x86_64.tar.gz -C /usr/local
mv /usr/local/mysql-5.7.35-el7-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
修改/etc/profile
cd /usr/local/mysql
./bin/mysqld --defaults-file=mysql3306.cnf --initialize
mysqld_safe --defaults-file=mysql3306.cnf &
#修改密码
cat /data/mysql/error.log | grep password
mysql -uroot -p'B&Ba)<ce&14R' -S/data/mysql/mysql.sock
set password for root@localhost='123456';
flush privileges;
主从复制
#主库操作记录文件号和位置
show master status
GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' identified by '123456';
flush privileges;
#从库操作
change master to master_host='11.0.1.141',master_user='repl',master_password='123456',master_log_file='mybinlog.000002',master_log_pos=1535;
start slave;
3.安装MHA
#配置ssh互信
ssh-keygen -t rsa
ssh-copy-id -i id_rsa.pub 11.0.1.142
#mha-manager,mha-node都要安装
yum install -y 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
tar -xzvf mha4mysql-node-0.58.tar.gz
#安装mha-node
cd mha4mysql-node-0.58/
perl Makefile.PL
make && make install
#mha-manager节点操作安装mha-manager
tar -xzvf mha4mysql-manager-0.58.tar.gz
perl Makefile.PL #先n 后y
make && make install
mkdir -p /usr/local/mha
mkdir -p /etc/mha
cat >/etc/mha/mha.cnf<<EOF
[server default]
manager_workdir=/usr/local/mha
manager_log=/usr/local/mha/manager.log
master_binlog_dir=/data/mysql
master_ip_failover_script= /usr/local/mha/scripts/master_ip_failover
master_ip_online_change_script= /usr/local/mha/scripts/master_ip_online_change
password=123456
user=root
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=repl
report_script=/usr/local/send_report
secondary_check_script= /usr/local/bin/masterha_secondary_check -s tidb2 -s tidb3 --user=root --master_host=tidb1 --master_ip=11.0.1.141 --master_port=3306
shutdown_script=""
ssh_user=root
[server1]
hostname=11.0.1.141
port=3306
[server2]
hostname=11.0.1.142
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=11.0.1.143
port=3306
EOF
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 = '11.0.1.140/24';
my $mhadev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig $mhadev:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig $mhadev:$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";
}
############################
#master_ip_failover测试脚本
##########该脚本主要用于切换vip
#mysql主节点绑定vip
ifconfig ens33:1 11.0.1.140
#集群都要装
#yum -y install initscripts
cd /usr/local/mha/scripts/
./master_ip_failover --command=stop --ssh_user=root --orig_master_host=11.0.1.141 --orig_master_port=3306 --new_master_host=11.0.1.142 --new_master_port=3306
./master_ip_failover --command=start --ssh_user=root --orig_master_host=11.0.1.141 --orig_master_port=3306 --new_master_host=11.0.1.142 --new_master_port=3306
#################
#检查脚步
masterha_check_ssh --conf=/etc/mha/mha.cnf
masterha_check_repl --conf=/etc/mha/mha.cnf
[root@tidb1 scripts]# find / -name master_ip_online_change
/data/soft/mha4mysql-manager-0.58/samples/scripts/master_ip_online_change
cp /data/soft/mha4mysql-manager-0.58/samples/scripts/master_ip_online_change /usr/local/mha/scripts/
cd /usr/local/mha/scripts/
chmod +x master_ip_online_change
#启动mha-manager
nohup masterha_manager --conf=/etc/mha/mha.cnf < /dev/null> /usr/local/mha/manager_start.log 2>&1 &
#检查脚步
masterha_check_ssh --conf=/etc/mha/mha.cnf
masterha_check_repl --conf=/etc/mha/mha.cnf
masterha_check_status --conf=/etc/mha/mha.cnf
masterha_stop --conf=/etc/mha/mha.cnf
[root@tidb1 mha]# masterha_check_status --conf=/etc/mha/mha.cnf
mha (pid:15959) is running(0:PING_OK), master:11.0.1.141
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix