1. 程式人生 > >MHA叢集(涉及mysql安裝,主從同步,半同步複製...)

MHA叢集(涉及mysql安裝,主從同步,半同步複製...)

MHA(Master High Availablity)-資料庫高可用叢集

由日本DeNA公司開發的一套實現mysql高可用的解決方案,可以保障資料庫自動故障切換操作在0~30s之內,可以確保切換過程中資料的一致,實現真正意義上的高可用。也就是在A主機上訪問資料,中途伺服器故障,客戶可以在繼續在B主機上訪問,而且資料同步。

MHA構成(每組要求至少是1主2從)
MHA Manager(管理節點) MHA Node(資料節點)
單獨部署在一臺獨立的機器,管理其他起點 執行在每臺MySQL伺服器

也可以部署在slave節點上

 

配置MHA

第一部分:環境準備

準備7臺伺服器:客戶端client -192.168.4.50,MHA_manager 192.168.4.56 ,資料庫伺服器192.168.4.51-192.168.4.55,見下圖

安裝perl依賴包(在51-56機器上,僅以51為例)

[[email protected] ~]# cd mha-soft-student/
[[email protected] mha-soft-student]# ls
app1.cnf
master_ip_failover
mha4mysql-manager-0.56.tar.gz
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
perl-Mail-Sendmail-0.79-21.el7.art.noarch.rpm
perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[
[email protected]
mha-soft-student]# yum -y install perl-*.rpm [[email protected] ~]#yum -y install perl-* //安裝光碟上的perl包

所有資料庫伺服器之間實現ssh免密登入(51-55)

[[email protected] ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):      //回車就可以,預設儲存路徑
Created directory '/root/.ssh'.
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:rJOgGlx4twTN9ds3BmPdNe5gL455AyWVKe635rqN2DM 
[email protected]
The key's randomart image is: +---[RSA 2048]----+ | . .o..| | o . . .ooo o| | . o ..=.= o | | . . . +.* + | | . o.o S..o = o | |. o.o..o .B.o | |... .+ o.+. | | o . oE+o. | |. . =Bo | +----[SHA256]-----+ [[email protected]~]# for i in 192.168.4.{52,53,54,55} > do > ssh-copy-id $i > done

配置MHA_Manager (192.168.4.56)免密登入所有資料庫伺服器(192.168.4.51-55)

[[email protected] ~]# ssh-keygen
[[email protected] ~]#for i in 192.168.4.{52,53,54,51,55}; do ssh-copy-id $i; done

在所有資料庫伺服器上安裝mha-node包(51-55),安裝mysql(為了實驗簡便,可以更改密碼許可權,這裡略)

[[email protected] ~]# cd mha-soft-student/
[[email protected] mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm 
準備中...                          ################################# [100%]
正在升級/安裝...
   1:mha4mysql-node-0.56-0.el6        ################################# [100%]
[[email protected] ~]#tar -xf mysql-5.7.17.tar
[[email protected] ~]rpm -Uvh mysql-community-*.rpm
[[email protected]~]#systemctl start mysqld
[[email protected]~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
[[email protected]~]#systemctl restart mysqld   

在管理主機(56)上先安裝mha-node包,後安裝mha4mysql-manager-0.56.tar.gz

[[email protected] ~]# cd mha-soft-student/
[[email protected] mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm 
準備中...                          ################################# [100%]
正在升級/安裝...
   1:mha4mysql-node-0.56-0.el6        ################################# [100%]
[[email protected] mha-soft-student]# tar -zxf mha4mysql-manager-0.56.tar.gz 
[[email protected] mha-soft-student]# cd mha4mysql-manager-0.56/
[[email protected] mha4mysql-manager-0.56]# ls
AUTHORS  COPYING  inc  Makefile.PL  META.yml  rpm      t
bin      debian   lib  MANIFEST     README    samples  tests
[[email protected] mha4mysql-manager-0.56]# perl Makefile.PL     //類似於c程式設計裡的./configure
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- DBI                   ...loaded. (1.627)
- DBD::mysql            ...loaded. (4.023)
- Time::HiRes           ...loaded. (1.9725)
- Config::Tiny          ...loaded. (2.14)
- Log::Dispatch         ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst        ...loaded. (0.56)
*** Module::AutoInstall configuration finished.            //配置完成
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::manager
Writing MYMETA.yml and MYMETA.json
[[email protected] mha4mysql-manager-0.56]# make
[[email protected] mha4mysql-manager-0.56]# make install

第二部分:配置MHA叢集環境

配置資料庫伺服器192.168.4.51(master)

[[email protected]~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=51
log_bin=master51
binlog-format="mixed"
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master-enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off                          #不自動刪除日誌
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# mysql -uroot -p123456
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 |     154  |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> grant replication slave on *.* to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.03 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 |      441 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

配置資料庫伺服器192.168.4.52(備用master1),192.168.4.53(備用master2)

[[email protected]~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=52
log_bin=master52
binlog-format="mixed"
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master-enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off                          #不自動刪除日誌
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# mysql -uroot -p123456
mysql> change master to 
    -> master_host="192.168.4.51",
    -> master_user="repluser",
    -> master_password="123456",
    -> master_log_file="master51.000001",
    -> master_log_pos=154;                      //設定偏移量為初始154,則可以同步使用者
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
...
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
mysql> show grants for [email protected]"%";
+--------------------------------------------------+
| Grants for [email protected]%                            |
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' |
+--------------------------------------------------+
1 row in set (0.00 sec)
[[email protected]~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=53
log_bin=master53
...                                               //以下所有操作同52
...             

配置從資料庫伺服器192.168.4.54和192.168.4.55

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=54
plugin-load="rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_slave_enabled=1
relay_log_purge=off
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.51 sec)
mysql> start slave;
Query OK, 0 rows affected (0.10 sec)

mysql> show slave status\G;
...
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
mysql> show grants for [email protected]"%";
+--------------------------------------------------+
| Grants for [email protected]%                            |
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' |
+--------------------------------------------------+
1 row in set (0.00 sec)
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
server_id=55                                   //其餘同54
...   

第三部分:配置MHA-Manager管理伺服器(192.168.4.56)

修改配置檔案

[[email protected] samples]# mkdir /etc/mha
[[email protected] samples]# pwd
/root/mha-soft-student/mha4mysql-manager-0.56/samples
[[email protected] samples]# cp conf/app1.cnf  /etc/mha/       //拷貝模板
[[email protected] samples]# ls /etc/mha/app1.cnf 
/etc/mha/app1.cnf
[[email protected] samples]# vim /etc/mha/app1.cnf 
[server default]
manager_workdir=/etc/mha
manager_log=/etc/mha/manager.log

ssh_user=root
ssh_port=22

repl_user=repluser                                 //主從同步使用者
repl_password=123456

user=root                                          //資料庫使用者名稱
password=123456

[server1]
hostname=192.168.4.51
candidate_master=1

[server2]
hostname=192.168.4.52
candidate_master=1

[server3]
hostname=192.168.4.53
candidate_master=1

[server4]
hostname=192.168.4.54
no_master=1

[server5]
hostname=192.168.4.55
no_master=1


根據配置檔案的配置做對應的設定

1.在51上授權使用者

[[email protected] ~]# mysql -uroot -p123456                   //登入主庫授權資料庫使用者
mysql> grant replication slave on *.* to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.03 sec)

2.在56上配置vip及自動更換master指令碼

[[email protected] ~]# ls mha-soft-student/
master_ip_failover           ...
[[email protected] ~]# cp  /root/mha-soft-student/master_ip_failover  /etc/mha/
[[email protected] ~]# vim /etc/mha/master_ip_failover
...
my $vip = '192.168.4.100/24';  # Virtual IP 
...
[[email protected] ~]# chmod 755 /etc/mha/master_ip_failover 
vim /etc/mha/app1.cnf
[[email protected] ~]#[server default]
...
master_ip_failover_script=/etc/mha/master_ip_failover     #自動failover的指令碼
[[email protected] ~]# ifconfig eth0:1 192.168.4.100/24        //臨時配置vip在51上
[[email protected] ~]# ifconfig eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.4.100  netmask 255.255.255.0  broadcast 192.168.4.255
        ether 52:54:00:0d:20:84  txqueuelen 1000  (Ethernet)

檢測配置

在56管理伺服器上檢測/etc/mha/app1.conf配置

1.檢測ssh免密登入是否成功

[[email protected] ~]# masterha_
masterha_check_repl       masterha_conf_host        masterha_master_switch    
masterha_check_ssh        masterha_manager          masterha_secondary_check  
masterha_check_status     masterha_master_monitor   masterha_stop             
[[email protected] ~]# masterha_check_ssh  --conf=/etc/mha/app1.cnf
...
Mon Dec 17 17:36:15 2018 - [info] All SSH connection tests passed successfully
//證明ssh免密成功

2.檢測主從同步配置

[[email protected] ~]# masterha_check_repl  --conf=/etc/mha/app1.cnf
...
MySQL Replication Health is OK.                            //配置成功

啟動管理服務

[[email protected] ~]# masterha_check_status  --conf=/etc/mha/app1.cnf   
app1 is stopped(2:NOT_RUNNING).
[[email protected] ~]# masterha_manager --conf=/etc/mha/app1.cnf  --remove_dead_master_conf   --ignore_last_failover  
//啟動服務,刪除宕機主庫配置,忽略xxx.health檔案
Mon Dec 17 17:54:32 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 17 17:54:32 2018 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Dec 17 17:54:32 2018 - [info] Reading server configuration from /etc/mha/app1.cnf..

//會佔用一個終端,需另開一個終端檢視狀態
[[email protected] ~]# masterha_check_status  --conf=/etc/mha/app1.cnf
app1 (pid:8451) is running(0:PING_OK), master:192.168.4.51

第四部分:測試叢集配置

1.在主庫51上新增測試資料庫的連結使用者

mysql> create database db8;
Query OK, 1 row affected (0.04 sec)

mysql> create table  db8.a(id int);
Query OK, 0 rows affected (0.27 sec)

mysql> grant select,insert on db8.a to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.02 sec)

2.在客戶端50上鍊接vip地址,訪問叢集

[[email protected] ~]# mysql -uadmin -h192.168.4.100 -p123456
mysql> insert into db8.a values(999);
Query OK, 1 row affected (0.07 sec)

mysql> select * from db8.a;                  //在其餘資料庫伺服器上同樣驗證
+------+
| id   |
+------+
|  999 |
+------+
1 row in set (0.00 sec)  

3.停止51主機的資料庫服務

[[email protected] ~]# systemctl stop mysqld.service 

4.在兩個備用的主庫上檢視現在的vip地址在52還是53上

[[email protected] ~]# ifconfig eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.4.100  netmask 255.255.255.0  broadcast 192.168.4.255
        ether 52:54:00:ce:74:9a  txqueuelen 1000  (Ethernet)
[[email protected] ~]# ifconfig eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:99:fd:2f  txqueuelen 1000  (Ethernet)

5.在50上開終端連結資料庫服務vip地址,並且可以正常檢視和儲存資料

mysql> insert into db8.a values(111);
Query OK, 1 row affected (0.05 sec)

mysql> select * from db8.a;         //在53,54,55上繼續檢視是否同步
+------+
| id   |
+------+
|  999 |
|  111 |
+------+
2 rows in set (0.00 sec)

6.檢視管理節點的app1.cnf檔案,主庫51的配置自動刪除

[[email protected] ~]# vim /etc/mha/app1.cnf
[server default]
...
ssh_user=root
user=root

[server2]
candidate_master=1
hostname=192.168.4.52
...

7.54和55會自動把主庫的ip地址指向新的主庫地址

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.4.52
            ...
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            ...

第五部分:維護叢集

將故障的資料庫伺服器再新增到叢集裡

1.啟動51資料庫伺服器,配置為當前主庫的從庫

mysql> show master status;                     //在新的主庫上檢視主庫資訊
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master52.000001 |      422 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec

[[email protected] ~]# systemctl restart  mysqld.service 
mysql> change master to master_host="192.168.4.52",master_user="repluser",master_password="123456",master_log_file="master52.000001",master_log_pos=422;
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

mysql> show slave status\G;
                    ...
                  Master_Host: 192.168.4.52
                    ...
                  Slave_IO_Running: Yes
                   Slave_SQL_Running: Yes


2.修改56的app1.cnf檔案,新增51的配置資訊

[[email protected] ~]# vim /etc/mha/app1.cnf 
  ...
[server1]
candidate_master=1
hostname=192.168.4.51
  ...

3.啟動管理主機56上的管理服務,並檢視狀態資訊

[[email protected] ~]# masterha_check_status  --conf=/etc/mha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
[[email protected] ~]# masterha_manager --conf=/etc/mha/app1.cnf   --remove_dead_master_conf --ignore_last_failover
Mon Dec 17 20:35:49 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 17 20:35:49 2018 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Dec 17 20:35:49 2018 - [info] Reading server configuration from /etc/mha/app1.cnf..


[[email protected] ~]# masterha_check_status  --conf=/etc/mha/app1.cnf
app1 (pid:12778) is running(0:PING_OK), master:192.168.4.52