暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【MySQL8.0+MHA部署步骤】

原创 张sir 2023-03-08
1887

1、安装mysql


yum install wget
wget https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
yum install mysql80-community-release-el7-7.noarch.rpm
yum install mysql-community-server
systemctl start mysqld
grep 'temporary password' /var/log/mysqld.log

2、修改密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Jabcdem+9p:z';

set global validate_password.policy=0;

set global validate_password.length=6;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

create user root@'%' IDENTIFIED WITH mysql_native_password BY '123456';

create user 'repl'@'%' identified with mysql_native_password by '123456';

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

create user 'mha'@'%' identified with mysql_native_password by '123456';

grant all privileges on *.* to 'mha'@'%';

flush privileges;



3、关闭selinux,重启os

vim /etc/selinux/config


4、迁移目录到/data/mysql下

systemctl stop mysqld
mv /var/lib/mysql/mysql /data
systemctl start mysqld

5、配置互信,每台都要做
ssh-keygen -t rsa
ssh-copy-id 192.168.88.200
ssh-copy-id 192.168.88.201
ssh-copy-id 192.168.88.202

6、配置mysql
#修改配置文件,开启GTID
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log_bin=/data/mysql/mysql-bin
relay_log=/data/mysql/mysql-relay-bin
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=1 #-----》每个节点都不一样
binlog_format=row
slave_exec_mode=IDEMPOTEN
explicit_defaults_for_timestamp=true
max_allowed_packet = 200M
#Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default_authentication_plugin=mysql_native_password
#开启GTID
gtid_mode=ON
enforce_gtid_consistency=ON
character-set-server = utf8
[client]
socket=/var/lib/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

7、

配置主从复制,在两个备节点执行
change master to
master_host="192.168.88.200",
master_user="repl",
master_password="123456",
master_auto_position=1;


start slave;

show slave status\G


8、安装mha依赖包

[root@localhost ~]# yum -y install epel-release
[root@localhost ~]#yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN




安装mha 每台服务器都需要:
[root@localhost ~]# wget https://qiniu.wsfnk.com/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@localhost ~]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm


安装manager节点,只在manager上安装,这里在db3上执行

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


9、配置管理节点

[root@localhost ~]# mkdir /etc/mha
[root@localhost ~]# mkdir /home/mysql_mha
[root@localhost ~]# vim /etc/mha/mysql_mha.cnf
[server default]
#mha访问数据库的账号与密码
user=mha
password=123456
#指定mha的工作目录
manager_workdir=/home/mysql_mha
#指定管理日志路径
manager_log=/home/mysql_mha/manager.log
#指定mha在远程节点上的工作目录
remote_workdir=/home/mysql_mha
#可以使用ssh登入的用户
ssh_user=root
ssh_port=22
#指定主从复制的mysq用户和密码
repl_user=repl
repl_password=123456
#指定检测间隔时间
ping_interval=1
#指定master节点存放binlog的日志文件的目录
master_binlog_dir=/var/lib/mysql/logs
#指定一个脚本,该脚本实现了在主从切换之后,将虚拟ip漂移到新的master上
master_ip_failover_script=/usr/bin/master_ip_failover
#指定用于二次检查节点状态的节点
secondary_check_script= /usr/local/bin/masterha_secondary_check -s 192.168.88.200 -s 192.168.88.201 -s 192.168.88.202 --user=root --master_host=192.168.88.200 --master_ip=192.168.199.200 --master_port=3306
#配置集群中的节点
[server1]
hostname=192.168.88.200
port=3306

[server2]
hostname=192.168.88.201
#指定该节点可以参与master选举
candidate_master=1
check_repl_delay=0

[server3]
hostname=192.168.88.202
no_master=1


10、编辑vip配置文件

vim /usr/bin/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 = '192.168.88.203/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig ens192:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens192:$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" ) {

# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
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" ) {

# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
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";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}

# A simple system call that enable the VIP on the new master
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/bin/master_ip_failover

11、在其他节点创建mha目录

mkdir /home/mysql_mha

12、在master节点设置vip:
ifconfig ens192:1 192.168.88.203/24


13、在manager进行检测工作,检测ssh免密和mysql主从同步

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

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

14、启动前检查

[root@db3 ~]# masterha_check_status --conf=/etc/mha/mysql_mha.cnf
mysql_mha is stopped(2:NOT_RUNNING).

15、在manager节点上启动mha服务
nohup masterha_manager --conf=/etc/mha/mysql_mha.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /home/mysql_mha/manager.log 2>&1 &


16、测试
a、在200上停止mysql进程
systemctl stop mysqld
b、关注日志
/home/mysql_mha/manager.log
c、在201上查看vip的情况,vip已经切换到201上。
ip a
d、尝试登录数据库
mysql -uroot -p123456 -h192.168.88.203

e、恢复故障机
在原主节点启动mysql:systemctl start mysqld

指定主从,将故障机的主节点指向新的主
change master to
master_host="192.168.88.11",
master_user="rel",
master_password="mysql@123",
master_auto_position=1;
start slave;
show slave status\G

修改/etc/mha/mysql_mha.cnf
secondary_check_script=/usr/bin/masterha_secondary_check -s master-ip -s slave1-ip -s slave2-ip --user=root --master_host=master-ip --master_ip=master-ip --master_port=3306
shutdown_script=""
ssh_user=root
user=root
==========发生一次切换,原主节点【server】信息就会被清理,等server恢复后,需要把相应的信息补全
[server1]
candidate_master=1
hostname=192.168.88.200
port=3306

f、启动mha:
在monitor上启动mha进程:(192.168.88.202)
nohup masterha_manager --conf=/etc/mha/mysql_mha.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /home/mysql_mha/manager.log 2>&1 &
在monitor上检查状态:(192.168.88.202)
/usr/bin/masterha_check_status --conf=/etc/mha/mysql_mha.cnf
查看日志:(192.168.88.202)
less /home/mysql_mha/manager.log

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论