當我們做MySQL的讀寫分離與主從複製時,一臺主伺服器下面連著多臺從伺服器,如果當主伺服器掛掉之後,就會出現單點故障的問題,所以我們需要用到 MHA 高可用的故障切換。
主機 | IP地址 | 作業系統 |
---|---|---|
mysql1(主) | 20.0.0.23 | centos-7.6-x86_64 |
mysql2(從/主備) | 20.0.0.24 | centos-7.6-x86_64 |
mysql3(從) | 20.0.0.25 | centos-7.6-x86_64 |
Manger | 20.0.0.22 | centos-7.6-x86_64 |
因為作業系統是centos7,所以要下載MHA 0.57版本,MHA對於版本要求很高!
三臺資料庫伺服器需要先安裝MySQL資料庫,如果不會安裝可以看一下我之前的部落格
部落格地址:安裝資料庫部落格地址
mysql 1:
[root@mysql1 ~]# vi /etc/hosts ## 新增下面三句話
20.0.0.23 mysql1
20.0.0.24 mysql2
20.0.0.25 mysql3
==》》 wq
mysql 2:
[root@mysql2 ~]# vi /etc/hosts ## 新增下面三句話
20.0.0.23 mysql1
20.0.0.24 mysql2
20.0.0.25 mysql3
==》》 wq
mysql 3:
[root@mysql3 ~]# vi /etc/hosts ## 新增下面三句話
20.0.0.23 mysql1
20.0.0.24 mysql2
20.0.0.25 mysql3
==》》 wq
[root@mysql2 ~]# ping mysql1
PING mysql1 (20.0.0.23) 56(84) bytes of data.
64 bytes from mysql1 (20.0.0.23): icmp_seq=1 ttl=64 time=0.293 ms
64 bytes from mysql1 (20.0.0.23): icmp_seq=2 ttl=64 time=0.383 ms
[root@mysql1 cmake-2.8.6]# ping mysql2
PING mysql2 (20.0.0.24) 56(84) bytes of data.
64 bytes from mysql2 (20.0.0.24): icmp_seq=1 ttl=64 time=1.17 ms
64 bytes from mysql2 (20.0.0.24): icmp_seq=2 ttl=64 time=0.392 ms
[root@mysql1 cmake-2.8.6]# ping mysql3
PING mysql3 (20.0.0.25) 56(84) bytes of data.
64 bytes from mysql3 (20.0.0.25): icmp_seq=1 ttl=64 time=0.317 ms
64 bytes from mysql3 (20.0.0.25): icmp_seq=2 ttl=64 time=0.338 ms
mysql 1:
[root@Mysql1 ~]# cat /etc/my.cnf
#default-character-set=utf8 ## 這句話註釋掉,不然健康檢查過不去
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true ## 允許從伺服器同步
==》》wq
[root@mysql1 ~]# systemctl restart mysqld
mysql 2:
設定從伺服器1:
在/etc/my.cnf 中修改或者增加下面內容。
[root@Mysql2 ~]# vim /etc/my.cnf
#default-character-set=utf8 ## 這句話註釋掉,不然健康檢查過不去
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
==》》wq
[root@mysql1 ~]# systemctl restart mysqld
mysql 3:
設定從伺服器2:
在/etc/my.cnf 中修改或者增加下面內容。
[root@Mysql2 ~]# vim /etc/my.cnf
#default-character-set=utf8 ## 這句話註釋掉,不然健康檢查過不去
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
==》》wq
[root@mysql1 ~]# systemctl restart mysqld
[root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/ ## 不建立軟連結,系統識別不了,健康檢查會報錯
[root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
① MySQL 主從設定相對比較簡單。需要注意的是授權。步驟如下:
在所有資料庫節點上授權兩個使用者,一個是從庫同步使用,另外一個是 manager 使用。
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123'; ## 主從複製授權
mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager'; ## 三個資料庫可以被manger所管理
mysql> flush privileges;
② 在 Mysql1 主機上檢視二進位制檔案和同步點
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 895 | | | |
+-------------------+----------+--------------+------------------+-------------------+
③ 接下來在 Mysql2 和 Mysql3 分別執行同步。
mysql> change master to master_host='20.0.0.23',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=895;
mysql> start slave;
④ 檢視 IO 和 SQL 執行緒都是 yes 代表同步是否正常。
分別在mysql 2 和 mysql 3 檢視
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
⑤ 必須設定兩個從庫為唯讀模式:
mysql> set global read_only=1;
mysql> flush privileges;
[root@mysql1 ~]# cd /etc/yum.repos.d/
[root@mysql1 yum.repos.d]# mv backup/CentOS-Base.repo ./
[root@mysql1 yum.repos.d]# ll
total 8
drwxr-xr-x. 2 root root 163 Oct 21 02:21 backup
-rw-r--r--. 1 root root 1664 Nov 23 2018 CentOS-Base.repo
-rw-r--r--. 1 root root 116 Sep 13 21:35 local.repo
[root@mysql1 yum.repos.d]# yum clean all
[root@mysql1 yum.repos.d]# yum makecache
[root@MHA-manager ~]# yum install epel-release --nogpgcheck -y
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
[root@Mysql1 ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@Mysql1 ~]# cd mha4mysql-node-0.57
[root@mysql1 mha4mysql-node-0.57]# ls
AUTHORS COPYING inc Makefile.PL META.yml rpm
bin debian lib MANIFEST README t
[root@Mysql1 mha4mysql-node-0.57]# perl Makefile.PL
[root@Mysql1 mha4mysql-node-0.57]# make && make install
[root@MHA-manager ~]# tar zxvf mha4MHA-manager-0.57.tar.gz
[root@MHA-manager ~]# cd mha4MHA-manager-0.57
[root@localhost mha4mysql-manager-0.57]# ls
AUTHORS COPYING inc Makefile.PL META.yml rpm t
bin debian lib MANIFEST README samples tests
[root@MHA-manager mha4MHA-manager-0.57]# perl Makefile.PL
[root@MHA-manager mha4MHA-manager-0.57]# make && make install
1. 在 manager 上設定到所有資料庫節點的無密碼認證
[root@MHA-manager ~]# ssh-keygen -t rsa //一路按確認鍵
[root@MHA-manager ~]# ssh-copy-id 20.0.0.23 ## 按yes,然後輸入 root 密碼
[root@MHA-manager ~]# ssh-copy-id 20.0.0.24
[root@MHA-manager ~]# ssh-copy-id 20.0.0.25
2. 在 Mysql1 上設定到資料庫節點Mysql2和Mysql3的無密碼認證
[root@Mysql1 ~]# ssh-keygen -t rsa //一路按確認鍵
[root@Mysql1 ~]# ssh-copy-id 20.0.0.24 ## 按yes,然後輸入 root 密碼
[root@Mysql1 ~]# ssh-copy-id 20.0.0.25
3. 在 Mysql2 上設定到資料庫節點Mysql1和Mysql3的無密碼認證
[root@Mysql2 ~]# ssh-keygen -t rsa //一路按確認鍵
[root@Mysql2 ~]# ssh-copy-id 20.0.0.23 ## 按yes,然後輸入 root 密碼
[root@Mysql2 ~]# ssh-copy-id 20.0.0.25
4. 在 Mysql3 上設定到資料庫節點Mysql1和Mysql2的無密碼認證
[root@Mysql3 ~]# ssh-keygen -t rsa //一路按確認鍵
[root@Mysql3 ~]# ssh-copy-id 20.0.0.23 ## 按yes,然後輸入 root 密碼
[root@Mysql3 ~]# ssh-copy-id 20.0.0.24
[root@MHA-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷貝後會有四個執行檔案
[root@atlas ~]# ll /usr/local/bin/scripts/
總用量 32
-rwxr-xr-x 1 mysql mysql 3648 5 月 31 2015 master_ip_failover #自動切換時 VIP 管理的指令碼
-rwxr-xr-x 1 mysql mysql 9872 5 月 25 09:07 master_ip_online_change #線上切換時 vip 的管理
-rwxr-xr-x 1 mysql mysql 11867 5 月 31 2015 power_manager #故障發生後關閉主機的指令碼
-rwxr-xr-x 1 mysql mysql 1360 5 月 31 2015 send_report #因故障切換後傳送報警的指令碼
[root@MHA-manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[root@MHA-manager ~]#vim /usr/local/bin/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
);
#############################新增內容部分#########################################
my $vip = '192.168.195.200';
my $brdc = '192.168.195.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
##################################################################################
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,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
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" ) {
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";
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";
}
①、:% s/^#// 把全文的開頭的#號刪除
②、 第一行開頭加上#號 #!/usr/bin/env perl
③、my $vip = '20.0.0.200'; ## 漂移地址,改成自己網段的IP
my $brdc = '20.0.0.255'; ## 改成自己網段
[root@MHA-manager ~]# mkdir /etc/masterha
[root@MHA-manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[root@MHA-manager ~]# vim /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.24 -s 20.0.0.25
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.23
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.24
port=3306
[server3]
hostname=20.0.0.25
port=3306
[root@MHA-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Tue Nov 26 23:09:46 2019 - [debug] Connecting via SSH from root@192.168.195.131(192.168.195.131:22) to root@192.168.195.130(192.168.195.130:22)..
Tue Nov 26 23:09:47 2019 - [debug] ok.
Tue Nov 26 23:09:47 2019 - [info] All SSH connection tests passed successfully.
root@MHA-manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf ## 測試健康狀態
Checking the Status of the script.. OK
Tue Nov 26 23:10:29 2019 - [info] OK.
Tue Nov 26 23:10:29 2019 - [warning] shutdown_script is not defined.
Tue Nov 26 23:10:29 2019 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 20.0.0.200/24
[root@Mysql1 ~]# ifconfig 檢視一下
如果是最小化安裝需要先安裝網路元件
[root@mysql1 ~]# yum -y install net-tools
[root@Mysql1 ~]# ifconfig
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.200 netmask 255.0.0.0 broadcast 20.255.255.255
ether 00:0c:29:a4:3e:ec txqueuelen 1000 (Ethernet)
[root@MHA-manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
[1] 129929
[root@localhost ~]# jobs ## 檢視後臺執行的任務
[1]+ Running nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
[root@MHA-manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:32493) is running(0:PING_OK), master:20.0.0.23 ## master是20.0.0.23,對的
[root@MHA-manager ~]# cat /var/log/masterha/app1/manager.log
Tue Nov 26 23:12:30 2019 - [info] Checking master_ip_failover_script status:
Tue Nov 26 23:12:30 2019 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=20.0.0.23 --orig_master_ip=20.0.0.23 --orig_master_port=3306
[root@MHA-manager ~]#tailf /var/log/masterha/app1/manager.log ## 啟用監控觀察紀錄檔記錄
[root@mysql1 ~]# systemctl stop mysqld ## 關閉主資料庫mysql1上的資料庫
我們可以在紀錄檔裡面看到漂移地址已經成功
20.0.0.25(20.0.0.25:3306): OK: Applying all logs succeeded. Slave started, replicating from 20.0.0.24(20.0.0.24:3306)
我們現在去mysql2檢視一下網路卡設定資訊,漂移地址已經漂移成功!
[root@mysql2 ~]# ifconfig
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.200 netmask 255.0.0.0 broadcast 20.255.255.255
ether 00:0c:29:a4:3e:ec txqueuelen 1000 (Ethernet)
再檢視一下mysql 1的網路卡設定資訊
[root@mysql2 ~]# ifconfig
已經沒有 ens33:1 虛擬介面資訊