MySQL 또는 MariaDB의 MHA(Master High Availability) 구성하기

MySQL(MariaDB)의 고가용성(High Availability)는 여러 종류가 있다. Replication, MHA, MMM, Galera, MaxScale과 같은 3rd party 도구들이 있으며, 이 도구들로 MySQL(MariaDB)를 운영하는 사람들에게는 조금이나마 도움이 된다.

MHA는 Perl로 이루어져있으며, 최초 Failover이후 수동으로 동기화 및 서비스를 실행해야하는 단점이 있지만, 매우 빠르게 Failover가 된다는 점에서는 매력적인 도구이다.

구성환경

  • 수량: 3 EA
  • OS: CentOS 7
  • DBMS: MySQL 또는 MariaDB
  • 192.168.0.4 server1
  • 192.168.0.5 server2
  • 192.168.0.6 manager
  • 192.168.0.10 vip
  1. 필요한 도구 설치
yum install epel-release -y
yum install sysstat wget lrzsz lsof htop iftop rsync bzip2 unzip -y
  1. hosts 파일 설정
vi /etc/hosts
127.0.0.1 localhost
192.168.0.4 server1
192.168.0.5 server2
192.168.0.6 manager
  1. MHA 필요 패키지 설치
  • Node에 필요한 패키지
yum install perl-DBD-MySQL -y
  • Manager에 필요한 패키지
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager
  1. MySQL(MariaDB) 설치
  • MySQL 설치
rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
yum install mysql-community-server mysql-community-client
  • MariaDB 설치
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
yum install MariaDB-server MariaDB-client
  1. my.cnf 설정
  • server 1 설정
vi /etc/my.cnf
server_id=1
skip-name-resolve
log-bin=/var/lib/mysql/mysql-bin
relay-log=/var/lib/mysql/mysql-relay-bin
relay-log-index/var/lib/mysql/mysql-relay-bin.index
  • server 2 설정
vi /etc/my.cnf
server_id=2
skip-name-resolve
read_only=1
log-bin=/var/lib/mysql/mysql-bin
relay-log=/var/lib/mysql/mysql-relay-bin
relay-log-index/var/lib/mysql/mysql-relay-bin.index
  1. MHA 및 Replication 계정 생성
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.0.%' identified by 'password';
CREATE USER 'rep'@'192.168.0.%' identified by 'password';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* to 'rep'@'192.168.0.%'
  1. Replication 설정
  • server1
show master status\G;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      327 |              |                  | 
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
  • server2
CHANGE MASTER TO
MASTER_HOST='192.168.0.5',
MASTER_PORT=3306,
MASTER_USER=mha,
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=327;
  1. MHA 설정
vi /etc/masterha_default.cnf
[server default]
user=mha
password='password'

ssh_user=root

manager_workdir=/usr/local/masterha/manager
manager_log=/usr/local/masterha/log/mha.log
remote_workdir=/usr/local/masterha/remote

master_ip_failover_script=/usr/local/masterha/bin/master_ip_failover
master_ip_online_change_script=/usr/local/masterha/bin/master_ip_online_change

[server1]
hostname=192.168.0.4

[server2]
hostname=192.168.0.5
  1. MHA 설치
  • server1 및 server2
cd /usr/local/src
tar zxf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make
make install
  • manager
cd /usr/local/src
tar zxf mha4mysql-node-0.57.tar.gz
tar zxf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make
make install
cd ..
cd mha4mysql-manager-0.57
perl Makefile.PL
make
make install

madir /usr/local/masterha/
madir /usr/local/masterha/bin
madir /usr/local/masterha/manager
madir /usr/local/masterha/log
madir /usr/local/masterha/remote
  1. MHA 스크립트 작성
cd /usr/local/masterha/bin
vi 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,
        $new_master_user,
        $new_master_password
);
 
my $vip = '192.168.0.10';  # Virtual IP
my $netmask = '255.255.255.0';
my $broadcast = '192.168.0.255';
my $key = "0";
my $ssh_start_vip   = "/sbin/ifconfig eth0:$key $vip netmask $netmask broadcast $broadcast up";
my $ssh_stop_vip    = "/sbin/ifconfig eth0:$key down";
my $ssh_mac_refresh = "/sbin/arping -c3 -D -I eth0:0 -s 192.168.0.10 192.168.0.10"; 

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 {
 
 print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
 
 if ( $command eq "stop" || $command eq "stopssh" ) {
 
        print $command;
 
     # $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 \"`;
        `ssh $ssh_user\@$new_master_host \" $ssh_mac_refresh\"`;
}
# 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 755 master_ip_failover
vi mha
nohup /usr/bin/masterha_manager --conf=/etc/masterha_default.cnf --ignore_last_failover < /dev/null > /usr/local/managerha/log/mha.log 2>&1 &
chmod 755 mha

여기까지하면 MHA 구성이 완료된다. 이후에는 Failover에 대한 TEST를 하면 되며, 복구시에는 다음과 같이 진행하면 된다.

  • mha.log 에서 동기화 구간 확인
cd /usr/local/managerha/log
vi mha.log
/All\ other

[info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.0.5', MASTER_PORT=3306, ASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=342, MASTER_USER='rep', MASTER_PASSWORD='xxx';
  • server1의 MySQL(MAriaDB) 정상화 후 현재 Master(구 Slave)와 동기화
mysql -uroot -p
CHANGE MASTER TO MASTER_HOST='192.168.0.5', MASTER_PORT=3306, ASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=342, MASTER_USER='rep', MASTER_PASSWORD='password';
  • manager 폴더에서 mha.failover.complete 파일을 삭제
cd /usr/local/managerha/manager
rm ./mha.failover.complete
  • manager에서 mha 실행
/usr/local/managerha/bin/mha
  • slave 중지

  • 1)~4) 까찌 반복 2)에서 Slave에서 Replication 설정 진행

이렇게 하면 MHA 구성과 Failover, 복구까지 잘 되는 것을 확인할 수 있다.