mysql高可用

1.什么是MHA

masterhight availabulity:基于主库的高可用环境下:主从复制 故障切换

主从的架构

MHA:最少要一主两从

mysql的单点故障问题,一旦主库崩溃,MHA可以在0-30秒内自动完成故障切换

MHA使用的是半同步复制,只要有一台服务器写入数据,就会自动提交给客户端

master崩溃,slave就会从主的二进制日志保存文件

slave会识别最新更新的日志

差异部分同步到slave

提升一个新的slave作为master

其他的slave继续和新的master同步

2.实验

实验准备:

1.mha架构塔键

2.模拟故障

3.故障恢复

MHA manager 192.168.233.10

master 192.168.233.21

slave1 192.168.233.22

slave2 192.168.233.23

node组件----------------->需要部署在所有服务器上。manager组件依赖于node组件,node组件监控mysql的状态

node组件靠ssh来进行通信

所有

systemctl stop firewalld

setenforce 0

master:

hostnamectl set-hostname master

su

slave1:

hostnamectl set-hostname slave1

su

slave2:

hostnamectl set-hostname slave2

su

主从复制

master:

vim /etc/my.cnf

server-id=1

log_bin=master-bin

log-slave-updates=true

slave1:

vim /etc/my.cnf

server-id = 2

log_bin = master-bin

relay-log = relay-log-bin

relay-log-index = slave-relay-bin.index

slave2:

vim /etc/my.cnf

server-id=3

log_bin=master-bin

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

所有:

systemctl restart mysqld

ln -s /usr/local/mysql/bin/mysql /usr/sbin/

ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

mysql -u root -p123456

grant replication slave on *.* to 'myslave'@'192.168.66.%' identified by '123456';

grant all privileges on *.* to 'mha'@'192.168.66.%' identified by 'manager';

grant all privileges on *.* to 'mha'@'master' identified by 'manager';

grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';

grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';

flush privileges;

master:

show master status;

slave1:

change master to master_host='192.168.66.15',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=2043;

start slave;

show slave status\G;

set global read_only=1; #设置数据库为只读模式

slave2:

change master to master_host='192.168.66.15',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=2043;

start slave;

show slave status\G;

set global read_only=1; #设置数据库为只读模式

终端测试

master:

create database kgc;

所有的服务器都要安装MHA的依赖环境,必须安装epel源

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

#######先安装node组件在装MHA

cd /opt/

把node拖进去所有文件

ls

tar zxvf mha4mysql-node-0.57.tar.gz

cd mha4mysql-node-0.57

ls

perl Makefile.PL

make && make install

test1:

cd /opt

高可用拖进去

tar zxvf mha4mysql-manager-0.57.tar.gz

cd mha4mysql-manager-0.57

perl Makefile.PL

make && make install

cd /usr/local/bin/

ls

masterha_check_ssh: 检查MHA的SSH的配置状况

masterha_check_repl:检查mysql的复制情况

masterha_manager:启动manager的脚本

masterha_check_status:检测MHA的运行状态

masterha_master_monitor:检测matser主机的情况,maters是否宕机。

masterha_master_switch:控制故障转移

masterha_conf_host:添加或者删除配置的server信息。

masterha_stop:停止MAH脚本

node组件

save_binary_logs 保存和复制mater的二进制日志

apply_diff_relay_logs:识别二进制日志当中的差异事件,然后发送给其他的slave

filter_mysqlbinlog:去除不必要的回滚(MHA已经不用了)

purge_relay_logs:同步之后清除中继日志(不会阻塞sql的线程)

node依赖ssh通信,每台主机实现ssh的免密登录

test1:

ssh-keygen -t rsa

一路回车

ssh-copy-id 192.168.66.15

yes

123

ssh-copy-id 192.168.66.16

yes

123

ssh-copy-id 192.168.66.17

yes

123

master:

ssh-keygen -t rsa

一路回车

ssh-copy-id 192.168.66.16

yes

123

ssh-copy-id 192.168.66.17

yes

123

slave1:

ssh-keygen -t rsa

一路回车

ssh-copy-id 192.168.66.15

yes

123

ssh-copy-id 192.168.66.17

yes

123

slave2:

ssh-keygen -t rsa

一路回车

ssh-copy-id 192.168.66.15

yes

123

ssh-copy-id 192.168.66.16

yes

123

test1:

cd /opt

ls

cd mha-man

ls

cd samples/

cd scripts/

ls

###

master_ip_failover:自动切换时管理vip的脚本

master_ip_online_change:在线切换时,vip的管理脚本

power_manager:故障发送之后,关闭master脚本

send_report:发送故障时的告警脚本

###

cp -a /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin

cd /usr/local/bin/

cd scripts/

ls

cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin

cd ..

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.66.100'; #设定vip地址

my $brdc = '192.168.66.255'; #设定vip的广播地址

my $ifdev = 'ens33'; #vip绑定的网卡

my $key = '1'; #ifconfig ens33:1 192.168.233.100/24

my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #ens33:1 192.168.233.100 up

my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #ens33:1 192.168.233.100 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";

}

mkdir /etc/masterha

cd /opt/mha4mysql-manager /samples/conf

###

app1.cnf

##

cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha

vim /etc/masterha/app1.cnf

100dd

[server default]

manager_log=/var/log/masterha/app1/manager.log #manager的管理日志

manager_workdir=/var/log/masterha/app1 #manager的工作目录

master_binlog_dir=/usr/local/mysql/data #保存master的二进制日志的位置,必须要和master保存的路口一致

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 #监控主库,发送ping包的时间间隔。1秒。尝试3次之后会切换到failover脚本进行自动切换

remote_workdir=/tmp #mysql在发送切换时,binlog的保存位置

repl_password=123456 #用户密码

repl_user=myslave #用户名

secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.66.16 -s 192.168.66.17

#从对主监听

shutdown_script="" #设置切换时,告警的脚本

ssh_user=root #远程登录的用户名

user=mha

[server1]

hostname=192.168.66.15

#主服务器

port=3306

[server2]

candidate_master=1 #设置候选的master主库崩溃会切换到server2 233.22

check_repl_delay=0 #即使设置了权重,但是从服务器的relay_logs落后master100M,也不会切换。设置0,会忽略延迟复制,直接升为主。强制切换到设定为候选master的服务器。

hostname=192.168.66.16

#备用主服务器

port=3306

[server3]

hostname=192.168.66.17

#从服务器2

port=3306

/sbin/ifconfig ens33:1 192.168.66.100/24

masterha_check_ssh -conf=/etc/masterha/app1.cnf

masterha_check_repl -conf=/etc/masterha/app1.cnf

master:

/sbin/ifconfig ens33:1 192.168.66.100/24

test1:

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 &

-remove_dead_master_conf:该参数代表当发生主从切换后,老的主库的 ip 将会从配置文件中移除。

--manger_log:日志存放位置。

--ignore_last_failover:在缺省情况下,如果 MHA 检测到连续发生宕机,且两次宕机间隔不足 8 小时的话,则不会进行自动切换。忽略时间间隔,只要发生故障就会切换

ps -elf | grep 81112

重新开个test1:

tail -f /var/log/masterha/app1/manager.log

master:

systemctl stop mysqld

slave1:

mysql -u root -p123456;

show master status;

master:

systemctl restart mysqld

mysql -u root -p123456

hange master to master_host='192.168.66.16',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1747;

start slave;

show slave status\G;

test1:

vim /etc/masterha/app1.cnf

masterha_check_ssh -conf=/etc/masterha/app1.cnf

masterha_check_repl -conf=/etc/masterha/app1.cnf

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 &

模拟故障

master:

ifconfig ens33:1 192.168.66.100/24

ip addr

systemctl stop mysqld

ip addr

slave1:

ip addr

故障修复:

master:

vim /etc/my.cnf

末尾改成

log_bin = master-bin

relay-log = relay-log-bin

relay-log-index = slave-relay-bin.index

systemctl restart mysqld.service

slave1:

vim /etc/my.cnf

末尾改成

log_bin = master-bin

log-slave-updates = true

systemctl restart mysqld.service

test1:

masterha_stop --conf=/etc/masterha/app1.cnf

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 192.168.66.15 -s 192.168.66.17

shutdown_script=""

ssh_user=root

user=mha

[server1]

hostname=192.168.66.16

port=3306

[server2]

candidate_master=1

check_repl_delay=0

hostname=192.168.66.15

port=3306

[server3]

hostname=192.168.66.17

port=3306

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 &

slave1:

mysql -u root -p123

show master status;

master:

mysql -u root -p123

stop slave;

change master to master_host='192.168.66.16',master_user='myslave',master_password='123456',master_log_file='master-bin.000003',master_log_pos=310;

reset slave;

start slave;

测试

你可能感兴趣的:(mysql,数据库)