MHA集群
昨日mycat总结
昨日搭建了利用mycat分片服务器控制三条数据库服务器的练习,毫无逻辑的总结下
- 客户端访问分片服务器上面(默认8066端口)的逻辑数据库(server.xml里面定义),使用的是虚拟的用户(不是真实数据库里面的用户,是mycat的server.xml里面设置的)
- 进入逻辑数据库之后,使用show tables可以查看在schema.xml里面设置的table标签对应的逻辑表,但是desc查看表结构是空的,所以需要自己根据schema.xml表和真实数据库内容配置相应参数
- schema.xml里面不同逻辑库里面不能出现相同名字的逻辑表,要问为什么,如果不同逻辑库里面的逻辑表同名,那么实际数据库里面就会出现相同的表,schema.xml会指定每个节点服务器上的真实数据库作为节点的!
- 在逻辑表中插入数据时,必须要同时指定字段名和值,因为分片服务器上是虚拟的数据库 ,本质是处理SQL语句字符串,根据字段名找到对应的分片规则,然后分别去真实的数据库执行SQL语句处理,所以不能省略字段名。
- 分片规则在schema.xml里面的table标签里面应用,对应的是rule.xml里面的算法名和包含对应参数的配置文件。
MHA
MHA概述
- 是日本DeNA公司youshimaton开发
- 是一套优秀的实现MySQL高可用的解决方案
- 数据库的自动故障切换操作能做到在0~30秒之内完成
- MHA能确保在故障切换过程中最大限度保证数据的一致性,以达到真正意义上的高可用
MHA组成
- MHA Manager (管理节点)
- 管理所有数据库服务器
- 可用单独部署在一台独立的机器上
- 也可以部署在某台数据库服务器上
- MHA Node(数据节点)
- 存储数据的MySQL服务器
- 运行在每台MySQL服务器上
MHA集群架构
MHA工作过程
- 由Manager定时探测集群中的master节点
- 当master故障时,Manager自动将拥有最新数据的slave提升为新的master
MHA集群测试
前置准备
IP地址 | 主从同步角色 | 集群角色 | 主机名 |
---|---|---|---|
192.168.4.50 | 客户端 | 无 | client50 |
192.168.4.53 | 主库 | 当前主库 | host53 |
192.168.4.54 | 从库 | 备用主库 | host54 |
192.168.4.55 | 从库 | 备用主库 | host55 |
192.168.4.51 | 无 | 管理主机 | mag51 |
192.168.4.100 | 无 | VIP地址 | 无 |
过程
配置ssh免密登录
#4.53
ssh-keygen -f /root/.ssh/id_rsa -N ''
ssh-copy-id root@192.168.4.54
ssh-copy-id root@192.168.4.55
#4.54
ssh-keygen -f /root/.ssh/id_rsa -N ''
ssh-copy-id root@192.168.4.53
ssh-copy-id root@192.168.4.55
#4.55
ssh-keygen -f /root/.ssh/id_rsa -N ''
ssh-copy-id root@192.168.4.54
ssh-copy-id root@192.168.4.53
#因为每个数据库都可能变成Master 所以需要都发送密钥
#4.51 控制服务器也需要ssh密钥
ssh-keygen -f /root/.ssh/id_rsa -N ''
ssh-copy-id root@192.168.4.55
ssh-copy-id root@192.168.4.54
ssh-copy-id root@192.168.4.53
数据库服务器安装依赖包
#4.53~55 4.51
yum -y install perl-* #安装系统自带的perl软件包
cd mha-soft-student; yum -y install perl-* #安装自己下的perl软件包新东西
配置mysql一主多从
#4.53
vim /etc/my.cnf
=============================
[mysqld]
log-bin=master53 #启动binlog日志
server_id=53 #设置server_id
========================================
systemctl restart mysqld
mysql -uroot -p #设置从用户权限
set global validate_password_policy=0; #密码策略为0 只检查长度
set global validate_password_length=6; #最短6位
grant replication slave on *.* to look@'%' identified by '123456'; #密码策略可以修改简单点
show master status; #查看状态
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master53.000001 | 437 | | | |
+-----------------+----------+--------------+------------------+-------------------+
#4.54~55 #配置主服务器信息 启动slave
vim /etc/my.cnf
=====================
[mysqld]
server_id=54 #54和55 要不一样即可
log-bin=master54 #54和55 后面MHA集群配置需要改变slave为master所以需要开启binlog日志
===============================
systemctl restart mysqld
mysql -uroot -p
change master to
master_host='192.168.4.53',
master_user='look',
master_password='123456',
master_log_file='master53.000001', #日志名以主服务器binlog日志名为准
master_log_pos=437; #偏移量以主服务器binlog日志为准
start slave;
show slave status\G #检查IO线程 和SQL 线程状态
grant replication slave on *.* to look@'%' identified by '123456';
#变身成Master时需要访问权限 4.54 4.55也要配置
#按照上面顺序 到这 一主多从已经完成了
#4.51上执行允许访问权限设置
mysql -uroot -p
grant all on *.* to root@'%' identified by '123456' #主从配置完成三台都可以被访问
配置MHA集群
#4.51上安装mha manager源码编译
yum -y install perl-ExtUtils-* perl-CPAN* #依赖库
cd mha
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm #安装节点rpm包
tar -zxvf mha4mysql-manager-0.56.tar.gz
cd mha4mysql-manager-0.56
perl Makefile.PL
make && make install #源码编译安装manager
which masterha_check* #检查masterha_check_ssh 等命令程序是否安装
mkdir /etc/mha #创建mha配置文件目录
cd mha4mysql-manager-0.56
ls samples/conf/ #查看模板文件
app1.cnf masterha_default.cnf
cp samples/conf/app1.cnf /etc/mha #复制模板配置文件到自己文件目录下
vim /etc/mha/app1.cnf #修改符合自己IP地址
==============================
[server default] #管理服务允许参数设置
manager_workdir=/etc/mha #控制工作目录
manager_log=/etc/mha/manager.log #服务日志位置
master_ip_failover_script=/etc/mha/master_ip_failover #perl故障脚本
#故障脚本master_ip_failover 由perl语言编写 帮助切换VIP等 下面给出内容
ssh_user=root #ssh使用角色 最开始发送root的ssh公钥
ssh_port=22 #ssh端口号
repl_user=look #主从数据库角色 上面replication slave设置的
repl_password=123456
user=root #访问数据库的角色 上面最后设置的
password=123456
[server1] #指定数据库服务器ip地址
hostname=192.168.4.53
port=3306
candidate_master=1 #能够变成master数据库
[server2]
hostname=192.168.4.54
port=3306
candidate_master=1
[server3]
hostname=192.168.4.55
port=3306
candidate_master=1
===========================================
#故障脚本编写
vim /etc/mha/master_ip_failover #故障时调用执行
===============================
#!/usr/bin/env perl
# Copyright (C) 2011 DeNA Co.,Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by:
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
## Note: This is a sample script and is not complete. Modify the script based on your environment.
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
my (
$command, $ssh_user, $orig_master_host,
$orig_master_ip, $orig_master_port, $new_master_host,
$new_master_ip, $new_master_port, $new_master_user,
$new_master_password
);
my $vip = '192.168.4.100/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #指定在哪个网卡上设置VIP
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #指定那个网卡上关闭VIP
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,
'new_master_user=s' => \$new_master_user,
'new_master_password=s' => \$new_master_password,
);
exit &main();
sub main {
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 {
# updating global catalog, etc
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\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 {
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
## Set read_only=0 on the new master
$new_master_handler->disable_log_bin_local();
print "Set read_only=0 on the new master.\n";
$new_master_handler->disable_read_only();
## Creating an app user on the new master
print "Creating app user on the new master..\n";
$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();
## Update master ip on the catalog database, etc
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
# If you want to continue failover, exit 10.
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
# do nothing
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`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";
}
==========================================
#手动配置VIP到4.53上 检查4.54 4.55 有没有ifconfig命令 自动配置使用大概ifconfig命令
which ifconfig || yum -y install net-tools
/usr/sbin/ifconfig
ifconfig ens33:1 192.168.4.100 #配置虚拟IP
ifconfig ens33:1 #查看是否配置成功
配置数据库服务器半同步复制模式和mha-node
#4.53启动半同步主从模块
mysql -uroot -p
install plugin rpl_semi_sync_master SONAME 'semisync_master.so';#安装半同步主模块
install plugin rpl_semi_sync_slave SONAME 'semisync_slave.so';#安装半同步从模块
set global rpl_semi_sync_master_enabled=1; #启动主模块
set global rpl_semi_sync_slave_enabled=1; #启动从模块
exit
#现在4.53模块已经启动了主从模块了
#修改配置文件永久启动服务时候启动模块
vim /etc/my.cnf
============================
[mysqld]
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=0 #禁止自动删除本机的中继日志文件
=====================================
#4.54和4.55可能变成主数据库服务 也要修改
vim /etc/my.cnf
===================================
[mysqld]
log-bin=master54
server_id=54 #主的配置都开启
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=0 #禁止自动删除本机的中继日志文件
============================================
systemctl restart mysqld
#到这数据库配置完成 记得检查下主从关系还在不在
#所有数据库安装节点包
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
测试
#4.51测试情况
masterha_check_ssh --conf=/etc/mha/app1.conf #执行测试是否能ssh 用的root用户写在配置文件里面了
masterha_check-repl --conf=/etc/mha/app1.cnf #测试主从是否正常
.....
MySQL Replication Heath is OK
masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf \
--ignore_last_failover #执行服务启动命令 执行之后这个终端就监听了 需要换个终端使用
#--remove参数是指主数据库宕机后把配置文件里面的对应[server数字]文本删除
#--ignore参数是指无论在任何时间,发生故障都进行故障切换,忽略8小时的时间限制(每次failover切换后会在管理目录生成文件app1.failover.complete ,下次在切换的时候会发现有这个文件导致切换不成功,需要手动清理掉。)
Tue Jan 26 13:03:50 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jan 26 13:03:50 2021 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jan 26 13:03:50 2021 - [info] Reading server configuration from /etc/mha/app1.cnf.. #监控中。。。。换个终端查看
换个终端查看
masterha_check_status --conf=/etc/mha/app1.cnf #执行服务状态查询
app1 (pid:48179) is running(0:PING_OK), master:192.168.4.53 #服务app1开启中 主数据库是192.168.4.53
ls /etc/mha/ #查看工作目录文件列表
app1.cnf app1.master_status.health manager.log master_ip_failover
# 服务状态文件
4.50客户端访问测试
#4.53添加访问数据库用户
mysql -uroot -p
create database db9;
create table db9.a(id int);
grant select ,insert on db9.* to wangxiaoming@'%' identified by '123465';
#4.50访问VIP来访问集群
mysql -h192.168.4.100 -uwangxiaoming -p
select * from db9.a;
insert into db9.a values(100);
select * from db9.a;
+------+
| id |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
exit
测试高可用
#停掉4.53主数据库的服务 模拟宕机
#4.53
systemctl stop mysqld
#4.51服务终端有反应了
Creating /var/tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to master53.000001
Tue Jan 26 14:36:28 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jan 26 14:36:28 2021 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jan 26 14:36:28 2021 - [info] Reading server configuration from /etc/mha/app1.cnf..
masterha_check_status --conf=/etc/mha/app1.cnf #查看服务状态
app1 is stopped(2:NOT_RUNNING). #服务停掉了
#4.50客户端依然可以链接VIP4.100 访问集群
#4.50
mysql -h192.168.4.100 -uwangxiaoming -p
insert into db9.a values(200);
select * from db9.a;
+------+
| id |
+------+
| 100 | #还是能访问数据 高可用体现了
| 200 |
+------+
#检查VIP在哪台数据库上
#4.53
ifconfig ens33:1
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:c1:b8:ec txqueuelen 1000 (Ethernet)
#4.54
ifconfig ens33:1
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:cf:42:b6 txqueuelen 1000 (Ethernet)
#4.55
ens33: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 00:0c:29:d0:8e:d2 txqueuelen 1000 (Ethernet)
#VIP4.100在4.55数据库服务器上了
#检查4.55是否是主服务器了
#4.54
mysql -uroot -p -e 'show slave status\G' | grep -i 192
Master_Host: 192.168.4.55 #4.55是主数据库服务器
抢救‘宕机’手动关闭的4.53数据库服务器
#4.53
systemctl start mysqld #修复了~
#4.55 备份数据
mysqldump -uroot -p --master-data db9 > db9.sql
scp db9.sql 192.168.4.53:/root/ #发送
#4.53 恢复
mysql -uroot -p db9 < /root/db9.sql
grep master55 /root/db9.sql #查看偏移量
CHANGE MASTER TO MASTER_LOG_FILE='master55.000002', MASTER_LOG_POS=401;
mysql -uroot -p #设置slave进程
change master to
master_host='192.168.4.55',
master_user='look',
master_password='123456',
master_log_file='master55.000002',
master_log_pos=401;
start slave; #启动slave线程
show slave status\G #查看IO、SQL线程 主服务器是4.55
exit
#4.51管理服务器重新配置配置文件
vim /etc/mmha/app1.cnf
===================
[server1]
hostname=192.168.4.51
port=3306
candidate_master=1 #重新写回来 被--remove_dead_master_conf参数删除了
==========================
masterha_check_ssh --conf=/etc/mha/app1.cnf #测试SSH
.....
MySQL Replication Health is OK.
masterha_stop --conf=/etc/mha/app1.cnf #停止管理服务
masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf \
--ignore_last_failover #重新启动服务
#换个终端查看下服务状态
#4.51
masterha_check_status --conf=/etc/mha/app1.cnf
总结
- 数据库服务器高可用 通过perl脚本来实现在集群里面master宕机之后实现VIP的可用(转移到新的master上)和slave变成新的master
- 能够成为主数据库的在配置文件中有candidate_master=1参数,需要开启binlog日志,全部统一授权replication slave权限的用户权限
- 数据库服务器之间,数据库与控制服务器之间要配置ssh密钥
- 数据库服务器都要安装mha-node包
- 控制服务器源码安装mha-manager
- masterha_check_ssh 检查ssh情况
- masterha_check_repl 检查主从情况
- masterha_manager 启动控制服务
- mha配置文件
- 指定控制日志
- 指定控制配置文件目录
- 指定故障修复脚本perl语言
- 访问数据库的用户
- ssh数据库服务器的用户和端口
- 配置主从的用户和密码
- 各数据库的IP 端口 是否能够变成主数据库
- 优化 半同步模式 每个数据库都开启半同步主从模块