当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql mha

mysql mha

2019年03月21日  | 移动技术网IT编程  | 我要评论

1.rpm安装mysql5.7

yum remove mariadb* -y
rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm

2.启动mysql,更改root密码

systemctl start mysqld
grep "temporary password" /var/log/mysqld.log
mysqladmin -uroot -p password

3.主从配置

修改mysql配置文件/etc/my.cnf,添加以下内容
[client]
user=root
password=jpcms123!
[mysqld]
server-id=1
read-only=1
log-bin=mysql-bin
relay-log=mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
创建同步账户
mysql>grant replication slave on *.* to 'repl_user'@'192.168.1.%' identified by 'repl_passwd123!';
mysql>grant all on *.* to 'root'@'192.168.1.%' identified by 'jpcms123456!';    #很重要

查看master状态

在slave上设置master

change master to master_host='192.168.1.111',master_user='repl_user',master_password='repl_passwd123!',master_log_file='mysql-bin.000001',master_log_pos=742;

查看slave状态

start slave;
show slave status\g;

4.配置3个节点ssh互信

ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.111
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.112
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.113

最后测试三个节点之间可以无密码登录

ssh 192.168.1.111 date
ssh 192.168.1.112 date
ssh 192.168.1.113 date

5.安装mha

 安装依赖包

rpm -ivh mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
yum install epel-release -y
yum install perl-dbd-mysql perl-config-tiny perl-log-dispatch perl-parallel-forkmanager perl-config-inifiles perl-time-hires

在3个节点安装mha的node

rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm

在slave/mha manager节点上安装mha4mysql-manage

rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

6.配置mha

mkdir -p /etc/mha/scripts
vim /etc/masterha_default.cnf
[server default]
user=root
password=jpcms123456!
ssh_user=root
repl_user=repl_user
repl_password=repl_passwd123!
ping_interval=1
secondary_check_script=masterha_secondary_check -s 192.168.1.111 -s 192.168.1.112 -s 192.168.1.113 --user=repl_user --master_host=localhost.localdomain --master_ip=192.168.1.111 --master_port=3306
master_ip_failover_script="/etc/mha/scripts/master_ip_failover"
# master_ip_online_change_script="/etc/mha/scripts/master_ip_online_change"
# shutdown_script= /script/masterha/power_manager
report_script="/etc/mha/scripts/send_report"
vim /etc/mha/app1.cnf
脚本
#!/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 = '192.168.1.115/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$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 \"`;
}
sub stop_vip() {
 return 0 unless ($ssh_user);
 `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上添加vip

ifconfig eth0:1 192.168.1.115/24

 

测试mha

a.通过 masterha_check_ssh 验证 ssh 信任登录是否成功

masterha_check_ssh --conf=/etc/mha/app1.cnf

b.masterha_check_repl 验证 mysql 复制是否成功

masterha_check_repl --conf=/etc/mha/app1.cnf

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网