当前位置:Gxlcms > 数据库问题 > MHA部署笔记(centos6+mysql5.6)

MHA部署笔记(centos6+mysql5.6)

时间:2021-07-01 10:21:17 帮助过:9人阅读

    CentOS6.5x86_64

    MySQL5.6.34编译安装版

    MHA版本:mha4mysql-manager-0.56-0.el6.noarch.rpm mha4mysql-node-0.56-0.el6.noarch.rpm

 

节点角色:

    node9310.1.20.93   默认主库

    node9410.1.20.94   从库1,原先主库宕机后可提升为主库【mha管理节点也部署在这台机器上】

    node9510.1.20.95   从库2,不允许提升为主库

   准备的VIP是 10.1.20.100/24

step1、配置主从关系

node93/etc/my.cnf配置文件部分关键地方:

[mysqld]

port            = 3306

socket          = /tmp/mysql.sock

datadir         = /bdata/data/nowdb2

 

innodb_file_per_table=ON

character-set-server = utf8

default_storage_engine = InnoDB

skip-innodb_adaptive_hash_index

 

master_info_repository = TABLE

relay_log_info_repository = TABLE

relay_log_recovery = 1 #crash safe

log-bin=mysql-bin

binlog_format=row

sync_binlog =1  #确保事务提交的时候BINLOG落盘

log-slave-updates

log_bin_trust_function_creators =1

binlog_rows_query_log_events=ON  #记录执行的语句到BINLOG query event

 

server-id=1020093

relay_log_purge=0

read_only=1

 

 

 

node94/etc/my.cnf配置文件部分关键地方:

[mysqld]

port            = 3306

socket          = /tmp/mysql.sock

datadir         = /bdata/data/nowdb2

 

innodb_file_per_table=ON

character-set-server = utf8

default_storage_engine = InnoDB

skip-innodb_adaptive_hash_index

 

master_info_repository = TABLE

relay_log_info_repository = TABLE

relay_log_recovery = 1 #crash safe

log-bin=mysql-bin

binlog_format=row

sync_binlog =1  #确保事务提交的时候BINLOG落盘

log-slave-updates

log_bin_trust_function_creators =1

binlog_rows_query_log_events=ON  #记录执行的语句到BINLOG query event

 

server-id=1020094

relay_log_purge=0

read_only=1

 

 

 

node95/etc/my.cnf配置文件部分关键地方:

[mysqld]

port            = 3306

socket          = /tmp/mysql.sock

datadir         = /bdata/data/nowdb2

 

innodb_file_per_table=ON

character-set-server = utf8

default_storage_engine = InnoDB

skip-innodb_adaptive_hash_index

 

master_info_repository = TABLE

relay_log_info_repository = TABLE

relay_log_recovery = 1 #crash safe

log-bin=mysql-bin

binlog_format=row

sync_binlog =1  #确保事务提交的时候BINLOG落盘

log-slave-updates

log_bin_trust_function_creators = 1

binlog_rows_query_log_events=ON  #记录执行的语句到BINLOG query event

 

server-id=1020095

relay_log_purge=0

read_only=1

 

 

node93上创建复制权限的账号,GRANT REPLICATION SLAVE ,REPLICATION CLIENT ON *.* TO ‘rpl‘@‘10.1.%.%‘ IDENTIFIED BY ‘rpl‘;

然后配置12从,(具体步骤略过)。

 

注意:我们要确保能成为主库的节点(node93node94)都存在主从同步账号,如果node94上不存在rpl账号,就到node94节点去手工添加即可。

 

 

主从关系建立好后,我们在master上创建个mha管理账号,后期会用到:

grant all on *.* to ‘mhauser‘@‘10.1.%.%‘ identified by ‘Abcd@1234‘;   

(管理账号要在node93node94node95所有节点都存在)

 

step2、安装MHA

因为MHA依赖于SSH,因此需要在3台主机之间建立SSH免秘钥登陆。步骤略过。

 

 

3个节点都安装perl包:

yum install perl perl-DBD-MySQL perl-CPAN perl-devel perl-Time-HiRes

 

node93-node95上都安装node包:

rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

 

node94上安装Manager(当然,我们在3个节点都安装上Manager包也没问题)

rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

 

 

node94初始化MHA

mkdir /etc/masterha/

vim /etc/masterha/app1.cnf 内容如下:

[server default]

user=mhauser

password=Abcd@1234

manager_workdir=/data/masterha/app1

manager_log=/data/masterha/app1/manager.log

remote_workdir=/data/masterha/app1

ssh_user=root

repl_user=rpl

repl_password=rpl

ping_interval=1

master_binlog_dir=/bdata/data/nowdb2/    # 这个路径和你的mysqlbinlog存放路径要一致

 

master_ip_failover_script=/etc/masterha/master_ip_failover

report_script=/etc/masterha/send_report

 

#通过第三方机器确认目标主库是否存活,不是必须的,就算没有也是能用

#secondary_check_script=masterha_secondary_check -s remote_host1 -s remote_host2

 

#故障发生后关闭主机的脚本,不是必须的,但是你要设置为空

# shutdown_script=""

 

 

#手动在线切换VIP脚本,不是必须的,就算没有也是能用,

#如果你有keepalived这种来做切换VIP就可以直接不用了

master_ip_online_change_script==/etc/masterha/master_ip_online_change

 

[server1]

hostname=10.1.20.93

candidate_master=1

 

[server2]

hostname=10.1.20.94

candidate_master=1

 

[server3]

hostname=10.1.20.95

no_master=1   # 禁止

 

 

node94上添加脚本/etc/masterha/master_ip_failover  (里面填写上相关的VIP的信息)

#!/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,

   $orig_master_ssh_port, $new_master_ssh_port

);

 

my $vip =‘10.1.20.100‘;  # Virtual IP

my $devic=‘eth0‘;

my $key = "0";

my$net_mask=‘255.255.255.0‘;

my $ssh_start_vip ="/sbin/ifconfig $devic:$key $vip netmask $net_mask";

my $ssh_stop_vip ="/sbin/ifconfig $devic:$key down";

my$mysql_conf="/etc/my.cnf";

my $open_readonly="/bin/sed-i ‘s/.*read_only.*/read_only=1/g‘ $mysql_conf ";

my $close_readonly="/bin/sed-i ‘s/.*read_only.*/read_only=0/g‘ $mysql_conf ";

my $open_relaylog_purge="/bin/sed-i ‘s/.*relay_log_purge.*/relay_log_purge=0/g‘ $mysql_conf ";

my$close_relaylog_purge="/bin/sed -i‘s/.*relay_log_purge.*/relay_log_purge=0/g‘ $mysql_conf ";

 

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,

   ‘orig_master_ssh_port=i‘ => \$orig_master_ssh_port,

   ‘new_master_host=s‘  => \$new_master_host,

   ‘new_master_ip=s‘    =>\$new_master_ip,

   ‘new_master_port=i‘  =>\$new_master_port,

   ‘new_master_ssh_port=i‘ => \$new_master_ssh_port,

);

 

exit &main();

 

sub main {

 

   print "\n\nIN SCRIPTTEST====$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 atglobal catalog database,

        # invalidate orig_master_ip here.

        my $exit_code = 1;

        eval {

            print "Disabling the VIP onold 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" ) {

 

        # all arguments are passed.

        # If you manage master ip address atglobal 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 "Enabling the VIP - $vipon 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 thescript.. OK \n";

       # `ssh $ssh_user\@cluster1 \"$ssh_start_vip \"`;

        exit 0;

   }

   else {

        &usage();

        exit 1;

   }

}

 

# A simple system call that enablethe VIP on the new master

sub start_vip() {

   `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;

   print "Disable read_only and relay_log_purge in my.cnf -  on the new master - $new_master_host\n";

   `ssh $ssh_user\@$new_master_host \" $close_readonly \"`;

   `ssh $ssh_user\@$new_master_host \" $close_relaylog_purge \"`;

   

}

# A simple system call thatdisable the VIP on the old_master

sub stop_vip() {

   `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;

  print "Enable read_only and relay_log_purge in my.cnf -  on the orig master - $orig_master_host\n";

   `ssh $ssh_user\@$orig_master_host \" $open_readonly \"`;

   `ssh $ssh_user\@$orig_master_host \" $open_relaylog_purge \"`;

}

 

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--orig_master_ssh_port=ssh_port --new_master_ssh_port = ssh_port\n";

}

 

 

node94上添加脚本/etc/masterha/send_report(里面填写上相关的smtp账号的信息)

#!/usr/bin/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 scriptand is not complete. Modify the script based on your environment.

 

use strict;

use warnings FATAL => ‘all‘;

use Mail::Sender;

use Getopt::Long;

 

#new_master_host and new_slave_hostsare set only when recovering master succeeded

my ( $dead_master_host,$new_master_host, $new_slave_hosts, $subject, $body ,$conf);

my$smtp=‘smtp.exmail.qq.com‘;

my$mail_from=‘zabbix@xxxx.com‘;

my$mail_user=‘zabbix@xxxx.com‘;

my $mail_pass=‘xxxxxxx‘;

my$mail_to=[‘lixxxx@xxxx.com‘];

GetOptions(

 ‘orig_master_host=s‘ => \$dead_master_host,

 ‘new_master_host=s‘  =>\$new_master_host,

 ‘new_slave_hosts=s‘  =>\$new_slave_hosts,

 ‘subject=s‘          =>\$subject,

 ‘body=s‘             => \$body,

 ‘conf=s‘             => \$conf,

);

 

mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);

check_if_sendmail_ok(‘/tmp/monitormail.log‘);

 

sub mailToContacts {

   my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;

   open my $DEBUG, "> /tmp/monitormail.log"

        or die "Can‘t open the debug      file:$!\n";

   my $sender = new Mail::Sender {

        ctype       => ‘text/plain; charset=utf-8‘,

        encoding    => ‘utf-8‘,

        smtp        => $smtp,

        from        => $mail_from,

        auth        => ‘LOGIN‘,

        TLS_allowed => ‘0‘,

        authid      => $user,

        authpwd     => $passwd,

        to          => $mail_to,

        subject     => $subject,

        debug       => $DEBUG

   };

 

   $sender->MailMsg(

        {  msg   => $msg,

            debug => $DEBUG

        }

   ) or print $Mail::Sender::Error;

   return 1;

}

 

sub check_if_sendmail_ok{

    #>>250 2.0.0 Ok: queued as 3532C6DA009D

    #<<QUIT

    #>>221 2.0.0 Bye

    my$logf = shift;

    openRLOG, $logf or die "cannot open file $logf.\n";

    my@log = <RLOG>;

    closeRLOG;

    my$val = 0;

    if($log[$#log]=~ m/\>\>\s221\s.*\sBye/){

       print"Meet Bye.\t";

       $val++;

    }

    if($log[$#log-1]=~ m/\<\<\sQUIT/){

     

人气教程排行