MySQL MHA高可用方案【四、MHA部署】
4.1 建立普通使用者
所有伺服器上建立普通使用者toor、設定密碼及sudo授權ifconfig,加入mysql使用者組
useradd -u 900 toor
echo "chenliang"|passwd --stdin toor
id toor
sed -i '92a toor ALL=(ALL) NOPASSWD:/sbin/ifconfig' /etc/sudoers
visudo -c
usermod -g mysql toor
4.2 建立金鑰對
#切換到toor使用者下,生成金鑰,複製公鑰到其它伺服器(包含自身)
#切換到toor普通使用者下,生成金鑰對,
每一臺伺服器都要操作[[email protected] ~]$ su - toor
[[email protected] ~]$ ssh-keygen -t rsa -P "" -f /home/toor/.ssh/id_rsa
Generating public/private rsa key pair.
Created directory '/home/toor/.ssh'.
Your identification has been saved in /home/toor/.ssh/id_dsa.
Your public key has been saved in /home/toor/.ssh/id_dsa.pub.
The key fingerprint is:
16:11:a5:3a:6d:f6:9d:a9:69:7f:03:a3:91:95:34:a3 [email protected]
The key's randomart image is:
+--[ DSA 1024]----+
| oo. |
| o + |
| o o + |
| o .E o |
| o S o |
| = .o.oo |
| .o+o |
| oo o |
| .o... . |
+-----------------+
#拷貝公鑰到其它伺服器,包括自身,在
每一臺伺服器上都要執行以下四條命令#且是在toor普通使用者下
ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"
ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"
ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"
ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"
4.3 相關命令做軟連結
對mysqlbinlog和mysql命令做軟連結,所有伺服器都需要進行操作,這裡以db01為例
[[email protected] ~]# ln -sv /apps/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
`/usr/bin/mysqlbinlog' -> `/apps/mysql/bin/mysqlbinlog'
[[email protected] ~]# ln -sv /apps/mysql/bin/mysql /usr/bin/mysql
`/usr/bin/mysql' -> `/apps/mysql/bin/mysql'
[[email protected] ~]# ll /usr/bin/{mysqlbinlog,mysql}
lrwxrwxrwx 1 root root 21 Nov 16 11:32 /usr/bin/mysql -> /apps/mysql/bin/mysql
lrwxrwxrwx 1 root root 27 Nov 16 11:31 /usr/bin/mysqlbinlog -> /apps/mysql/bin/mysqlbinlog
4.4 建立mha使用者
這裡需要建立mha使用者(在mysql裡面建立),因前面做了主從複製了,所以只需要在
db01(master)伺服器上建立。
#在主庫上建立mha使用者
[[email protected] ~]# mysql -uroot -pchenliang -S /data/3306/mysql.sock
mysql>
mysql> grant all privileges on *.* to 'mha'@'172.16.1.%' identified by 'mha';
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> select user,host,authentication_string from mysql.user where user='mha' and host='172.16.1.%';
+------+------------+-------------------------------------------+
| user | host | authentication_string |
+------+------------+-------------------------------------------+
| mha | 172.16.1.% | *F4C9AC49A736981AE2739FC2F4A1FD92B4F07929 |
+------+------------+-------------------------------------------+
1 row in set (0.00 sec)
#在從庫(db02、db03、db04)上檢查是否有mha使用者
mysql> select user,host from mysql.user where user='mha' and host='172.16.1.%';
+------+------------+
| user | host |
+------+------------+
| mha | 172.16.1.% |
+------+------------+
1 row in set (0.00 sec)
4.5 安裝mha node包
安裝mha node包,所以伺服器都需要安裝。這裡以db01伺服器上的安裝為例
[[email protected] ~]# yum install perl-DBD-MySQL -y
[[email protected] ~]# cd tools/
[[email protected] tools]# rz
[[email protected] tools]# ll mha4mysql-node-0.56-0.el6.noarch.rpm
-rw-r--r-- 1 root root 36326 Sep 16 2016 mha4mysql-node-0.56-0.el6.noarch.rpm
[[email protected] tools]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]
4.6 安裝mha manager包
注意:這裡是在db04伺服器上安裝,其它伺服器不用安裝
[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[[email protected] ~]# yum install perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
[[email protected] ~]# rpm -qa perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
perl-Parallel-ForkManager-1.20-1.el6.noarch
perl-Log-Dispatch-2.27-1.el6.noarch
perl-Time-HiRes-1.9721-144.el6.x86_64
perl-Config-Tiny-2.12-7.1.el6.noarch
epel-release-6-8.noarch
[[email protected] ~]# cd tools/
[[email protected] tools]# rz
[[email protected] tools]# ll mha4mysql-manager-0.56-0.el6.noarch.rpm
-rw-r--r-- 1 root root 87119 Sep 16 2016 mha4mysql-manager-0.56-0.el6.noarch.rpm
[[email protected] tools]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-manager ########################################### [100%]
4.7 建立mha相關目錄
注意:這裡是在db04伺服器上建立相應的目錄
#建立MHA的工作目錄
[[email protected] ~]# mkdir /etc/mha/app/app1 -p
[[email protected] ~]# chown -R toor:toor /etc/mha/app/app1/
[[email protected] ~]# ls -ld /etc/mha/app/app1/
drwxr-xr-x 2 toor toor 4096 Nov 16 12:27 /etc/mha/app/app1/
#建立MHA的日誌目錄
[[email protected] ~]# mkdir /var/log/mha/app/app1 -p
[[email protected] ~]# chown -R toor:toor /var/log/mha/app/app1/
[[email protected] ~]# ls -ld /var/log/mha/app/app1/
drwxr-xr-x 2 toor toor 4096 Nov 16 12:28 /var/log/mha/app/app1/
4.8 配置mha配置檔案
[[email protected] ~]# cd /etc/mha/app/app1/
[[email protected] app1]# vim app1.cnf
[server default]
manager_log=/var/log/mha/app/app1/manager.log
manager_workdir=/var/log/mha/app/app1
master_binlog_dir=/data/3306/binlog
master_ip_failover_script=/server/scripts/master_ip_failover
password=mha
ping_interval=2
repl_password=chenliang
repl_user=rep
ssh_user=toor
ssh_port=921
user=mha
[server1]
hostname=172.16.1.11
port=3306
[server2]
hostname=172.16.1.12
port=3306
[server3]
hostname=172.16.1.13
port=3306
[server4]
hostname=172.16.1.14
no_master=1
port=3306
[[email protected] app1]# chown -R toor:toor /etc/mha/app/app1/app1.cnf
[[email protected] app1]# ll /etc/mha/app/app1/app1.cnf
-rw-r--r-- 1 toor toor 468 Nov 16 12:32 /etc/mha/app/app1/app1.cnf
#配置檔案說明
[server default]
manager_log=/var/log/mha/app/app1/manager.log #mha的日誌儲存路徑及檔名
manager_workdir=/var/log/mha/app/app1 #mha的工作目錄
master_binlog_dir=/data/3306/binlog #所有mysql 的binlog日誌存放路徑
master_ip_failover_script=/server/scripts/master_ip_failover#VIP飄移指令碼的存放路徑
password=mha #mha使用者的密碼
ping_interval=2 #每隔2秒檢測一下Master是否正常
repl_password=chenliang #主從複製使用者的密碼
repl_user=rep #主從複製的使用者
ssh_user=toor #ssh通訊的使用者
ssh_port=921 #ssh的埠
user=mha #mha的使用者名稱mha
[server1] #伺服器1
hostname=172.16.1.11 #hostname最好用ip地址來指定
port=3306 #mysql服務的埠
[server2] #伺服器2
hostname=172.16.1.12 #hostname最好用ip地址來指定
port=3306 #mysql服務的埠
[server3] #伺服器3
hostname=172.16.1.13 #hostname最好用ip地址來指定
port=3306 #mysql服務的埠
[server4] #伺服器4
hostname=172.16.1.14 #hostname最好用ip地址來指定
no_master=1 #不讓其伺服器提升為master
port=3306 #mysql服務的埠
4.9 配置mha vip飄移指令碼
注意:這裡也只是在db04伺服器上編寫指令碼
[[email protected] ~]# mkdir /server/scripts/ -p
[[email protected] ~]# cd /server/scripts/
[[email protected] scripts]# rz
[[email protected] scripts]# ll master_ip_failover
-rw-r--r-- 1 root root 3049 Nov 8 20:30 master_ip_failover
[[email protected] scripts]# cat 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 $ssh_port = '921' ;
my $vip = '172.16.1.10/24';
my $key = "1";
my $ssh_start_vip = " sudo /sbin/ifconfig eth1:$key $vip";
my $ssh_stop_vip = "sudo /sbin/ifconfig eth1:$key down";
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" ) {
# $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 "\n\n\n***************************************************************\n";
print "Disabling the VIP - $vip on old master: $orig_master_host\n";
print "***************************************************************\n\n\n\n";
&stop_vip();
$exit_code = 0;
};
if ([email protected]) {
warn "Got Error: [email protected]\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 "\n\n\n***************************************************************\n";
print "Enabling the VIP - $vip on new master: $new_master_host \n";
print "***************************************************************\n\n\n\n";
&start_vip();
$exit_code = 0;
};
if ([email protected]) {
warn [email protected];
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
`ssh -p$ssh_port $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 -p$ssh_port $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh -p$ssh_port $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
"Usage: master_ip_failover -command=start|stop|stopssh|status -orig_master_host=host -orig_master_ip=ip -orig_master_port=po
rt -new_master_host=host -new_master_ip=i-new_master_port=port\n";
}
[[email protected] scripts]# chmod +x master_ip_failover
[[email protected] scripts]# chown toor:toor master_ip_failover
[[email protected] scripts]# ll master_ip_failover
-rwxr-xr-x 1 toor toor 3049 Nov 16 12:47 master_ip_failover
4.10 配置VIP地址
注意:這裡是在Master(db01)伺服器上配置vip地址
[[email protected] ~]# ifconfig eth1:1 172.16.1.10/24
[[email protected] ~]# ifconfig eth1:1
eth1:1 Link encap:Ethernet HWaddr 00:0C:29:6B:2E:55
inet addr:172.16.1.10 Bcast:172.16.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[[email protected] ~]# cp -a /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1:1
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1:1
DEVICE=eth1:1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.16.1.10
NETMASK=255.255.255.0
USERCTL=no
PEERDNS=yes
IPV6INIT=no
1.11 檢查mha的配置
注意:這裡是在db04伺服器上的toor普通使用者下進行操作
#檢查ssh免密觸是否正常
[[email protected] ~]$ masterha_check_ssh --conf=/etc/mha/app/app1/app1.cnf
Fri Nov 16 12:57:21 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Nov 16 12:57:21 2018 - [info] Reading application default configuration from /etc/mha/app/app1/app1.cnf..
Fri Nov 16 12:57:21 2018 - [info] Reading server configuration from /etc/mha/app/app1/app1.cnf..
Fri Nov 16 12:57:21 2018 - [info] Starting SSH connection tests..
Fri Nov 16 12:57:22 2018 - [debug]
Fri Nov 16 12:57:21 2018 - [debug] Connecting via SSH from [email protected](172.16.1.11:921) to [email protected](172.16.1.12:921)..
Fri Nov 16 12:57:22 2018 - [debug] ok.
Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.11:921) to [email protected](172.16.1.13:921)..
Fri Nov 16 12:57:22 2018 - [debug] ok.
Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.11:921) to [email protected](172.16.1.14:921)..
Fri Nov 16 12:57:22 2018 - [debug] ok.
Fri Nov 16 12:57:23 2018 - [debug]
Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.12:921) to [email protected](172.16.1.11:921)..
Fri Nov 16 12:57:22 2018 - [debug] ok.
Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.12:921) to [email protected](172.16.1.13:921)..
Fri Nov 16 12:57:22 2018 - [debug] ok.
Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.12:921) to [email protected](172.16.1.14:921)..
Fri Nov 16 12:57:23 2018 - [debug] ok.
Fri Nov 16 12:57:23 2018 - [debug]
Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.13:921) to [email protected](172.16.1.11:921)..
Fri Nov 16 12:57:23 2018 - [debug] ok.
Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.13:921) to [email protected](172.16.1.12:921)..
Fri Nov 16 12:57:23 2018 - [debug] ok.
Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.13:921) to [email protected](172.16.1.14:921)..
Fri Nov 16 12:57:23 2018 - [debug] ok.
Fri Nov 16 12:57:24 2018 - [debug]
Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.14:921) to [email protected](172.16.1.11:921)..
Fri Nov 16 12:57:23 2018 - [debug] ok.
Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.14:921) to [email protected](172.16.1.12:921)..
Fri Nov 16 12:57:24 2018 - [debug] ok.
Fri Nov 16 12:57:24 2018 - [debug] Connecting via SSH from [email protected](172.16.1.14:921) to [email protected](172.16.1.13:921)..
Fri Nov 16 12:57:24 2018 - [debug] ok.
Fri Nov 16 12:57:24 2018 - [info] All SSH connection tests passed successfully.
#檢查主從複製是否正常
[[email protected] ~]$ masterha_check_repl --conf=/etc/mha/app/app1/app1.cnf
Fri Nov 16 12:59:03 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Nov 16 12:59:03 2018 - [info] Reading application default configuration from /etc/mha/app/app1/app1.cnf..
Fri Nov 16 12:59:03 2018 - [info] Reading server configuration from /etc/mha/app/app1/app1.cnf..
Fri Nov 16 12:59:03 2018 - [info] MHA::MasterMonitor version 0.56.
Fri Nov 16 12:59:04 2018 - [info] GTID failover mode = 1
Fri Nov 16 12:59:04 2018 - [info] Dead Servers:
Fri Nov 16 12:59:04 2018 - [info] Alive Servers:
Fri Nov 16 12:59:04 2018 - [info] 172.16.1.11(172.16.1.11:3306)
Fri Nov 16 12:59:04 2018 - [info] 172.16.1.12(172.16.1.12:3306)
Fri Nov 16 12:59:04 2018 - [info] 172.16.1.13(172.16.1.13:3306)
Fri Nov 16 12:59:04 2018 - [info] 172.16.1.14(172.16.1.14:3306)
Fri Nov 16 12:59:04 2018 - [info] Alive Slaves:
Fri Nov 16 12:59:04 2018 - [info] 172.16.1.12(172.16.1.12:3306) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled
Fri Nov 16 12:59:04 2018 - [info] GTID ON
Fri Nov 16 12:59:04 2018 - [info] Replicating from 172.16.1.11(172.16.1.11:3306)
Fri Nov 16 12:59:04 2018 - [info] 172.16.1.13(172.16.1.13:3306) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled
Fri Nov 16 12:59:04 2018 - [info] GTID ON
Fri Nov 16 12:59:04 2018 - [info] Replicating from 172.16.1.11(172.16.1.11:3306)
Fri Nov 16 12:59:04 2018 - [info] 172.16.1.14(172.16.1.14:3306) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled
Fri Nov 16 12:59:04 2018 - [info] GTID ON
Fri Nov 16 12:59:04 2018 - [info] Replicating from 172.16.1.11(172.16.1.11:3306)
Fri Nov 16 12:59:04 2018 - [info] Not candidate for the new Master (no_master is set)
Fri Nov 16 12:59:04 2018 - [info] Current Alive Master: 172.16.1.11(172.16.1.11:3306)
Fri Nov 16 12:59:04 2018 - [info] Checking slave configurations..
Fri Nov 16 12:59:04 2018 - [info] read_only=1 is not set on slave 172.16.1.12(172.16.1.12:3306).
Fri Nov 16 12:59:04 2018 - [info] read_only=1 is not set on slave 172.16.1.13(172.16.1.13:3306).
Fri Nov 16 12:59:04 2018 - [info] read_only=1 is not set on slave 172.16.1.14(172.16.1.14:3306).
Fri Nov 16 12:59:04 2018 - [info] Checking replication filtering settings..
Fri Nov 16 12:59:04 2018 - [info] binlog_do_db= , binlog_ignore_db=
Fri Nov 16 12:59:04 2018 - [info] Replication filtering check ok.
Fri Nov 16 12:59:04 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Fri Nov 16 12:59:04 2018 - [info] Checking SSH publickey authentication settings on the current master..
Fri Nov 16 12:59:04 2018 - [info] HealthCheck: SSH to 172.16.1.11 is reachable.
Fri Nov 16 12:59:04 2018 - [info]
172.16.1.11(172.16.1.11:3306) (current master)
+--172.16.1.12(172.16.1.12:3306)
+--172.16.1.13(172.16.1.13:3306)
+--172.16.1.14(172.16.1.14:3306)
Fri Nov 16 12:59:04 2018 - [info] Checking replication health on 172.16.1.12..
Fri Nov 16 12:59:04 2018 - [info] ok.
Fri Nov 16 12:59:04 2018 - [info] Checking replication health on 172.16.1.13..
Fri Nov 16 12:59:04 2018 - [info] ok.
Fri Nov 16 12:59:04 2018 - [info] Checking replication health on 172.16.1.14..
Fri Nov 16 12:59:04 2018 - [info] ok.
Fri Nov 16 12:59:04 2018 - [info] Checking master_ip_failover_script status:
Fri Nov 16 12:59:04 2018 - [info] /server/scripts/master_ip_failover --command=status --ssh_user=toor --orig_master_host=172.16.1.11 --orig_master_ip=172.16.1.11 --orig_master_port=3306 --orig_master_ssh_port=921
Unknown option: orig_master_ssh_port
Checking the Status of the script.. OK
Fri Nov 16 12:59:04 2018 - [info] OK.
Fri Nov 16 12:59:04 2018 - [warning] shutdown_script is not defined.
Fri Nov 16 12:59:04 2018 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK
4.12 編寫mha的啟動命令
注意:這裡是在db04伺服器上的root使用者下進行操作
#把啟動命令追加到一個檔案中
[[email protected] ~]# echo "nohup masterha_manager --conf=/etc/mha/app/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover </dev/null> /var/log/mha/app/app1/manager.log 2>&1 &" >mha
[[email protected] ~]# cat mha
nohup masterha_manager --conf=/etc/mha/app/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover </dev/null> /var/log/mha/app/app1/manager.log 2>&1 &
[[email protected] ~]# ll mha
-rw-r--r-- 1 root root 159 Nov 16 13:49 mha
[[email protected] ~]# chown toor:toor mha
[[email protected] ~]# chmod 700 mha
[[email protected] ~]# mv mha /usr/bin/
[[email protected] ~]# ll /usr/bin/mha
-rwx------ 1 toor toor 159 Nov 16 13:49 /usr/bin/mha
4.13 啟動mha服務
注意:這裡是在db04伺服器上進行操作
[[email protected] ~]# su - toor
[[email protected] ~]$ mha
[[email protected] ~]$ ps -ef|grep mha
toor 5220 1 8 13:51 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/app/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover
toor 5232 5191 1 13:51 pts/0 00:00:00 grep --color=auto mha