MariaDB(Mysql) MHA
MariaDB(Mysql) MHA๋?
MHA๋?
Master DB๊ฐ ์ฅ์ ๋ก ์๋น์ค๊ฐ ๋ถ๊ฐ๋ฅํ ์ํ๊ฐ ๋๋ฉด, ์๋์ผ๋ก Failover๋ฅผ ์ํํ์ฌ Slave DB๋ฅผ Master DB๋ก ์น๊ฒฉ์์ผ ์๋น์ค ๋ค์ดํ์์ ์ต์ํํ๋ auto failover ์๋ฃจ์ ์ด๋ค.
MHA ์๋ฒ ๊ธฐ๋ณธ๊ตฌ์ฑ
MHA Manager, Master, Slave ์๋ฒ ์ด 3๊ฐ๊ฐ ๊ธฐ๋ณธ ๊ตฌ์ฑ์ด๋ค.
๊ตฌ๋ถ | ์ค๋ช |
---|---|
Manager | ๋ชจ๋ DB์ Alive๋ฅผ ์ฒดํฌํ๊ณ Failover, Master Switch ๋ฑ์ ์ฒ๋ฆฌ |
Master | ๋ฐ์ดํฐ์ write, read๊ฐ ๋ชจ๋ ๋ฐ์๋๋ ๋ฉ์ธ DB |
Slave | Master์ ๋ถํ ๋ถ์ฐ์ ์ํด read_only ๋ DB |
MHA ์ ์ฅ์
-
์ต์ํ์ Down Time์ผ๋ก Master์ ์ฅ์ ์ฒ๋ฆฌ ๋ฐ Slave์ ์๋ก์ด Master๋ก ๋ณ๊ฒฝ ์ํ ๊ฐ๋ฅ
- Master์ ์ฅ์ ๋ก ๊ฐ ๋ ธ๋(Master ๋ฐ Slave)์ ๋ฐ์ดํฐ ๋ถ์ผ์น๊ฐ ๋ฐ์ํ์ง ์์
- ํ์ฌ MySQL ์๋ฒ์ ์ค์ ์ ๋ณ๊ฒฝ ํ ํ์๊ฐ ์์ (MySQL 5.0์ด์)
- ์๋ฒ๋ฅผ ๋ง์ด ๋๋ฆด ํ์๊ฐ ์์ (MySQL Cluster ๋๋น)
- ์คํ ๋ฆฌ์ง ์์ง์ ์ ์ฝ์ ๋ฐ์ง ์์
- MySQL ์ฑ๋ฅ์ ์ ํ ์ ์ฝ ์ฌํญ์ด ์์
MHA์ ์ฅ์ ์ฒดํฌ
MHA Manager๊ฐ 3์ด๋ง๋ค ๋ง์คํฐ DB๋ฅผ CONNECT / SELECET / INSERT ์ฒดํฌํ๋ฉฐ 3ํ ์คํจ์ ์ฅ์ ๋ก ์ธ์ํ๊ณ Failover๋ฅผ ์ํ
MHA์ Replication
- Mysql์์๋ Lossless Replication์ ์ง์ ํ๋๋ฐ ์ด๋ Master์์ ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ด ๋๋ฉด Slave ์ด๋๊ฐ์ ๋ฐ๋์ ๋ณ๊ฒฝ์ด๋ ฅ(relay log)์ด ๋จ์์๋ค๋ ๊ฒ์ ๋ณด์ฅ
- MHA์์๋ ์ฅ์ ๋ฐ์์ ๊ฐ์ฅ ์ต๊ทผ์ ๋ณ๊ฒฝ๋ Slave๋ฅผ Master DB๋ก ์น๊ฒฉ ์ํจ ํ Lossless Replication ๋ฅผ ํตํ relay log๋ก ๋ฐ์ดํฐ ๋ณต๊ตฌ๋ฅผ ์ํ
- ์ฅ์ ๋ฐ์๋ถํฐ Master DB ์น๊ฒฉ, ๋ฐ์ดํฐ ๋ณต๊ตฌ ๊น์ง ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ํต์ 10~30์ด
MHA์ FailOver
Master DB์ ์ฅ์ ๊ฐ ๋ฐ์ํ๋ฉด MHA Manager๋ Master์ฅ๋น์ VIP๋ฅผ down์ํจ๋ค. ์ดํ Replication์ ์งํ ํ down ์ํจ VIP๋ฅผ Slave์ฅ๋น์์ ์ฌ๋ ค Failover๋ฅผ ์งํ
MHA + Replication ๊ตฌ์ฑํ๊ธฐ
์ฐ์ Master, Slave ์๋ฒ์ Replication ๋ถํฐ ๊ตฌ์ฑํ๋ค.
Replication
/etc/hosts
OS ํ๊ฒฝ์ Ubuntu 18.04 ๋ฒ์ ์ด๋ค.
/etc/hosts ๊ตฌ์ฑ์ ์๋์ ๊ฐ๋ค.
192.168.100.125 mha-manager # Manager IP
192.168.100.126 server1 # Master IP
192.168.100.127 server2 # Slave IP
192.168.100.128 mha-master-vip # VIP
hosts ํ์ผ์ ๋ณด๋ฉด ์๊ฒ ์ง๋ง VIP๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑํ ๊ฒ์ด๋ค. VIP์ ๋ํ ์ค๋ช ์ ์๋ ๋งํฌ๋ฅผ ์ฐธ์กฐํ๋ค.
๊ธฐ๋ณธ ํจํค์ง ์ค์น (๋งค๋์ , ์๋ฒ1, ์๋ฒ2)
ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํด ์ค๋ค.
apt-get install libdbd-mysql-perl
apt-get install libconfig-tiny-perl
apt-get install libparams-validate-perl
apt-get install libparallel-forkmanager-perl
apt-get install liblog-dispatch-perl
apt-get install libtime-hires-perl
apt-get install make
apt-get install libnet-telnet-perl
apt-get install libmodule-install-perl
MariaDB Master / Slave ๊ตฌ์ฑ
DB ์ค์น (์๋ฒ1, ์๋ฒ2)
MariaDB๋ฅผ ์ค์นํ๋ค
$ apt-get install mariadb-server
$ apt-get install mariadb-client
$ service mysql start
root ํจ์ค์๋ ์ค์ ํ ์ ์ ํ ์คํธ
$ mysql_secure_installation
Enter current password for root (enter for none): enter
Set root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] n
Reload privilege tables now? [Y/n] y
$ mysql -u root -p
>
์ธ๋ถ ์ ์์ด ๊ฐ๋ฅํ๋๋ก ํ๊ธฐ ์ํด ์๋ ์ค์ ๋ ์ฃผ์์ฒ๋ฆฌ ํด ์ค๋ค. (DB ์๋ฒ ์ค์ ํ์ผ์ ํด๋น ๊ฒฝ๋ก์ ๋ค๋ฅผ ์ ์๋ค.)
$ vim /etc/mysql/mariadb.conf.d/50-server.cnf
<before>
bind-address = 127.0.0.1
<after>
#bind-address = 127.0.0.1
์ ์ ์์ฑ (์๋ฒ1, ์๋ฒ2)
Replication ์ ์ ์์ฑ
> create user 'rep'@'%' identified by 'Rep123!';
> grant replication slave on *.* to 'rep';
> flush privileges;
MHA ์ ์ ๋ ๋ฏธ๋ฆฌ ์์ฑํด ์ฃผ์. ์ฌ๊ธฐ์, mha ๊ณ์ ์ ํน์๋ฌธ์๋ ๋ค์ด๊ฐ์ง ์๋๋ก ํ๋ค.
> grant all privileges on *.* to 'mha'@'%' identified by 'Mha123';
> flush privileges;
Replication ํ๊ฒฝ ์ค์
์ค์ ์ ์ํด DB ์๋น์ค๋ฅผ ์ค์งํ๋ค. (์๋ฒ1, ์๋ฒ2)
$ service mysql stop
DB ์๋ฒ ์ค์ ํ์ผ์์ [mariadb]
๋ถ๋ถ์ ๋ค์๊ณผ ๊ฐ์ด ์ถ๊ฐํ๋ค. (DB ์๋ฒ ์ค์ ํ์ผ์ ํด๋น ๊ฒฝ๋ก์ ๋ค๋ฅผ ์ ์๋ค.) (๊ฐ ์๋ฒ์ ๋ง๋ ์ค์ ์
๋ ฅ)
$ vim /etc/mysql/mariadb.conf.d/50-server.cnf
# ์๋ฒ1 (Master) ์์
[mariadb]
server_id=1
log-bin=mysql-bin
# ์๋ฒ2 (Slave) ์์
[mariadb]
server_id=2
log-bin=mysql-bin
read_only=1
relay_log_purge=0
๋ค์ DB ์๋น์ค ์คํ (์๋ฒ1, ์๋ฒ2)
$ service mysql start
Master ์๋ฒ์์ ๋ก๊ทธํ์ผ๊ณผ ์์น ํ์ธ (์๋ฒ1)
> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 313
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
Slave ์๋ฒ์์ Replication ์ธํ (์๋ฒ2)
> CHANGE MASTER TO
-> MASTER_HOST='server1', -- ํ์ฌ ๋ง์คํฐ์๋ฒ host๋ช
-> MASTER_USER='rep', -- ํ์ฌ ๋ง์คํฐ์๋ฒ replication ๊ณ์ user
-> MASTER_PASSWORD='Rep123!', -- ํ์ฌ ๋ง์คํฐ์๋ฒ replication ๊ณ์ password
-> MASTER_LOG_FILE='mysql-bin.000001', -- ํ์ฌ ๋ง์คํฐ์๋ฒ์ logfile(show master status\G ์ฐธ์กฐ)
-> MASTER_LOG_POS=313; -- ํ์ฌ ๋ง์คํฐ์๋ฒ์ log ํฌ์ง์
(show master status\G ์ฐธ์กฐ)
> start slave;
> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: server1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 313
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 537
Relay_Master_Log_File: mysql-bin.000001
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: 313
Relay_Log_Space: 836
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: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
1 row in set (0.00 sec)
show slave status\G
์ ๊ฒฐ๊ณผ์์ Slave_IO_State, Slave_IO_Running, Slave_SQL_Running์ด ์์ ๊ฐ์ ์ํ์ฌ์ผ ํ๋ค.
์ค์ Replication์ด ๋์ํ๋์ง Master ์๋ฒ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ ์ด๋ธ์ ์์ฑํ๊ณ Slave ์๋ ์์ฑ๋์๋์ง ํ์ธํด ๋ณธ๋ค.
MHA
Replication ๊ตฌ์ฑ์ด ์๋ฃ๊ฐ ๋์๋ค๋ฉด, MHA๋ฅผ ์ค์นํ์ฌ Master, Slave๋ฅผ ๋ชจ๋ํฐ๋ง, Failover ๋ฑ์ ๊ธฐ๋ฅ์ ์ํํด ๋ณด์.
MHA ํ๊ฒฝ ๊ตฌ์ฑ
๋๋ ํ ๋ฆฌ ๋ฐ ์ ์ ์์ฑ (๋งค๋์ , ์๋ฒ1, ์๋ฒ2)
MHA ๋งค๋์ ๊ฐ ์ฌ์ฉํ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ๋ค. ์์น๋ ์ํ๋ ๋๋ก.
$ mkdir /data/mariadb_mha
MHA ์ ์ ๋ ์์ฑํด ์ค๋ค.
$ useradd -g mysql -d /home/mha -m -s /bin/bash mha
$ passwd mha
$ chown -R mha:mysql /data/mariadb_mha
MHA node ์ค์น (๋งค๋์ , ์๋ฒ1, ์๋ฒ2)
$ mkdir /data/mariadb_mha/source
$ cd /data/mariadb_mha/source
$ wget http://cliel.com/data/mha4mysql-node-0.57.tar.gz
$ tar -xvzf mha4mysql-node-0.57.tar.gz
$ cd mha4mysql-node-0.57
$ perl Makefile.PL
$ make
$ make install
MHA Manager ์ค์น (๋งค๋์ )
$ cd /data/mariadb_mha/source
$ wget http://cliel.com/data/mha4mysql-manager-0.57.tar.gz
$ tar -xvzf mha4mysql-manager-0.57.tar.gz
$ cd mha4mysql-manager-0.57
$ perl Makefile.PL
$ make
$ make install
visudo ์ค์ (๋งค๋์ , ์๋ฒ1, ์๋ฒ2)
mha ๊ณ์ ์ด sudo ๊ถํ์ ๋ํด ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํ์ง ์๋๋ก ํ๊ธฐ ์ํด ์๋ ์ค์ ์ ํด์ค๋ค.
$ visudo
# ๋งจ ์๋ ์ถ๊ฐ
mha ALL = (ALL) NOPASSWD: ALL
ssh-keygen (๋งค๋์ , ์๋ฒ1, ์๋ฒ2)
๊ฐ ์๋ฒ์ ssh ํต์ ์ ๋น๋ฐ๋ฒํธ๊ฐ ์์ด ํต์ ํ ์ ์๋๋ก ๊ณต๊ฐํค ์ค์ ์ ํ๋ค.
# ํค ์์ฑ
$ su - mha
$ ssh-keygen -t rsa
enter
enter
enter
# ํค ๋ณต์ฌ
$ ssh-copy-id -i ~/.ssh/id_rsa.pub mha@192.168.100.125
$ ssh-copy-id -i ~/.ssh/id_rsa.pub mha@192.168.100.126
$ ssh-copy-id -i ~/.ssh/id_rsa.pub mha@192.168.100.127
# ์ฐ๊ฒฐ ํ
์คํธ
$ ssh 192.168.100.125
$ ssh 192.168.100.126
$ ssh 192.168.100.127
# .ssh ๊ถํ ์์
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/id_rsa
$ chmod 644 ~/.ssh/id_rsa.pub
$ chmod 644 ~/.ssh/authorized_keys
$ chmod 644 ~/.ssh/known_hosts
MHA์์ ์ฌ์ฉํ config ํ์ผ ์์ฑ (๋งค๋์ )
๊ฐ ์ค์ ๊ฒฝ๋ก์ ์ญํ ์ ๊ฐ ๋ณ์ ๋ช
์ ๋ณด๋ฉด ์ ์ ์๋ค. manager_log
์ ๊ฒฝ๋ก๋ฅผ ํนํ ์ ๊ธฐ์ตํด ๋์
$ cp data/mariadb_mha/source/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/mha.cnf
$ vim /etc/mha.cnf
[server default]
user=mha
password=Mha123
ssh_user=mha
repl_user=rep
repl_password=Rep123!
manager_workdir=/data/mariadb_mha/log/masterha
manager_log=/data/mariadb_mha/log/masterha/MHA.log
remote_workdir=/data/mariadb_mha/log/masterha
master_binlog_dir=/var/lib/mysql
[server1]
hostname=192.168.100.126
candidate_master=1
[server2]
hostname=192.168.100.127
candidate_master=1
์ค์ ํ์ธ (๋งค๋์ )
ssh ํต์ ์ฒดํฌ
$ masterha_check_ssh --conf=/etc/mha.cnf
Thu Jan 5 15:47:58 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jan 5 15:47:58 2023 - [info] Reading application default configuration from /etc/mha.cnf..
Thu Jan 5 15:47:58 2023 - [info] Reading server configuration from /etc/mha.cnf..
Thu Jan 5 15:47:58 2023 - [info] Starting SSH connection tests..
Thu Jan 5 15:48:00 2023 - [debug]
Thu Jan 5 15:47:59 2023 - [debug] Connecting via SSH from mha@192.168.100.127(192.168.100.127:22) to mha@192.168.100.126(192.168.100.126:22)..
Thu Jan 5 15:48:00 2023 - [debug] ok.
Thu Jan 5 15:48:00 2023 - [debug]
Thu Jan 5 15:47:58 2023 - [debug] Connecting via SSH from mha@192.168.100.126(192.168.100.126:22) to mha@192.168.100.127(192.168.100.127:22)..
Thu Jan 5 15:48:00 2023 - [debug] ok.
Thu Jan 5 15:48:00 2023 - [info] All SSH connection tests passed successfully.
Replication ์ค์ ์ฒดํฌ
$ masterha_check_repl --conf=/etc/mha.cnf
Thu Jan 5 15:49:39 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jan 5 15:49:39 2023 - [info] Reading application default configuration from /etc/mha.cnf..
Thu Jan 5 15:49:39 2023 - [info] Reading server configuration from /etc/mha.cnf..
Thu Jan 5 15:49:39 2023 - [info] MHA::MasterMonitor version 0.57.
Thu Jan 5 15:49:40 2023 - [info] GTID failover mode = 0
...
Thu Jan 5 15:49:46 2023 - [info] Checking replication health on 192.168.100.127..
Thu Jan 5 15:49:46 2023 - [info] ok.
Thu Jan 5 15:49:46 2023 - [info] Checking master_ip_failover_script status:
Thu Jan 5 15:49:46 2023 - [info] /data/mariadb_mha/scripts/master_ip_failover --command=status --ssh_user=mha --orig_master_host=192.168.100.126 --orig_master_ip=192.168.100.126 --orig_master_port=3306
Thu Jan 5 15:49:46 2023 - [info] OK.
Thu Jan 5 15:49:46 2023 - [warning] shutdown_script is not defined.
Thu Jan 5 15:49:46 2023 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
Replication ์ค์ ์ฒดํฌ ์ ์๋ฌ ๋ฐ์
ํ์๋
masterha_check_repl
์ฒดํฌ ์์ ์๋ 2๊ฐ์ง ์๋ฌ๊ฐ ๋ฐ์ํ์๋ค. ๊ทธ์ ๋ํ ํด๊ฒฐ์ฑ ์ ์ ๋ฆฌํ์๋ค.1. MHA NodeUtil.pm ๊ด๋ จ version ์๋ฌ
์๋ฌ ๋ฌธ๊ตฌ๋ ์๋์ ๊ฐ๋ค.
$ masterha_check_repl --conf=/etc/mha.cnf Tue Dec 27 01:52:44 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Dec 27 01:52:44 2022 - [info] Reading application default configuration from /etc/mha.cnf.. Tue Dec 27 01:52:44 2022 - [info] Reading server configuration from /etc/mha.cnf.. Tue Dec 27 01:52:44 2022 - [info] MHA::MasterMonitor version 0.57. Tue Dec 27 01:52:45 2022 - [error][/usr/local/share/perl/5.26.1/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. Redundant argument in sprintf at /usr/local/share/perl/5.26.1/MHA/NodeUtil.pm line 184. Tue Dec 27 01:52:45 2022 - [error][/usr/local/share/perl/5.26.1/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers. Tue Dec 27 01:52:45 2022 - [info] Got exit code 1 (Not master dead).
NodeUtil.pm ํ์ผ์์ mysql ๋ฒ์ ์ ๊ฐ์ ธ์ค๋๋ฐ ํ์ฑ์๋ฌ๊ฐ ๋ ๊ฒ์ด๋ค.
์๋์ ๊ฐ์ด NodeUtil.pm ํ์ผ์ ์ฝ๋๋ฅผ ์์ ํด ์ค๋ค.
$ vim /usr/local/share/perl/5.26.1/MHA/NodeUtil.pm <before> sub parse_mysql_version($) { my $str = shift; my $result = sprintf( '%03d%03d%03d', $str =~ m/(\d+)/g ); return $result; } sub parse_mysql_major_version($) { my $str = shift; my $result = sprintf( '%03d%03d', $str =~ m/(\d+)/g ); return $result; } <after> sub parse_mysql_version($) { my $str = shift; ($str) = $str =~ m/^[^-]*/g; my $result = sprintf( '%03d%03d%03d', $str =~ m/(\d+)/g ); return $result; } sub parse_mysql_major_version($) { my $str = shift; $str =~ /(\d+)\.(\d+)/; my $strmajor = "$1.$2"; my $result = sprintf( '%03d%03d', $strmajor =~ m/(\d+)/g ); return $result; }
2. mysqlbinlog: unknown variable โdefault-character-set=utf8mb4โ ์๋ฌ
์๋ฌ ๋ฌธ๊ตฌ๋ ์๋์ ๊ฐ๋ค.
Tue Dec 27 04:27:16 2022 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Dec 27 04:27:16 2022 - [info] Reading application default configuration from /etc/mha.cnf.. Tue Dec 27 04:27:16 2022 - [info] Reading server configuration from /etc/mha.cnf.. Tue Dec 27 04:27:16 2022 - [info] MHA::MasterMonitor version 0.57. Tue Dec 27 04:27:17 2022 - [info] GTID failover mode = 0 ... Tue Dec 27 04:27:22 2022 - [info] Connecting to root@192.168.100.127(192.168.100.127:22).. mysqlbinlog: unknown variable 'default-character-set=utf8mb4' mysqlbinlog version command failed with rc 7:0, please verify PATH, LD_LIBRARY_PATH, and client options at /usr/local/bin/apply_diff_relay_logs line 493. Tue Dec 27 04:27:23 2022 - [error][/usr/local/share/perl/5.26.1/MHA/MasterMonitor.pm, ln208] Slaves settings check failed! Tue Dec 27 04:27:23 2022 - [error][/usr/local/share/perl/5.26.1/MHA/MasterMonitor.pm, ln416] Slave configuration failed. Tue Dec 27 04:27:23 2022 - [error][/usr/local/share/perl/5.26.1/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. at /usr/local/bin/masterha_check_repl line 48. Tue Dec 27 04:27:23 2022 - [error][/usr/local/share/perl/5.26.1/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers. Tue Dec 27 04:27:23 2022 - [info] Got exit code 1 (Not master dead).
MySQL 5.5.3 ๋ถํฐ default-character-set ์ด๋ผ๋ ์ต์ ์ด ์์ด์ก๊ธฐ ๋๋ฌธ์ด๋ค.
mysqlbinlog ํ๋ก๊ทธ๋จ์ ๊ธฐ๋ณธ์ ์ผ๋ก my.cnf ๋ฅผ ์ฝ๊ฒ ๋์ด ์๋๋ฐ ์ฌ๊ธฐ์ default-character-set ์ค์ ์ด ์์๊ธฐ ๋๋ฌธ์ ํด๋น ๋ณ์๋ฅผ ์ดํดํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ค.
$ vim /etc/mysql/my.cnf [client-server] # Import all .cnf files from configuration directory !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mariadb.conf.d/
/etc/mysql/my.cnf
ํ์ผ์ ๋ณด๋ฉด ์์ ๊ฐ์ด ๋๊ฐ์ง ๊ฒฝ๋ก์ ๋ชจ๋ conf ํ์ผ์ ๋ณด๊ณ ์๋ค.๊ทธ์ค default-character-set ์ค์ ์ด ๋ค์ด๊ฐ ์๋ ํ์ผ์ ์๋์ ๊ฐ๋ค. 2๊ฐ์ง ํ์ผ์ ์๋์ ๊ฐ์ด ์ฃผ์์ฒ๋ฆฌ ํ์ฌ ์์ ํ๋ค.
$ vim /etc/mysql/mariadb.conf.d/50-client.cnf [client] # Default is Latin1, if you need UTF-8 set this (also in server section) # default-character-set = utf8mb4 $ vim /etc/mysql/mariadb.conf.d/50-mysql-clients.cnf [mysql] # Default is Latin1, if you need UTF-8 set this (also in server section) # default-character-set = utf8mb4
MHA ๊ธฐ๋ฅ ๊ด๋ จ ์คํฌ๋ฆฝํธ ์์ (๋งค๋์ )
MHA Manager ์ค์น ์ ๊ธฐ๋ณธ ์ ๊ณต๋ ์คํฌ๋ฆฝํธ์์ master_ip_failover
, master_ip_online_change
ํ์ผ ์์ ์ด ํ์ํ๋ค.
์ฅ์ ๊ฐ ๊ฐ์ง ๋๋ฉด master_ip_failover
๊ฐ ์ฌ์ฉ๋๋ฉฐ
์ฌ์ฉ์ ์ ์ takeover(relocate) ์ master_ip_online_change
์ด ์ฌ์ฉ๋๋ค.
์ฐ์ 2๊ฐ์ง ์คํฌ๋ฆฝํธ๋ค์ ์์ ํด๋ ์์ฑ ํ ๋ณต์ฌํ๋ค.
$ mkdir /data/mariadb_mha/scripts
$ cp /data/mariadb_mha/source/mha4mysql-manager-0.57/samples/scripts/master_ip_online_change /data/mariadb_mha/scripts
$ cp /data/mariadb_mha/source/mha4mysql-manager-0.57/samples/scripts/master_ip_failover /data/mariadb_mha/scripts
Master IP <-> Slave IP ๋ณ๊ฒฝ ์คํฌ๋ฆฝํธ ์ค์
ํด๋น ๋ผ์ธ์ ์ฝ๋๋ฅผ ์ฃผ์์ฒ๋ฆฌ ํ๋ค.
$ vim /data/mariadb_mha/scripts/master_ip_online_change
...
149 ## Drop application user so that nobody can connect. Disabling per-session binlog beforeha nd
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);
...
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();
...
Failover ์คํฌ๋ฆฝํธ ์ค์
ํด๋น ๋ผ์ธ์ ์ฝ๋๋ฅผ ์ฃผ์์ฒ๋ฆฌ ํ๋ค.
$ vim /data/mariadb_mha/scripts/master_ip_failover
...
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;
...
/etc/mha.cnf ์์
์์์ ์ค์ ํ ๋ ์คํฌ๋ฆฝํธ์ ๋ํ ๊ฒฝ๋ก ์ค์ ์ ํด์ค๋ค.
master_ip_online_change_script
, master_ip_failover_script
์ถ๊ฐ
[server default]
user=mha
password=Mha123
ssh_user=mha
repl_user=rep
repl_password=Rep123!
manager_workdir=/data/mariadb_mha/log/masterha
manager_log=/data/mariadb_mha/log/masterha/MHA.log
remote_workdir=/data/mariadb_mha/log/masterha
master_binlog_dir=/var/lib/mysql
master_ip_online_change_script=/data/mariadb_mha/scripts/master_ip_online_change
master_ip_failover_script=/data/mariadb_mha/scripts/master_ip_failover
[server1]
hostname=192.168.100.126
candidate_master=1
[server2]
hostname=192.168.100.127
candidate_master=1
์ด๋ก์จ MHA ์ค์ ๊น์ง ๋ชจ๋ ๋์ด ๋ฌ๋ค.
VIP ์ค์
์ด๋๋ก ์๋น์คํด๋ ๊ธฐ๋ฅ์ ์ธ ๋ฌธ์ ๋ ์๋ค. ํ์ง๋ง ๋ณด์์ ์ธ ๋ฌธ์ ์ ์ํํ ์๋น์ค ์ ๊ณต์ ์ํ์ฌ VIP ์ค์ ์ ํด ์ฃผ๋๊ฒ์ด ์ข๋ค. (DNS ๋ฅผ ํตํ ์๋น์ค ์ ๊ณต ๋ฐฉ๋ฒ๋ ์์ง๋ง, ์ด ๊ธ์์๋ MHA + VIP๋ก ์์ฑํ์๋ค.)
VIP ํ์ฑํ / ๋นํ์ฑํ (์๋ฒ1)
ifconfig ๋ช ๋ น์ด๋ฅผ ํตํ์ฌ VIP๋ฅผ ํ์ฑํ, ๋นํ์ฑํ ์ํฌ ์ ์๋ค.
ํ์ฌ ์๋ฒ1์ด Master ์ํ์ด๋, ์๋ฒ1์ VIP(192.168.100.128)๋ฅผ ํ์ฑํ ์์ผ ๋์ผ๋ฉด ๋๋ค.
๋ช ๋ น์ด๋ ์๋์ ๊ฐ๋ค.
$ ifconfig
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.126 netmask 255.255.255.0 broadcast 192.168.100.255
...
$ ifconfig eno1:0 192.168.100.50 up
$ ifconfig
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.126 netmask 255.255.255.0 broadcast 192.168.100.255
...
eno1:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.50 netmask 255.255.255.0 broadcast 192.168.100.255
...
VIP ๋ณ๊ฒฝ ์คํฌ๋ฆฝํธ (๋งค๋์ )
Failover๋ Master Switch๊ฐ ๋ ๋ VIP๋ Master ์๋ฒ์ ๋ง๊ฒ down๋๊ณ up์ด ๋์ด์ผ ํ๋ค.
ํด๋น ์คํฌ๋ฆฝํธ๋ ์๋์ ๊ฐ๋ค. (ํ์์ ๋คํธ์ํฌ ๋ช ์ eno1์ด๋ค. ๊ฐ์์ ๋ง๊ฒ ์์ ํ์.)
$ vim /data/mariadb_mha/scripts/mha_change_vip.sh
#!/bin/bash
## Fail-Over VIP Change
V_NEW_MASTER=`cat /etc/hosts | grep $1 | awk '{print $2}'`
V_EXIST_VIP_CHK=`ping -c 1 -W 1 mha-master-vip | grep "packet loss" | awk '{print $6}'`
V_VIP_IP=`cat /etc/hosts | grep mha-master-vip | awk '{print $1}'`
if [ $V_EXIST_VIP_CHK = "0%" ]
then
echo "VIP IS Alive, VIP Relocate $V_NEW_MASTER "
/usr/bin/ssh -o StrictHostKeyChecking=no mha-master-vip /usr/bin/sudo /sbin/ifconfig eno1:0 down &
ssh -o StrictHostKeyChecking=no $V_NEW_MASTER /usr/bin/sudo /sbin/ifconfig eno1:0 $V_VIP_IP netmask 255.255.255.0
ssh -o StrictHostKeyChecking=no $V_NEW_MASTER /usr/sbin/arping -c 5 -D -I eno1 -s $V_VIP_IP $V_VIP_IP
VIP_NOHUP_PS=`ps -ef| grep "ifconfig eno1:0" | grep ssh | grep -v grep | awk '{print $2}'` && kill -9 $VIP_NOHUP_PS
elif [ $V_EXIST_VIP_CHK = "100%" ]
then
echo "VIP IS dead, VIP Relocate $V_NEW_MASTER "
/usr/bin/ssh -o StrictHostKeyChecking=no $V_NEW_MASTER /usr/bin/sudo /sbin/ifconfig eno1:0 $V_VIP_IP netmask 255.255.255.0
/usr/bin/ssh -o StrictHostKeyChecking=no $V_NEW_MASTER /usr/sbin/arping -c 5 -D -I eno1 -s $V_VIP_IP $V_VIP_IP
fi
VIP ๋ณ๊ฒฝ ์คํฌ๋ฆฝํธ ์ถ๊ฐ (๋งค๋์ )
์์ฑํ mha_change_vip.sh
๋ฅผ master_ip_online_change
, master_ip_failover
์ ์ถ๊ฐํด์ค๋ค.
$ vim master_ip_online_change
...
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();
249
250 ## Update master ip on the catalog database, etc
251
252 # Change vip
253 system("/bin/bash /data/mariadb_mha/scripts/mha_change_vip.sh $new_master_ip");
254
255 $exit_code = 0;
...
$ vim master_ip_failover
...
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;
94
95 # Change vip
96 system("/bin/bash /data/mariadb_ha/scripts/mha_change_vip.sh $new_master_ip");
97
98 $exit_code = 0;
...
MHA ํ ์คํธ (๋งค๋์ )
๋ชจ๋ ์ค์ ์ด ์๋ฃ ๋์์ผ๋ฉด ์๋ 2๊ฐ์ง ํ ์คํธ๋ฅผ ํด๋ณธ๋ค.
$ masterha_check_ssh --conf=/etc/mha.cnf
$ masterha_check_repl --conf=/etc/mha.cnf
MHA ๊ตฌ๋ (๋งค๋์ )
MHA๊ฐ ๋ง์คํฐ DB ๋ชจ๋ํฐ๋ง ์ค์ ์ฅ์ ๊ฐ ๊ฐ์ง๋์ด failover ๊ฐ ๋ฐ์ ๋ ์ดํ ์ผ์ ์๊ฐ๋ด์ ์ฅ์ ์ ๋ํด์๋ failover ๊ฐ ์งํ ๋์ง ์๋๋ค. failover ๊ฐ ํ๋ฒ ์ฒ๋ฆฌ ๋ ํ ์ผ์ ์๊ฐ์์๋ failover๊ฐ ๋์ง ์์ผ๋ฉฐ ๊ธฐ๋ณธ ๊ฐ์ 8์๊ฐ์ด๋ค. last_failover_minute์ผ๋ก 8์๊ฐ์ 1๋ถ์ผ๋ก ๋ณ๊ฒฝํ์ฌ ์คํํ์๋ค.
$ masterha_manager --conf=/etc/mha.cnf --last_failover_minute=1 &
$ masterha_check_status --conf=/etc/mha.cnf
mha (pid:8754) is running(0:PING_OK), master:192.168.100.126
status๊ฐ ์์ ๊ฐ์ด ๋ฌ๋ค๋ฉด ์ ์คํ ๋ ๊ฒ์ด๋ค.
๋ก๊ทธ๋ ํ์ธํด ๋ณด์
$ tail -f /data/mariadb_mha/log/masterha/MHA.log
Leave a comment