kjh00n의 기록저장소

Rocky Linux9 + MySQL 8로 MHA 구성 (SSL 없는 ver.) 본문

파이널 프로젝트

Rocky Linux9 + MySQL 8로 MHA 구성 (SSL 없는 ver.)

kjh00n 2025. 2. 13. 17:05

[서버 구성]

MHA Manager Rocky Linux9 + MySQL 8  192.168.50.10
MHA Master Rocky Linux9 + MySQL 8 192.168.50.70
MHA Slave Rocky Linux9 + MySQL 8 192.168.50.80
MHA Slave 2 Rocky Linux9 + MySQL 8 192.168.50.90

 

[모든 서버] (필요한 패키지 설치)

1. CPAN을 사용하기 위한 Perl 패키지 설치
# Perl의 CPAN (Comprehensive Perl Archive Network) 모듈을 관리할 수 있게 해주는 패키지를 설치합니다.
yum -y install perl-CPAN  

2. 시스템 패키지 업데이트 (커널 포함) (필수!!!!!!!!!!)
# 시스템 전체 패키지 업데이트, 여기에는 커널도 포함되어 최신 버전으로 업데이트합니다.
dnf update -y  

3. Git 설치 (소스 코드 관리 도구) 
# Git을 설치하여 소스 코드 관리 및 버전 관리를 할 수 있습니다.
yum -y install git-core 

4. Perl 개발 관련 패키지 설치
# Perl 모듈을 개발하는 데 필요한 도구들을 설치합니다. 예: 컴파일러, 헤더 파일 등
yum -y install perl-devel

5. MariaDB와 연결을 위한 Perl DBI 드라이버 설치
# Perl에서 MySQL(MariaDB)과 연결할 수 있게 해주는 DBI 드라이버 설치
yum -y install perl-DBD-MySQL

6. CPAN에서 Perl 모듈 설치 (Config::Tiny)
# Perl 설정 파일을 쉽게 읽고 쓸 수 있도록 도와주는 모듈 설치
cpan install Config::Tiny

7. CPAN에서 Perl 모듈 설치 (Log::Dispatch)
# Perl 애플리케이션의 로그를 다양한 방식으로 처리하고 출력할 수 있도록 하는 모듈 설치
cpan install Log::Dispatch

8. CPAN에서 Perl 모듈 설치 (Parallel::ForkManager)
# Perl에서 병렬 프로세스를 관리하고, 동시에 여러 작업을 처리할 수 있게 해주는 모듈 설치
cpan install Parallel::ForkManager

9. CPAN에서 Perl 모듈 설치 (Module::Install)
# Perl 모듈의 설치와 빌드를 쉽게 할 수 있도록 도와주는 도구 설치
cpan install Module::Install
vim /etc/ssh/sshd_config

Port 22	주석 해제하기
PermitRootLogin yes 추가하기

systemctl restart sshd

[Master와 Slave 구성 및 이중화]

1. [mysql 설치] (Master / Slave)

yum -y install mysql-server
systemctl enable mysqld
systemctl start mysqld

 

2. [mysql 접속해서 replication 계정 생성] (Master / Slave)

create user 'repl'@'%' identified by 'repl';
grant replication slave on *.* to 'repl'@'%';
flush privileges;

[SSL을 사용하지 않기 위해 설정]
ALTER USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY 'repl';
FLUSH PRIVILEGES;

 

3. [mha 계정 생성] (Master / Slave)

create user 'mha'@'%' identified by 'mha';
grant all privileges on *.* to 'mha'@'%';
flush privileges;

 

4. [master server의 my.cnf.d/mysql-server.cnf] (Master)

[mariadb]
server_id=1
log-bin=mysql-bin

 

5. [slave server의 my.cnf.d/mysql-server.cnf] (Slave)

[mariadb]
server_id=2
log-bin=mysql-bin
relay_log_purge=0

 

6. [master와 slave의 mysql 재시작] (Master / Slave)

systemctl restart mysqld

 

7. [master와 slave 이중화] (Master)

Master Mysql에서 입력

7-1. [slave에서 master 연결] (Slave)

CHANGE MASTER TO
    MASTER_HOST='192.168.50.80',
    MASTER_USER='repl',
    MASTER_PASSWORD='repl',
    MASTER_LOG_FILE='mysql-bin.000010',
    MASTER_LOG_POS=348,
    MASTER_SSL=0; (Mysql 8 버전에서 기본적으로 SSL을 사용권장하기 때문에 일단 사용하지 않는 방향으로 설정)
    
start slave;

show slave status\G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.50.80
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000010
          Read_Master_Log_Pos: 348
               Relay_Log_File: localhost-relay-bin.000006
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000010
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 348
              Relay_Log_Space: 889
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
                  Master_UUID: 875bfb84-e9e8-11ef-80c2-005056309e4a
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace:

위처럼 Last_IO_Errno와 Last_IO_Error가 없어야 하고

Slave_IO_Running과 Slave_SQL_Running이 YES로 설정되어 있어야 한다

 

[방화벽 설정]

(모든 서버)

systemctl start firewalld.service

firewall-cmd --list-ports (처음에는 아무런 결과도 나오지 않음)

firewall-cmd --zone=public --add-port=22/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload

firewall-cmd --list-ports

[root@localhost ~]# firewall-cmd --list-ports
22/tcp 3306/tcp

↑ 위 결과처럼 나옴

[MHA 설치]

1. MHA 프로그램이 사용할 디렉토리 생성 (모든 서버)

mkdir /mha

 

 

2. MHA 유저 생성 (모든 서버)

useradd -g mysql -d /home/mhauser -m -s /bin/bash mhauser

passwd mhauser
mhauser

chown -R mhauser:mysql /mha

 

3. MHA Node 설치 (모든 서버)

mkdir /source

cd /source

git clone https://github.com/yoshinorim/mha4mysql-node.git
cd /source/mha4mysql-node
perl Makefile.PL
make
make install

 

4. MHA Manager 설치 (Manager)

cd /source

git clone https://github.com/yoshinorim/mha4mysql-manager.git

cd /source/mha4mysql-manager
perl Makefile.PL
make
make install

 

5. MHA 모니터링과 Failover 수행을 위해 각각의 서버들이 서로 간에 비밀번호 없이 SSH 접속할 수 있어야 함  (모든 서버)

visudo

# 맨 밑줄에 추가
mhauser ALL = (ALL) NOPASSWD:/sbin/ifconfig
mhauser  ALL=(ALL)       ALL                 → mhauser에서 sudo를 사용하기 위함

 

6. 각 서버에서 다른 서버들에게 공개키를 줘야 함.

(MHA Manager -> Master DB, Slave DB / Master DB -> Slave DB, MHA Manager / Slave DB -> MasterDB, MHA Manager)

키 생성 :
	(모든 Server에서)
	su - mhauser
	ssh-keygen -t rsa [엔터 3번]

키 복사 :
	(Manager Server에서)
	ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.50.70
	ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.56.80
    ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.56.90
    
	(Master Server에서)
	ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.50.10
	ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.56.80
    ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.56.90
    
	(Slave Server에서)
	ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.50.10
	ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.56.70
    ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.56.80
    ssh-copy-id -i ~/.ssh/id_rsa.pub mhauser@192.168.56.90
    

연결 테스트 :
	ssh 192.168.50.10
	ssh 192.168.50.70
	ssh 192.168.50.80
	ssh 192.168.50.90

 

7. 파일의 내용이 변경되지 않도록 권한 수정 (모든 서버)

su - mhauser
cd .ssh
chmod 600 authorized_keys

 

8. MHA Manager 명령어 커스텀마이징 (Manager)

su - mhauser (mhauser로 전환하고 실행할 것)

sudo vim .bash_profile 에 아래 내용 추가

set -o vi
alias sshcheck='/usr/local/bin/masterha_check_ssh --conf=/etc/mha.cnf'
alias replcheck='/usr/local/bin/masterha_check_repl --conf=/etc/mha.cnf'
alias start='/usr/local/bin/masterha_manager --conf=/etc/mha.cnf &'
alias stop='/usr/local/bin/masterha_stop --conf=/etc/mha.cnf'
alias status='/usr/local/bin/masterha_check_status --conf=/etc/mha.cnf'
alias log='tail -f /mha/manager.log'

저장 후
source .bash_profile

 

9. MHA 사용할 config 파일 생성 (Manager)

cp -p /source/mha4mysql-manager/samples/conf/app1.cnf /etc/mha.cnf

[server default]
user=mha                      # MHA 관리자 사용자 계정 (일반적으로 MHA 관리용 사용자)
password=mha                  # MHA 관리자의 비밀번호 (위에서 지정된 사용자 'mha'의 비밀번호)

ssh_user=mhauser               # MHA에서 사용할 SSH 접속 사용자 (서버에 SSH로 연결할 때 사용되는 사용자)
                               # 일반적으로, MHA 관리용 사용자와 연결이 설정됩니다.

repl_user=repl                 # 복제에 사용될 MySQL 사용자 계정 (슬레이브가 마스터로부터 데이터를 복제할 때 사용)
repl_password=repl             # 복제 사용자의 비밀번호 (위에서 지정된 `repl` 사용자의 비밀번호)

manager_workdir=/mha           # MHA Manager가 작업을 수행하는 디렉터리 경로 (MHA Manager 로그 및 상태 파일이 저장되는 경로)
manager_log=/mha/manager.log   # MHA Manager 로그 파일 경로

remote_workdir=/mha           # 원격 서버에서 MHA Manager가 작업을 수행할 디렉터리 경로

master_binlog_dir=/var/lib/mysql  # 마스터 서버에서 Binary Log 파일이 위치한 경로 (복제와 관련된 로그 파일)


[server1]
hostname=192.168.50.70         # 서버1의 IP 주소 또는 호스트네임 (서버1은 주로 슬레이브나 후보 마스터로 설정될 수 있음)
port=3306                      # 서버1에서 MySQL의 포트 번호 (기본 MySQL 포트)
candidate_master=1             # 서버1이 마스터로 승격될 후보인지 여부 (1: 승격 가능, 0: 승격 불가)
                               # 이 설정이 1인 서버는 장애 발생 시 새로운 마스터 후보로 승격될 수 있습니다.

[server2]
hostname=192.168.50.80         # 서버2의 IP 주소 또는 호스트네임 (서버2는 마스터 또는 후보 마스터로 설정될 수 있음)
port=3306                      # 서버2에서 MySQL의 포트 번호 (기본 MySQL 포트)
candidate_master=1             # 서버2가 마스터로 승격될 후보인지 여부 (1: 승격 가능, 0: 승격 불가)
                               # 이 설정이 1인 서버도 장애 발생 시 새로운 마스터 후보로 승격될 수 있습니다.                             
[server3]
hostname=192.168.50.90
port=3306
candidate_master=1

 

[MHA 설정 확인]

(Manager)

sshcheck

replcheck

↓ replcheck라고 입력하면 밑처럼 MySQL Replication Health is OK가 출력되야 정상작동 가능

[mhauser@localhost ~]$ replcheck
Fri Feb 14 01:13:15 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Feb 14 01:13:15 2025 - [info] Reading application default configuration from /etc/mha.cnf..
Fri Feb 14 01:13:15 2025 - [info] Reading server configuration from /etc/mha.cnf..
Fri Feb 14 01:13:15 2025 - [info] MHA::MasterMonitor version 0.58.
Fri Feb 14 01:13:16 2025 - [info] GTID failover mode = 0
Fri Feb 14 01:13:16 2025 - [info] Dead Servers:
Fri Feb 14 01:13:16 2025 - [info] Alive Servers:
Fri Feb 14 01:13:16 2025 - [info]   192.168.50.70(192.168.50.70:3306)
Fri Feb 14 01:13:16 2025 - [info]   192.168.50.80(192.168.50.80:3306)
Fri Feb 14 01:13:16 2025 - [info] Alive Slaves:
Fri Feb 14 01:13:16 2025 - [info]   192.168.50.70(192.168.50.70:3306)  Version=8.0.36 (oldest major version between slaves) log-bin:enabled
Fri Feb 14 01:13:16 2025 - [info]     Replicating from 192.168.50.80(192.168.50.80:3306)
Fri Feb 14 01:13:16 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Feb 14 01:13:16 2025 - [info] Current Alive Master: 192.168.50.80(192.168.50.80:3306)
Fri Feb 14 01:13:16 2025 - [info] Checking slave configurations..
Fri Feb 14 01:13:16 2025 - [info]  read_only=1 is not set on slave 192.168.50.70(192.168.50.70:3306).
Fri Feb 14 01:13:16 2025 - [warning]  relay_log_purge=0 is not set on slave 192.168.50.70(192.168.50.70:3306).
Fri Feb 14 01:13:16 2025 - [info] Checking replication filtering settings..
Fri Feb 14 01:13:16 2025 - [info]  binlog_do_db= , binlog_ignore_db= 
Fri Feb 14 01:13:16 2025 - [info]  Replication filtering check ok.
Fri Feb 14 01:13:16 2025 - [info] GTID (with auto-pos) is not supported
Fri Feb 14 01:13:16 2025 - [info] Starting SSH connection tests..
Fri Feb 14 01:13:17 2025 - [info] All SSH connection tests passed successfully.
Fri Feb 14 01:13:17 2025 - [info] Checking MHA Node version..
Fri Feb 14 01:13:18 2025 - [info]  Version check ok.
Fri Feb 14 01:13:18 2025 - [info] Checking SSH publickey authentication settings on the current master..
Fri Feb 14 01:13:18 2025 - [info] HealthCheck: SSH to 192.168.50.80 is reachable.
Fri Feb 14 01:13:18 2025 - [info] Master MHA Node version is 0.58.
Fri Feb 14 01:13:18 2025 - [info] Checking recovery script configurations on 192.168.50.80(192.168.50.80:3306)..
Fri Feb 14 01:13:18 2025 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/mha/save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000010 
Fri Feb 14 01:13:18 2025 - [info]   Connecting to mhauser@192.168.50.80(192.168.50.80:22).. 
  Creating /mha if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to mysql-bin.000010
Fri Feb 14 01:13:19 2025 - [info] Binlog setting check done.
Fri Feb 14 01:13:19 2025 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Feb 14 01:13:19 2025 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.50.70 --slave_ip=192.168.50.70 --slave_port=3306 --workdir=/mha --target_version=8.0.36 --manager_version=0.58 --relay_dir=/var/lib/mysql --current_relay_log=localhost-relay-bin.000006  --slave_pass=xxx
Fri Feb 14 01:13:19 2025 - [info]   Connecting to mhauser@192.168.50.70(192.168.50.70:22).. 
  Checking slave recovery environment settings..
    Relay log found at /var/lib/mysql, up to localhost-relay-bin.000006
    Temporary relay log file is /var/lib/mysql/localhost-relay-bin.000006
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Feb 14 01:13:19 2025 - [info] Slaves settings check done.
Fri Feb 14 01:13:19 2025 - [info] 
192.168.50.80(192.168.50.80:3306) (current master)
 +--192.168.50.70(192.168.50.70:3306)

Fri Feb 14 01:13:19 2025 - [info] Checking replication health on 192.168.50.70..
Fri Feb 14 01:13:19 2025 - [info]  ok.
Fri Feb 14 01:13:19 2025 - [info] Checking master_ip_failover_script status:
Fri Feb 14 01:13:19 2025 - [info]   /mha/scripts/master_ip_failover --command=status --ssh_user=mhauser --orig_master_host=192.168.50.80 --orig_master_ip=192.168.50.80 --orig_master_port=3306 
Fri Feb 14 01:13:19 2025 - [info]  OK.
Fri Feb 14 01:13:19 2025 - [warning] shutdown_script is not defined.
Fri Feb 14 01:13:19 2025 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

 

[Slave IP ↔ Master IP 변경 스크립트 설정]

이 스크립트는 마스터 서버의 장애 발생 시 슬레이브 서버를 새로운 마스터로 승격시키는 과정에서 슬레이브 서버의 IP가 새로운 마스터의 IP로 전환될 수 있도록 돕는 역할을 합니다.

(Manager) 

mkdir /mha/scripts

cp -p /source/mha4mysql-manager-0.57/samples/scripts/master_ip_online_change /mha/scripts/master_ip_online_change

vim /mha/scripts/master_ip_online_change 수정

150, 151, 152, 245, 246, 247, 248 라인에 주석 추가

:set number
:150

    149       ## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
    150       # $orig_master_handler->disable_log_bin_local();
    151       # print current_time_us() . " Drpping app user on the orig master..\n";
    152       # FIXME_xxx_drop_app_user($orig_master_handler);

:245

    244       ## Creating an app user on the new master
    245       # print current_time_us() . " Creating app user on the new master..\n";
    246       # FIXME_xxx_create_app_user($new_master_handler);
    247       # $new_master_handler->enable_log_bin_local();
    248       # $new_master_handler->disconnect();

↓ vim /etc/mha.cnf 수정 (master_ip_online_change 파일 경로 추가) ↓

[server default]
user=mha
password=mha

ssh_user=mhauser

repl_user=repl
repl_password=repl

manager_workdir=/mha
manager_log=/mha/manager.log
remote_workdir=/mha

master_binlog_dir=/var/lib/mysql

↓ 얘를 추가
master_ip_online_change_script=/mha/scripts/master_ip_online_change

[server1]
hostname=192.168.50.70
port=3306
candidate_master=1

[server2]
hostname=192.168.50.80
port=3306
candidate_master=1

[server3]
hostname=192.168.50.90
port=3306
candidate_master=1

 

[Failover 스크립트 설정]

위 스크립트와의 차이점

cp -p /source/mha4mysql-manager-0.57/samples/scripts/master_ip_failover /mha/scripts/

vim /mha/scripts/master_ip_failover 수정

87, 88, 89, 90, 93 라인 주석 처리

:set number
:87

     86       ## Creating an app user on the new master
     87       # print "Creating app user on the new master..\n";
     88       # FIXME_xxx_create_user( $new_master_handler->{dbh} );
     89       # $new_master_handler->enable_log_bin_local();
     90       # $new_master_handler->disconnect();
     91
     92       ## Update master ip on the catalog database, etc
     93       # FIXME_xxx;

vim /etc/mha.cnf 수정 (Failover 스크립트 설정) ↓

[server default]
user=mha
password=mha

ssh_user=mhauser

repl_user=repl
repl_password=repl

manager_workdir=/mha
manager_log=/mha/manager.log
remote_workdir=/mha

master_binlog_dir=/var/lib/mysql

master_ip_online_change_script=/mha/scripts/master_ip_online_change

↓ 얘를 추가
master_ip_failover_script=/mha/scripts/master_ip_failover

[server1]
hostname=192.168.50.70
port=3306
candidate_master=1

[server2]
hostname=192.168.50.80
port=3306
candidate_master=1

[server3]
hostname=192.168.50.90
port=3306
candidate_master=1

[구현]

→ Master Mysql Server의 상태

→ Slave (192.168.50.80)이 Master (192.168.50.70)에 이중화 되어있는 상태

→ Slave 2 (192.168.50.90)이 Master (192.168.50.70)에 이중화 되어있는 상태

[MHA 상태 점검]

sshcheck 입력

replcheck 입력

[mhauser@localhost ~]$ replcheck
Fri Feb 14 19:37:04 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Feb 14 19:37:04 2025 - [info] Reading application default configuration from /etc/mha.cnf..
Fri Feb 14 19:37:04 2025 - [info] Reading server configuration from /etc/mha.cnf..
Fri Feb 14 19:37:04 2025 - [info] MHA::MasterMonitor version 0.58.
Fri Feb 14 19:37:05 2025 - [info] GTID failover mode = 0
Fri Feb 14 19:37:05 2025 - [info] Dead Servers:
Fri Feb 14 19:37:05 2025 - [info] Alive Servers:
Fri Feb 14 19:37:05 2025 - [info]   192.168.50.70(192.168.50.70:3306)
Fri Feb 14 19:37:05 2025 - [info]   192.168.50.80(192.168.50.80:3306)
Fri Feb 14 19:37:05 2025 - [info]   192.168.50.90(192.168.50.90:3306)
Fri Feb 14 19:37:05 2025 - [info] Alive Slaves:
Fri Feb 14 19:37:05 2025 - [info]   192.168.50.80(192.168.50.80:3306)  Version=8.0.36 (oldest major version between slaves) log-bin:enabled
Fri Feb 14 19:37:05 2025 - [info]     Replicating from 192.168.50.70(192.168.50.70:3306)
Fri Feb 14 19:37:05 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Feb 14 19:37:05 2025 - [info]   192.168.50.90(192.168.50.90:3306)  Version=8.0.36 (oldest major version between slaves) log-bin:enabled
Fri Feb 14 19:37:05 2025 - [info]     Replicating from 192.168.50.70(192.168.50.70:3306)
Fri Feb 14 19:37:05 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Feb 14 19:37:05 2025 - [info] Current Alive Master: 192.168.50.70(192.168.50.70:3306)
Fri Feb 14 19:37:05 2025 - [info] Checking slave configurations..
Fri Feb 14 19:37:05 2025 - [info]  read_only=1 is not set on slave 192.168.50.80(192.168.50.80:3306).
Fri Feb 14 19:37:05 2025 - [warning]  relay_log_purge=0 is not set on slave 192.168.50.80(192.168.50.80:3306).
Fri Feb 14 19:37:05 2025 - [info]  read_only=1 is not set on slave 192.168.50.90(192.168.50.90:3306).
Fri Feb 14 19:37:05 2025 - [info] Checking replication filtering settings..
Fri Feb 14 19:37:05 2025 - [info]  binlog_do_db= , binlog_ignore_db= 
Fri Feb 14 19:37:05 2025 - [info]  Replication filtering check ok.
Fri Feb 14 19:37:05 2025 - [info] GTID (with auto-pos) is not supported
Fri Feb 14 19:37:05 2025 - [info] Starting SSH connection tests..
Fri Feb 14 19:37:08 2025 - [info] All SSH connection tests passed successfully.
Fri Feb 14 19:37:08 2025 - [info] Checking MHA Node version..
Fri Feb 14 19:37:08 2025 - [info]  Version check ok.
Fri Feb 14 19:37:08 2025 - [info] Checking SSH publickey authentication settings on the current master..
Fri Feb 14 19:37:09 2025 - [info] HealthCheck: SSH to 192.168.50.70 is reachable.
Fri Feb 14 19:37:09 2025 - [info] Master MHA Node version is 0.58.
Fri Feb 14 19:37:09 2025 - [info] Checking recovery script configurations on 192.168.50.70(192.168.50.70:3306)..
Fri Feb 14 19:37:09 2025 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/mha/save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000026 
Fri Feb 14 19:37:09 2025 - [info]   Connecting to mhauser@192.168.50.70(192.168.50.70:22).. 
  Creating /mha if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to mysql-bin.000026
Fri Feb 14 19:37:09 2025 - [info] Binlog setting check done.
Fri Feb 14 19:37:09 2025 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Feb 14 19:37:09 2025 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.50.80 --slave_ip=192.168.50.80 --slave_port=3306 --workdir=/mha --target_version=8.0.36 --manager_version=0.58 --relay_dir=/var/lib/mysql --current_relay_log=mysql-relay-bin.000002  --slave_pass=xxx
Fri Feb 14 19:37:09 2025 - [info]   Connecting to mhauser@192.168.50.80(192.168.50.80:22).. 
  Checking slave recovery environment settings..
    Relay log found at /var/lib/mysql, up to mysql-relay-bin.000002
    Temporary relay log file is /var/lib/mysql/mysql-relay-bin.000002
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Feb 14 19:37:10 2025 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.50.90 --slave_ip=192.168.50.90 --slave_port=3306 --workdir=/mha --target_version=8.0.36 --manager_version=0.58 --relay_dir=/var/lib/mysql --current_relay_log=localhost-relay-bin.000002  --slave_pass=xxx
Fri Feb 14 19:37:10 2025 - [info]   Connecting to mhauser@192.168.50.90(192.168.50.90:22).. 
  Checking slave recovery environment settings..
    Relay log found at /var/lib/mysql, up to localhost-relay-bin.000002
    Temporary relay log file is /var/lib/mysql/localhost-relay-bin.000002
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Feb 14 19:37:10 2025 - [info] Slaves settings check done.
Fri Feb 14 19:37:10 2025 - [info] 
192.168.50.70(192.168.50.70:3306) (current master)
 +--192.168.50.80(192.168.50.80:3306)
 +--192.168.50.90(192.168.50.90:3306)

Fri Feb 14 19:37:10 2025 - [info] Checking replication health on 192.168.50.80..
Fri Feb 14 19:37:10 2025 - [info]  ok.
Fri Feb 14 19:37:10 2025 - [info] Checking replication health on 192.168.50.90..
Fri Feb 14 19:37:10 2025 - [info]  ok.
Fri Feb 14 19:37:10 2025 - [info] Checking master_ip_failover_script status:
Fri Feb 14 19:37:10 2025 - [info]   /mha/scripts/master_ip_failover --command=status --ssh_user=mhauser --orig_master_host=192.168.50.70 --orig_master_ip=192.168.50.70 --orig_master_port=3306 
Fri Feb 14 19:37:10 2025 - [info]  OK.
Fri Feb 14 19:37:10 2025 - [warning] shutdown_script is not defined.
Fri Feb 14 19:37:10 2025 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

맨 마지막에 'MySQL Replication Health is OK' 라고 뜨면 정상적으로 실행 가능하다.

[MHA를 Manager Server에서 실행 (start)]

nohup masterha_manager --conf=/etc/mha.cnf --last_failover_minute=1 &

or

start (이건 내가 단축키로 지정해서 사용할 수 있음)

MHA 실행
MHA가 실행중인지 상태 확인

Master (192.168.50.70)를 stop하기

systemctl stop mysqld

Manager에서 기록 확인하기 (tail -f /mha/manager.log) ↓

기존에 Slave였던 192.168.50.80이 Master로 자동 승격되었다.

→ Slave 2의 Master IP가 192.168.50.70에서 192.168.50.80으로 변경되었다.

grep "All other slaves should start replication from here" /mha/manager.log

→ MHA를 구동시켜 Slave가 Master로 승격되면 Master의 상태정보를 확인할 수 있다.

여기서 마지막 줄에 나오는 정보가 Master의 최신 상태이므로 복구가 된 장비를 Slave로 연결할 때 사요하면 된다.

→ 장애가 발생한 기존의 Master를 복구한 후에 Slave로 사용하기 위해 새로운 Master와 이중화를 구성하였다


이제 기존 Master는 Slave가 되었고 기존 Slave는 Master가 된다. 하지만 기존 Master가 복구되어서 다시 Master로 복구하려면 내가 수동으로 Master-Slave를 기존 설정처럼 다시 해줘야 된다.


[참고]

https://rachel0115.tistory.com/entry/MariaDB-Replication-%EC%A0%81%EC%9A%A9%EA%B8%B0-4-MHA

 

[MariaDB] Replication 적용기 - 4 (MHA)

개요 이전 실습에서 MariaDB Replication 구성과 LazyConnectionDatasourceProxy를 활용하여 읽기 / 쓰기 트랜잭션의 Connection을 분리하고 반동기 복제 적용까지 진행하였다. 이를 통해 데이터 복제를 통한 백업

rachel0115.tistory.com

https://engmisankim.tistory.com/61

 

MySQL MHA(Master High Availability) 설정

지난 DRBD를 이용한 MySQL HA 구성 관련 글과 AWS Aurora 스토리지 관련 글에서 MySQL 리플리케이션의 경우 비동기 복제 방식이기 때문에 데이터의 일관성을 보장하지는 않는다는 내용을 많이 언급했습

engmisankim.tistory.com

https://onestone-note.tistory.com/34

 

[MariaDB] MHA + Replication 구성하기

1. 사전 준비 환경 CentOS 7 + MariaDB 10.2 + MHA 0.57 조합으로 구성 서버 (3개), ip는 vip로 구성해도 될 것으로 보임. MHA Manager Server 192.168.0.150 (mgr) Master DB Server 192.168.0.151 (masterdb) Slave DB Server 192.168.0.152 (sl

onestone-note.tistory.com