码迷,mamicode.com
首页 > 数据库 > 详细

MySQL架构——MHA部署

时间:2019-12-25 22:02:42      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:sql   使用   utils   mysqld   架构   tmp   tin   install   password   

MHA 简介

MHA(Master High Availability)

  • 目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

该软件由两部分组成

  • MHA Manager(管理节点)
    • MHA Manager可以单独部署在一台单独的机器上管理多个master-slave集群,也可以部署在一台slave节点上。
  • MHA Node(数据节点)
    • MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。

工作原理

  • 在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。
  • 顺序
    • 从宕机崩溃的master保存二进制日志事件(binlog events);
    • 识别含有最新更新的slave;
    • 应用差异的中继日志(relay log)到其他的slave;
    • 应用从master保存的二进制日志事件(binlog events);
    • 提升一个slave为新的master;
    • 使其他的slave连接新的master进行复制

部署MHA

实验环境

  • master服务器IP地址:192.144.144.131
  • slave1服务器IP地址:192.144.144.140
  • slave2服务器IP地址:192.144.144.136
  • manager服务器IP地址:192.144.144.170

在master、slave1、slave2服务器安装mysql

yum -y install ncurses-devel gcc-c++ perl-Module-Install    //安装环境包
mount.cifs //192.144.100.8/shares /mnt/       //挂载软件包目录
cd /mnt      //进入挂载目录
tar zxvf mysql-5.6.26.tar.gz -C /opt/      //解压
cd /opt/mysql-5.6.26              //进入解压目录
cmake \                     //配置
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
make && make install         //编译安装
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql    //创建mysql用户
chown -R mysql.mysql /usr/local/mysql
cp support-files/my-default.cnf /etc/my.cnf     //复制配置文件
cp support-files/mysql.server /etc/rc.d/init.d/mysqld  //复制启动脚本
chmod +x /etc/rc.d/init.d/mysqld                //添加执行权限
chkconfig --add mysqld                        //添加声明
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile     //设置化境变量
source /etc/profile   //使环境变量生效

修改 mysql 的主配置文件:/etc/my.cnf ,注意三台服务器的 server-id 不能一样

//master服务器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 1
log_bin = master-bin
log-slave-updates = true

//slave1服务器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

//slave2服务器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

三台服务器启动 mysql 服务

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/   //创建链接文件
systemctl stop firewalld.service      //关闭防火墙
setenforce 0                          //关闭selinux
/usr/local/mysql/bin/mysqld_safe --user=mysql &        //开启服务
[root@s01 mysql-5.6.36]# netstat -natp | grep 3306    //查看端口是否正常开发
tcp6       0      0 :::3306                 :::*                    LISTEN      40105/mysqld 

配置 Mysql 主从同步(授权分别在master、slave1、slave2服务器上都要授权)

mysql -uroot -p      //进入数据库
mysql> grant replication slave on *.* to ‘myslave‘@‘192.144.144.%‘ identified by ‘123‘;   //添加授权
mysql> grant all privileges on *.* to ‘mha‘@‘192.144.144.%‘ identified by ‘manager‘;    //添加授权
mysql> flush privileges;  //刷新数据库
mysql> grant all privileges on *.* to ‘mha‘@‘master‘ identified by ‘manager‘;   //添加授权主机名
mysql> grant all privileges on *.* to ‘mha‘@‘slave1‘ identified by ‘manager‘;
mysql> grant all privileges on *.* to ‘mha‘@‘slave2‘ identified by ‘manager‘;
mysql> show master status;    //在 Mysql 主服务器上查看二进制文件和同步点
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1215 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

在slave1 和slave2 上分别执行同步

change master to master_host=‘192.168.144.131‘,master_user=‘myslave‘,master_password=‘123‘,master_log_file=‘master-bin.000001‘,master_log_pos=1215;

在两台从服务器上开启slave,查看 IO 和 SQL 线程是否都为 yes ,表示同步正常

mysql> start slave;
mysql> show slave status\G;
...
            raster user: mnysIave
            Master_ Port: 3306
          Connect_ Retry: 60
       Master Log_ File: master-bin .000001
    Read Master Log_ Pos: 1215
       Relay_ Log_ File: relay- log- bin.000002 
       Relay_ Log_ Pos: 284
  Relay_ Master Log_ File: master- bin .000001
      Slave_ IO_ Running: Yes
     Slave_ SQL_ Running: Yes
        Replicate DO DB:
...
mysql> set global read_only=1;    //设置两个从服务器为只读模式

安装MHA(所有服务器上都必须安装 MHA 依赖的环境包)

yum install epel-release --nogpgcheck -y        //安装epel
yum install -y perl-DBD-MYSQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
cd /mnt
tar zvxf mha4mysql-node-0.57.tar.gz -C /opt/     //解压node组件包
cd /opt/mha4mysql-node-0.57/
perl Makefile.PL 
make && make install

在manager服务器上安装 manager组件

cd /mnt
tar zvxf mha4mysql-manager-0.57.tar.gz -C /opt/
cd /opt/mha4mysql-manager-0.57/
perl Makefile.PL
make && make install

配置无密码认证

//在 manager 上配置到所有数据库节点的无密码认证:
ssh-keygen -t rsa  //因为是无密码登录,所以这步一直回车即可(会出现密钥)
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.140
ssh-copy-id 192.168.144.136
输入“yes”;再输入密码即可

//在 master 上配置到数据库节点slave1 和slave2 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.140
ssh-copy-id 192.168.144.136
输入“yes”;再输入密码即可

//在 slave1 上配置到数据库节点master 和slave2 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.136
输入“yes”;再输入密码即可

//在 slave2 上配置到数据库节点master 和slave1 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.140
输入“yes”;再输入密码即可

在 manager 节点上复制相关脚本到 /usr/local/bin 目录

cp -ra /opt/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin/
ls scripts/
master_ip_failover:        //自动切换时 VIP 管理的脚本;
master_ip_online_change:    //在线切换时 VIP 的管理;
power_manager:              //故障发生后关闭主机的脚本;
send_report:                //因故障切换后发送报警的脚本;
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/  //将自动切换时 VIP 管理的脚本复制到 /usr/local/bin/目录下

重新编写 master_ip_failover 脚本

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.114.100‘;
my $brdc = ‘192.168.114.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";
}

创建 MHA 软件目录并拷贝配置文件

mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
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
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.114.140 -s 192.168.114.136
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=192.168.114.131
port=3306

[server2]
candidate_master=1
hostname=192.168.114.140
check_repl_delay=0
port=3306

[server3]
hostname=192.168.114.136
port=3306

测试 ssh 无密码认证

masterha_check_ssh -conf=/etc/masterha/app1.cnf            //验证密钥对
Mon Dec 16 10:32:49 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 16 10:32:49 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:32:49 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:32:49 2019 - [info] Starting SSH connection tests..
Mon Dec 16 10:32:50 2019 - [debug] 
Mon Dec 16 10:32:49 2019 - [debug]  Connecting via SSH from root@192.168.144.131(192.168.144.131:22) to root@192.168.144.136(192.168.144.136:22)..
Mon Dec 16 10:32:49 2019 - [debug]   ok.
Mon Dec 16 10:32:49 2019 - [debug]  Connecting via SSH from root@192.168.144.131(192.168.144.131:22) to root@192.168.144.140(192.168.144.140:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:51 2019 - [debug] 
Mon Dec 16 10:32:50 2019 - [debug]  Connecting via SSH from root@192.168.144.140(192.168.144.140:22) to root@192.168.144.131(192.168.144.131:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:50 2019 - [debug]  Connecting via SSH from root@192.168.144.140(192.168.144.140:22) to root@192.168.144.136(192.168.144.136:22)..
Mon Dec 16 10:32:51 2019 - [debug]   ok.
Mon Dec 16 10:32:51 2019 - [debug] 
Mon Dec 16 10:32:49 2019 - [debug]  Connecting via SSH from root@192.168.144.136(192.168.144.136:22) to root@192.168.144.131(192.168.144.131:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:50 2019 - [debug]  Connecting via SSH from root@192.168.144.136(192.168.144.136:22) to root@192.168.144.140(192.168.144.140:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:51 2019 - [info] All SSH connection tests passed successfully.

验证复制

masterha_check_repl -conf=/etc/masterha/app1.cnf      #验证复制
Mon Dec 16 10:34:36 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 16 10:34:36 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:34:36 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:34:36 2019 - [info] MHA::MasterMonitor version 0.57.
Creating directory /var/log/masterha/app1.. done.
Mon Dec 16 10:34:37 2019 - [info] GTID failover mode = 0
Mon Dec 16 10:34:37 2019 - [info] Dead Servers:
Mon Dec 16 10:34:37 2019 - [info] Alive Servers:
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.140(192.168.144.140:3306)
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.136(192.168.144.136:3306)
Mon Dec 16 10:34:37 2019 - [info] Alive Slaves:
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.140(192.168.144.140:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Mon Dec 16 10:34:37 2019 - [info]     Replicating from 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.136(192.168.144.136:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Mon Dec 16 10:34:37 2019 - [info]     Replicating from 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] Current Alive Master: 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] Checking slave configurations..
Mon Dec 16 10:34:37 2019 - [warning]  relay_log_purge=0 is not set on slave 192.168.144.140(192.168.144.140:3306).
...
Checking the Status of the script.. OK 
Mon Dec 16 10:35:22 2019 - [info]  OK.
Mon Dec 16 10:35:22 2019 - [warning] shutdown_script is not defined.
Mon Dec 16 10:35:22 2019 - [info] Got exit code 0 (Not master dead).

在主服务器设置虚拟网卡

/sbin/ifconfig ens33:1 192.168.144.170/24

manager服务器开启服务

nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_cnf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &      //启动MHA
masterha_check_status --conf=/etc/masterha/app1.cnf                     //检查master
app1 (pid:3894) is running(0:PING_OK), master:192.168.144.131
cat /var/log/masterha/app1/manager.log            //查看节点信息
tailf /var/log/masterha/app1/manager.log            //启动监控日志

在主服务器停止MySQL模拟宕机

pkill -9 mysql                    
  • 此时在slave1服务器查看虚拟地址是否转移到此服务器上

MySQL架构——MHA部署

标签:sql   使用   utils   mysqld   架构   tmp   tin   install   password   

原文地址:https://blog.51cto.com/14473285/2461816

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!