实现思路
因为操作系统是centos7,所以要下载MHA 0.57版本
准备软件包
cmake-2.8.6.tar.gz
mha4mysql-manager-0.57.tar.gz
mha4mysql-node-0.57.tar.gz
mysql-5.6.36.tar.gz
ruby-2.4.1.tar.gz
iptables -F
setenfoce 0
yum -y install ncurses-devel gcc-c++ perl-Module-Install
cd /opt
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure
gmake &&gmake install
cd /opt
tar -zxvf mysql-5.6.36.tar.gz
cd mysql-5.6.36/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc
make && make install
[root@slave ~]# cd /opt/mysql-5.6.36/
[root@Mysql1 mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
[root@Mysql1 mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@Mysql1 ~]# chmod +x /etc/rc.d/init.d/mysqld
[root@Mysql1 ~]# chkconfig --add mysqld
[root@Mysql1 ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
[root@Mysql1 ~]# source /etc/profile
[root@Mysql1 ~]# groupadd mysql
[root@Mysql1 ~]# useradd -M -s /sbin/nologin mysql -g mysql
[root@Mysql1 ~]# chown -R mysql.mysql /usr/local/mysql
[root@Mysql1 ~]# mkdir -p /data/mysql
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
[root@Mysql1 mysql-5.6.36]# service mysqld start
[root@Mysql1 mysql-5.6.36]# netstat -ntap | grep 3306
[root@Mysql1 mysql-5.6.36]# vim /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true
'配置从服务器 mysql2 :'
'在/etc/my.cnf中修改或者增加下面内容'。
vim /etc/my.cnf
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
'配置从服务器 mysql3'
vim /etc/my.cnf
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@mysql1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@mysql1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@Mysql1 ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
或
service mysqld restart
mysql -uroot -p 无密码登陆
主从复制环境中的mysql同步的用户名为:myslave 密码:123456
额外调整
在所有数据库节点上授权两个用户,一个是从库同步使用,另外一个是master使用
mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'20.0.0.%' IDENTIFIED BY '123456';
'20.0.0.0段的从服务器拥有复制权限,可以使用myslave身份123456密码复制所有的库和表'
mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';
'主备mysql服务器添加用户和权限'
mysql> flush privileges; '刷新'
'下面三条授权按理论是不用添加的,但是做案例实验环境时候通过MHA检查MySQL主从有报错,'
'报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权'。
mysql> grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';
mysql> flush privileges; 刷新
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 1219 | | | |
+-------------------+----------+--------------+------------------+-------------------+
change master to master_host='20.0.0.51',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1284;
mysql>start slave; '开启从'
mysql> show slave status\G;
Slave_lO_Running: Yes
Slave_sQL_Running: Yes
mysql> set global read_only=1;
mysql> flush privileges;
[root@mha ~]# yum install epel-release --nogpgcheck -y
[root@mha ~]# 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 mysql-5.6.36]# cd /opt
[root@mysql1 opt]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@mysql1 opt]# cd mha4mysql-node-0.57/
[root@mysql1 mha4mysql-node-0.57]# perl Makefile.PL
[root@mysql1 mha4mysql-node-0.57]# make
[root@mysql1 mha4mysql-node-0.57]# make install
'在MHA-manager上安装manager组件(!注意:一定要先安装node组件才能安装manager组件)'
[root@manager mha4mysql-node-0.57]# cd /opt
[root@manager opt]# tar -zxvf mha4mysql-manager-0.57.tar.gz
[root@manager opt]# cd mha4mysql-manager-0.57/
[root@manager mha4mysql-manager-0.57]# perl Makefile.PL
[root@manager mha4mysql-manager-0.57]# make
[root@manager mha4mysql-manager-0.57]# make install
[root@mha mha4mysql-manager-0.57]# ls /usr/local/bin
masterha_check_ssh '检查MHA 的SSH 配置状况'
masterha_check_repl'检查MySQL复制状况'
masterha_manger'启动manager的脚本'
masterha_check_status'检测当前MHA运行状态'
masterha master_monitor'检测master是否宕机'
masterha_master_switch'控制故障转移(自动或者手动)'
masterha_conf_host'添加或删除配置的server信息'
masterha stop '关闭manager'
save_binary_logs '保存和复制master的二进制日志'
apply_diff_relay_logs '识别差异的中继日志事件并将其差异的事件应用于其他的slave'
filter_mysqlbinlog '去除不必要的ROLLBACK事件(MHA已不再使用这个工具)'
purge_relay_logs '清除中继日志(不会阻塞SQL线程)'
[root@mha mha4mysql-manager-0.57]# cd
[root@mha~]# ssh-keygen -t rsa
'创建非对称密钥对'
'因为想要免密登陆,所以三个都直接回车就行,不需要设置密码'
[root@mha ~]# ssh-copy-id 20.0.0.51
[root@mha ~]# ssh-copy-id 20.0.0.52
[root@mha ~]# ssh-copy-id 20.0.0.47
[root@mysql1 mha4mysql-node-0.57]# cd ~
[root@mysql1 ~]# ssh-keygen -t rsa
[[root@mysql1 ~]# ssh-copy-id 20.0.0.52
[root@mysql1 ~]# ssh-copy-id 20.0.0.47
[root@mysql2 mha4mysql-node-0.57]# cd ~
[root@mysql2 ~]# ssh-keygen -t rsa
[root@mysql2 ~]# ssh-copy-id 20.0.0.51
[root@mysql2 ~]# ssh-copy-id 20.0.0.47
[root@mysql3 mha4mysql-node-0.57]# cd ~
[root@mysql3 ~]# ssh-keygen -t rsa
[root@mysql3 ~]# ssh-copy-id 20.0.0.51
[root@mysql3 ~]# ssh-copy-id 20.0.0.52
[root@manager ~]# cp -ra /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
'拷贝后会有四个执行文件'
[root@manager ~]# ll /usr/local/bin/scripts/
总用量 32
-rwxr-xr-x 1 1001 1001 3648 5月 31 2015 master_ip_failover '自动切换时VIP管理的脚本'
-rwxr-xr-x 1 1001 1001 9870 5月 31 2015 master_ip_online_change '在线切换时VIP的管理'
-rwxr-xr-x 1 1001 1001 11867 5月 31 2015 power_manager '故障发生后闭主机的脚本'
-rwxr-xr-x 1 1001 1001 1360 5月 31 2015 send_report '因故障切换后发送报警的脚本'
[root@mha ~]# cd /usr/local/bin/scripts/
[root@mha scripts]# ls
master_ip_failover power_manager
master_ip_online_change send_report
[root@mha scripts]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[root@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 = '20.0.0.200';
my $brdc = '20.0.0.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;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
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";
}
[root@MHA-manager ~]# mkdir /etc/masterha
[root@MHA-manager~]# cp /opt/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.47 -s 20.0.0.52
shutdown script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.51
port=3306
[server2]
hostname=20.0.0.52
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=20.0.0.47
port=3306
[root@mha scripts]# cd /etc/masterha/
[root@mha masterha]# ls
app1.cnf
[root@mha masterha]# cd /usr/local/bin/scripts/
[root@mha scripts]# cp master_ip_online_change ../
[root@mha scripts]# cd ../
[root@mha bin]# ls
[root@mha bin]# cd scripts/
[root@mha scripts]# ls
master_ip_failover power_manager
master_ip_online_change send_report
[root@mha scripts]# cp send_report /usr/local/
[root@mha scripts]# ls /usr/local/
[root@mha scripts]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Thu Aug 27 23:43:52 2020 - [info] All SSH connection tests passed successfully.
[root@mha scripts]# masterha_check_repl -conf=/etc/masterha/app1.cnf
MySQL Replication Health is OK.
[root@mha scripts]# /sbin/ifconfig ens33:1 20.0.0.200/24
[root@mha scripts]# 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 scripts]# masterha_check_status --conf=/etc/masterha/app1.cnf
[root@mha scripts]# cat /var/log/masterha/app1/manager.log
[root@mha scripts]# tailf /var/log/masterha/app1/manager.log
[root@mysql1 log]# pkill -9 mysqld
[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:26:fd:0f txqueuelen 1000 (Ethernet)
[root@mha ~]# yum install mysql -y
[root@mysql2 ~]# mysql -uroot -p
Enter password:
mysql> grant all on *.* to 'root'@'%' identified by 'abc123';
[root@kevin ~]# mysql -h 20.0.0.200 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.36-log Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[server default]
'#manager配置文件'
manager_log=/var/log/masterha/app1/manager.log
'#manager日志'
manager_workdir=/var/log/masterha/app1
'#master保存binlog的位置,这里的路径要与master里配置的bilog的相同'
master_binlog_dir=/usr/local/mysql/data
'#设置自动failover时候的切换脚本。也就是上边的那个脚本'
master_ip_failover_script=/usr/local/bin/master_ip_failover
'#设置手动切换时候的切换脚本'
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
'#这个密码是前文中创建监控用户的那个密码'
password=manager
remote_workdir=/tmp
'#设置复制用户密码'
repl_password=123
'#设置复制用户的用户'
repl_user=myslave
'#设置发生切换后发生报警的脚本'
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.13.130 -s 192.168.13.131 '#指向两个从服务器的IP地址'
'#设置故障发生关闭故障脚本主机'
shutdown_script=""
'#设置ssh的登录用户名'
ssh_user=root
'#设置监控用户'
user=mha
[server1]
hostname=192.168.13.129 '#主服务器的IP地址'
port=3306
[server2]
candidate_master=1
'#设置为候选master,如果设置该参数以后,发送主从切换以后将会从此从库升级为主库'
hostname=192.168.13.130 '#候选master的IP地址'
check_repl_delay=0 '#默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master'
port=3306
[server3]
hostname=192.168.13.131 '#从服务器的IP地址'
port=3306