环境

  • 主服务器 (Master): 192.168.230.142
  • 从服务器 1 (Node1): 192.168.230.143
  • 从服务器 2 (Node2): 192.168.230.144

MySQL 8 的 root 密码为 '1qaz!QAZ'。我们将按照以下步骤重新配置 MySQL 8 和 MHA。

预先的准备

要放行3306端口并关闭selinux

firewall-cmd --add-port=3306/tcp
firewall-cmd --reload
setenforce 0
sed -i.bak '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config

做服务器之间的免密

这个主要是给MHA做的,如果只是主从复制的话就不需要

每台机器上都做一次

[root@master etc]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:awyy1vWR7SkjR0r71KuNKNRkikkh09Vx0QUsAthCXUE root@master
The key's randomart image is:
+---[RSA 3072]----+
|   o.=o=Eoo+.o.  |
|  o = o o.. o    |
|   o o   . .     |
|    .   o  o     |
|   ..o.=S + .    |
|    o+o=.* + .   |
|    o.. B * +    |
|   .  .. * = .   |
|       .. +.o    |
+----[SHA256]-----+
[root@master etc]# ssh-copy-id 192.168.230.142
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.230.142 (192.168.230.142)' can't be established.
ED25519 key fingerprint is SHA256:nvMD1rM9HGT2nAVkvLNYN7dufEuHhP70uR48TRUo/qE.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.230.142's password:
Permission denied, please try again.
root@192.168.230.142's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.230.142'"
and check to make sure that only the key(s) you wanted were added.

[root@master etc]# ssh-copy-id 192.168.230.143
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.230.143 (192.168.230.143)' can't be established.
ED25519 key fingerprint is SHA256:nvMD1rM9HGT2nAVkvLNYN7dufEuHhP70uR48TRUo/qE.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: 192.168.230.142
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.230.143's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.230.143'"
and check to make sure that only the key(s) you wanted were added.

[root@master etc]# ssh-copy-id 192.168.230.144
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.230.144 (192.168.230.144)' can't be established.
ED25519 key fingerprint is SHA256:nvMD1rM9HGT2nAVkvLNYN7dufEuHhP70uR48TRUo/qE.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: 192.168.230.142
    ~/.ssh/known_hosts:4: 192.168.230.143
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.230.144's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.230.144'"
and check to make sure that only the key(s) you wanted were added.
ssh-keygen # 一直回车
ssh-copy-id 192.168.10.142
ssh-copy-id 192.168.10.143
ssh-copy-id 192.168.10.144

1. 安装 MySQL 8

1.1. 添加 MySQL 仓库

在所有服务器上添加 MySQL 的官方 YUM 仓库:

sudo dnf install https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm

1.2. 安装 MySQL 8

在所有服务器上安装 MySQL 8:

sudo dnf install mysql-community-server

1.3. 启动并初始化 MySQL

启动 MySQL 服务并设置开机启动:

sudo systemctl start mysqld
sudo systemctl enable mysqld

1.4. 设置 MySQL root 密码

登录 MySQL:


grep password /var/log/mysqld.log
sudo mysql -u root

设置 root 密码:

ALTER USER 'root'@'localhost' IDENTIFIED BY '1qaz!QAZ';

退出 MySQL:

EXIT;

2. 配置主服务器

2.1. 修改 MySQL 配置文件

在主服务器 (192.168.10.139) 上编辑 MySQL 配置文件 /etc/my.cnf,添加以下配置:

[mysqld]
server-id = 1
log_bin = /var/lib/mysql/mysql-bin.log
binlog_format = row
expire_logs_days = 7
max_binlog_size = 100M

重启 MySQL 服务以应用配置:

sudo systemctl restart mysqld

2.2. 创建复制用户

登录 MySQL:

mysql -u root -p

执行以下 SQL 语句创建复制用户:

CREATE USER 'replca'@'%' IDENTIFIED BY '1qaz!QAZ';
GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%';
FLUSH PRIVILEGES;

获取当前的二进制日志文件和位置:

SHOW MASTER STATUS;

记下 FilePosition 的值。

3. 配置从服务器

3.1. 修改 MySQL 配置文件

在从服务器 1 (192.168.230.143) 和从服务器 2 (192.168.230.144) 上,编辑 MySQL 配置文件 /etc/my.cnf,添加以下配置:

[mysqld]
server-id = 2 # For Node1, use 3 for Node2
relay_log = /var/lib/mysql/mysql-relay-bin.log
log_bin = /var/lib/mysql/mysql-bin.log
binlog_format = row
log-slave-updates=1

重启 MySQL 服务以应用配置:

sudo systemctl restart mysqld

3.2. 配置从服务器连接到主服务器

在每个从服务器上登录 MySQL:

mysql -u root -p

执行以下 SQL 语句配置主服务器信息:

CHANGE MASTER TO
    MASTER_HOST='192.168.10.142',
    MASTER_USER='replca',
    MASTER_PASSWORD='1qaz!QAZ';

START SLAVE;

检查从服务器的状态,确保复制正常:

SHOW SLAVE STATUS\G;

4. 安装和配置 MHA

4.0 准备好perl依赖包和环境

4.1. 安装 MHA

在所有服务器上安装 MHA Manager 和 Node:
查看附件,里面已经有PRM包,先按照node rpm包,再安装manager管理包、

[root@master mha]# ll
总用量 572
-rw-r--r--. 1 root root  81024  8月 11 13:45 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-rw-r--r--. 1 root root 119801  8月 11 13:44 mha4mysql-manager-0.58.tar.gz
-rw-r--r--. 1 root root  36328  8月 11 13:46 mha4mysql-node-0.58-0.el7.centos.noarch.rpm
-rw-r--r--. 1 root root  56220  8月 11 13:46 mha4mysql-node-0.58.tar.gz
-rw-r--r--. 1 root root 284401  8月 11 13:47 mha.zip
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

4.2. 配置 MHA Manager

在 MHA Manager 服务器上,创建配置目录

mkdir -p /data/mysql_mha
mkdir -p /etc/mha/
创建配置文件

[root@master ~]# cat /etc/mha/mha-manager.conf
[server default]
user=mha
password=1qaz!QAZ
port=3306
manager_workdir=/var/log/mha
manager_log=/var/log/mha/manager.log
master_binlog_dir=/var/lib/mysql
remote_workdir=/var/log/mha
repl_user=repl
repl_password=1qaz!QAZ
ping_interval=1
master_ip_failover_script=/etc/mha/scripts/master_ip_failover
master_ip_online_change_script=/etc/mha/scripts/master_ip_online_change
ssh_user=root

[server1]
hostname=192.168.10.67

[server2]
hostname=192.168.10.104

[server3]
hostname=192.168.10.238

master_ip_failover_script

#!/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;
use MHA::DBHelper;

my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);
my $vip = '192.168.168.100/24';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);

exit &main();

sub main {
  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 {

      # updating global catalog, etc
      $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();
            &stop_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;
    }
}


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";
}

配置集群中的节点信息

[server1]
hostname=192.168.230.142

指定该节点可以参与Master选举

candidate_master=1

[server2]
hostname=192.168.230.143
candidate_master=1

[server3]
hostname=192.168.230.144

指定该节点不能参与Master选举

no_master=3
EOF


#### 4.3. 配置 MHA Node

在每个 MySQL 服务器上,创建 MHA 用户并授权:

```sql
mysql -u root -p

执行以下 SQL 语句创建 MHA 用户并授权,注意,必须使用native_password:

CREATE USER 'mha'@'%' IDENTIFIED with mysql_native_password BY  '1qaz!QAZ';
GRANT REPLICATION SLAVE ON *.* TO 'mha'@'%';
GRANT PROCESS ON *.* TO 'mha'@'%';

检查

检查SSH 是否OK

[root@master ~]#  masterha_check_ssh --conf=/etc/mha/mysql-mha.conf
Sun Aug 11 17:15:30 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Aug 11 17:15:30 2024 - [info] Reading application default configuration from /etc/mha/mysql-mha.conf..
Sun Aug 11 17:15:30 2024 - [info] Reading server configuration from /etc/mha/mysql-mha.conf..
Sun Aug 11 17:15:30 2024 - [info] Starting SSH connection tests..
Sun Aug 11 17:15:32 2024 - [debug]
Sun Aug 11 17:15:30 2024 - [debug]  Connecting via SSH from root@192.168.230.142(192.168.230.142:22) to root@192.168.230.143(192.168.230.143:22)..
Sun Aug 11 17:15:31 2024 - [debug]   ok.
Sun Aug 11 17:15:31 2024 - [debug]  Connecting via SSH from root@192.168.230.142(192.168.230.142:22) to root@192.168.230.144(192.168.230.144:22)..
Sun Aug 11 17:15:32 2024 - [debug]   ok.
Sun Aug 11 17:15:33 2024 - [debug]
Sun Aug 11 17:15:31 2024 - [debug]  Connecting via SSH from root@192.168.230.144(192.168.230.144:22) to root@192.168.230.142(192.168.230.142:22)..
Sun Aug 11 17:15:32 2024 - [debug]   ok.
Sun Aug 11 17:15:32 2024 - [debug]  Connecting via SSH from root@192.168.230.144(192.168.230.144:22) to root@192.168.230.143(192.168.230.143:22)..
Sun Aug 11 17:15:32 2024 - [debug]   ok.
Sun Aug 11 17:15:33 2024 - [debug]
Sun Aug 11 17:15:31 2024 - [debug]  Connecting via SSH from root@192.168.230.143(192.168.230.143:22) to root@192.168.230.142(192.168.230.142:22)..
Sun Aug 11 17:15:31 2024 - [debug]   ok.
Sun Aug 11 17:15:31 2024 - [debug]  Connecting via SSH from root@192.168.230.143(192.168.230.143:22) to root@192.168.230.144(192.168.230.144:22)..
Sun Aug 11 17:15:32 2024 - [debug]   ok.
Sun Aug 11 17:15:33 2024 - [info] All SSH connection tests passed successfully.
Use of uninitialized value in exit at /usr/bin/masterha_check_ssh line 44.
[root@master ~]#

用于检查主从节点的复制链路是否正常

4.4. 启动 MHA Manager

启动 MHA Manager 以监控主从切换:

masterha_manager --conf=/etc/mha/mha_manager.cnf

5. 测试配置

5.1. 检查 MHA 状态

使用以下命令检查 MHA 监控的主从状态和故障转移情况:

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

5.2. 模拟故障转移

为了测试故障转移,停止主服务器的 MySQL 服务:

sudo systemctl stop mysqld

MHA Manager 应该能够检测到主服务器故障,并将一个从服务器提升为新的主服务器。

安装mha软件没仓库出错

看起来 mha4mysql-managermha4mysql-node 在 CentOS Stream 9 的默认仓库中不可用。我们需要手动安装 MHA(Master High Availability)工具。以下是手动安装 MHA 的步骤:

1. 安装 MHA 的前提条件

确保你已经安装了 EPEL 仓库,它包含了一些额外的软件包:

sudo dnf install epel-release

实际上安装 ERPL仓库也没用,如果是centos7的话或许可以。目前我们使用的是centos9,需要用到清华的云,
首先把centos下默认的yum库调换的,分别是centos.repo和centos-addons.repo.

centos.repo如下,覆盖原理的系统文件。

[baseos]
name=CentOS Stream $releasever - BaseOS
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/BaseOS/$basearch/os
# metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[baseos-debuginfo]
name=CentOS Stream $releasever - BaseOS - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/BaseOS/$basearch/debug/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-debug-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[baseos-source]
name=CentOS Stream $releasever - BaseOS - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/BaseOS/source/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-baseos-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[appstream]
name=CentOS Stream $releasever - AppStream
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/AppStream/$basearch/os
# metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[appstream-debuginfo]
name=CentOS Stream $releasever - AppStream - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/AppStream/$basearch/debug/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-debug-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[appstream-source]
name=CentOS Stream $releasever - AppStream - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/AppStream/source/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-appstream-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[crb]
name=CentOS Stream $releasever - CRB
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/CRB/$basearch/os
# metalink=https://mirrors.centos.org/metalink?repo=centos-crb-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[crb-debuginfo]
name=CentOS Stream $releasever - CRB - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/CRB/$basearch/debug/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-crb-debug-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[crb-source]
name=CentOS Stream $releasever - CRB - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/CRB/source/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-crb-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

用以下的文本覆盖 /etc/yum.repo.d/centos-addons.repo

[highavailability]
name=CentOS Stream $releasever - HighAvailability
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/HighAvailability/$basearch/os
# metalink=https://mirrors.centos.org/metalink?repo=centos-highavailability-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[highavailability-debuginfo]
name=CentOS Stream $releasever - HighAvailability - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/HighAvailability/$basearch/debug/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-highavailability-debug-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[highavailability-source]
name=CentOS Stream $releasever - HighAvailability - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/HighAvailability/source/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-highavailability-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[nfv]
name=CentOS Stream $releasever - NFV
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/NFV/$basearch/os
# metalink=https://mirrors.centos.org/metalink?repo=centos-nfv-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[nfv-debuginfo]
name=CentOS Stream $releasever - NFV - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/NFV/$basearch/debug/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-nfv-debug-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[nfv-source]
name=CentOS Stream $releasever - NFV - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/NFV/source/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-nfv-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[rt]
name=CentOS Stream $releasever - RT
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/RT/$basearch/os
# metalink=https://mirrors.centos.org/metalink?repo=centos-rt-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[rt-debuginfo]
name=CentOS Stream $releasever - RT - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/RT/$basearch/debug/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-rt-debug-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[rt-source]
name=CentOS Stream $releasever - RT - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/RT/source/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-rt-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[resilientstorage]
name=CentOS Stream $releasever - ResilientStorage
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/ResilientStorage/$basearch/os
# metalink=https://mirrors.centos.org/metalink?repo=centos-resilientstorage-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=0

[resilientstorage-debuginfo]
name=CentOS Stream $releasever - ResilientStorage - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/ResilientStorage/$basearch/debug/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-resilientstorage-debug-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[resilientstorage-source]
name=CentOS Stream $releasever - ResilientStorage - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/ResilientStorage/source/tree/
# metalink=https://mirrors.centos.org/metalink?repo=centos-resilientstorage-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

[extras-common]
name=CentOS Stream $releasever - Extras packages
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/SIGs/$releasever-stream/extras/$basearch/extras-common
# metalink=https://mirrors.centos.org/metalink?repo=centos-extras-sig-extras-common-$stream&arch=$basearch&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
countme=1
enabled=1

[extras-common-source]
name=CentOS Stream $releasever - Extras packages - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/SIGs/$releasever-stream/extras/source/extras-common
# metalink=https://mirrors.centos.org/metalink?repo=centos-extras-sig-extras-common-source-$stream&arch=source&protocol=https,http
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512
gpgcheck=1
repo_gpgcheck=0
metadata_expire=6h
enabled=0

这样二个文件覆盖好以后,使用
dnf makecache 准备缓冲列表。

2. 安装 MHA 的依赖包

MHA 需要一些 Perl 模块和其他依赖包。安装这些依赖包:

sudo dnf install perl-DBD-MySQL perl-DBD-DBI perl-DBI perl-Config-Tiny perl-Time-HiRes perl-Parallel-ForkManager perl-Log-Dispatch 

如果不更换仓库,是不可以成功安装上述模块的。

3. 下载并安装 MHA

MHA 的安装包可以从其 GitHub 仓库或官方网站下载。以下步骤将引导你下载并安装 MHA:

3.1. 下载 MHA

到 [MHA 的 GitHub 发布页面]https://github.com/yoshinorim/) 下载最新的 MHA 版本。

例如,下载 mha4mysql-managermha4mysql-node.tar.gz 文件:

看本文附件,是zip包 。


cd /tmp
wget https://github.com/yoshinorim/mha4mysql/releases/download/v0.59/mha4mysql-manager-0.59.tar.gz
wget https://github.com/yoshinorim/mha4mysql/releases/download/v0.59/mha4mysql-node-0.59.tar.gz

3.2. 解压和安装 MHA

解压下载的文件并安装:

tar -xzf mha4mysql-manager-0.59.tar.gz
tar -xzf mha4mysql-node-0.59.tar.gz

cd mha4mysql-manager-0.59
perl Makefile.PL
make
sudo make install

cd ../mha4mysql-node-0.59
perl Makefile.PL
make
sudo make install

4. 配置 MHA

在所有服务器上配置 MHA,包括 MHA Manager 和 Node。

4.1. 配置 MHA Manager

在 MHA Manager 服务器上创建配置文件 /etc/mha/mha_manager.cnf

[server1]
hostname=192.168.10.139
user=mha
password=mha_password

[server2]
hostname=192.168.10.155
user=mha
password=mha_password

[server3]
hostname=192.168.10.165
user=mha
password=mha_password

[manager]
manager_log=/var/log/mha/mha_manager.log
manager_workdir=/var/lib/mha
manager_admin_password=mha_admin_password

4.2. 配置 MHA Node

在每个 MySQL 服务器上创建 MHA 用户并授权:

CREATE USER 'mha'@'%' IDENTIFIED BY 'mha_password';
GRANT REPLICATION SLAVE ON *.* TO 'mha'@'%';
GRANT PROCESS ON *.* TO 'mha'@'%';

创建 MHA Node 配置文件 /etc/mha/mha_node.cnf

[server]
hostname=192.168.10.139
user=mha
password=mha_password

在每个从服务器上执行相同的步骤,只需将 hostname 替换为相应的 IP 地址。

5. 启动 MHA

5.1. 启动 MHA Manager

启动 MHA Manager 以监控主从切换:

masterha_manager --conf=/etc/mha/mha_manager.cnf

5.2. 启动 MHA Node

在每个从服务器上启动 MHA Node:

masterha_node --conf=/etc/mha/mha_node.cnf

6. 测试配置

6.1. 检查 MHA 状态

使用以下命令检查 MHA 监控的主从状态和故障转移情况:

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

6.2. 模拟故障转移

为了测试故障转移,停止主服务器的 MySQL 服务:

sudo systemctl stop mysqld

MHA Manager 应该能够检测到主服务器故障,并将一个从服务器提升为新的主服务器。

总结

通过手动下载和安装 MHA 以及配置相应的文件,你可以在 CentOS Stream 9 上成功设置 MySQL 8 和 MHA 高可用性架构。如果在安装过程中遇到任何问题,请检查 MHA 的官方文档或 GitHub 页面以获取更多帮助。

附件
作者:严锋  创建时间:2024-08-08 23:11
最后编辑:严锋  更新时间:2025-05-22 13:03