Arquitectura de alta disponibilidad de la base de datos MHA

Documento de instalación de MHA

Preparar cuatro servidores
192.168.126.177 administrador
192.168.126.178 maestro
192.168.126.179 esclavo
192.168.126.180 esclavo

Cuatro servidores necesitan realizar una operación ssh sin secretos:
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

178, 179, 180 necesitan instalar mariadb

yum -y install mariadb mariadb-server

Modificación del archivo de configuración de Mariadb

178. Operaciones en el maestro:
Inserte la descripción de la imagen aquí


```bash=
server-id=1
log-bin=mysql-bin

179, operación desde 1:

Inserte la descripción de la imagen aquí

server-id=2
log-bin=slave1-bin
relay-log=slave1-log

180, de la operación el 2:

Inserte la descripción de la imagen aquí

server-id=3
log-bin=slave2-bin
relay-log=slave2-log

Abra la base de datos (mariadb)

systemctl start mariadb

Ingrese a la base de datos para realizar la replicación maestro-esclavo:

Operación en el maestro 178:
Inserte la descripción de la imagen aquí

grant all on *.* to 'tom'@'192.168.126.%' identified by '123';

Operación de 179 y 180 desde arriba:
Inserte la descripción de la imagen aquí

change master to master_host='192.168.126.178,master_user='tom',master_password='123',master_log_file='slave1-bin.000003',master_log_pos=519;

Inserte la descripción de la imagen aquí

set global read_only=1;

Operaciones necesarias para maestro y esclavo 178, 179 y 180:

Inserte la descripción de la imagen aquí

grant all privileges on *.* to 'tom'@'192.168.126.% identified  by '123';

Las cuatro estaciones realizan una operación de sincronización horaria:

yum -y install ntpdate

Inserte la descripción de la imagen aquí

ntpdate ntp.aliyun.com

Inserte la descripción de la imagen aquí

Las cuatro máquinas se instalan con nodos de nodo y funcionan de la misma manera:

yum -y install  perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-DBD-MySQL perl-devel perl-CPAN

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

mkdir -p /etc/mha
tar zxf mha4mysql-node-0.57.tar.gz -C /etc/mha/
mv /etc/mha/mha4mysql-node-0.57/ /etc/mha/node
cd  /etc/mha/node/
perl Makefile.PL
make && make install

177 Instale el nodo administrador:

yum -y install epel-release --nogpgcheck

Inserte la descripción de la imagen aquí

yum install -y perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

tar zxf mha4mysql-manager-0.57.tar.gz -C /etc/mha/
mv /etc/mha/mha4mysql-manager-0.57/ /etc/mha/manager
cd /etc/mha/manager/
perl Makefile.PL
make && make install

Inserte la descripción de la imagen aquí

[server default]
manager_workdir=/etc/mha/app1
manager_log=/etc/mha/app1/manager.log
master_binlog_dir="/var/lib/mysql"
remote_workdir=/etc/mha/app1
master_ip_failover_script=/etc/mha/master_ip_failover
master_ip_online_change_script=/etc/mha/master_ip_online_change
report_script=/etc/mha/send_report
user=tom
password=123
repl_user=tom
repl_password=123
ping_interval=1
secondary_check_script= masterha_secondary_check -s 192.168.181.168 -s 192.168.181.169


[server1]
hostname=192.168.181.142
port=3306
ssh_port=22

[server2]
hostname=192.168.181.168
port=3306
ssh_port=22
candidate_master=1
check_repl_delay=0

[server3]
hostname=192.168.181.169
port=3306
no_master=1
ssh_port=22

Inserte la descripción de la imagen aquí

179,180 dos esclavos

Inserte la descripción de la imagen aquí

set global relay_log_purge=0;

Inserte la descripción de la imagen aquí

mkdir /var/lib/mysql/logs1
ln /var/lib/mysql/relay-log.* /var/lib/mysql/logs1/

Inserte la descripción de la imagen aquí

#!/bin/bash
user=root

passwd=123

port=3306

log_dir='/var/lib/mysql/'

work_dir='/var/lib/mysql/logs1'

purge='/usr/local/bin/purge_relay_logs'

if [ ! -d $log_dir ]

then

   mkdir $log_dir -p

fi

$purge --user=$user --password=$passwd --disable_relay_log_purge --port=$port --host=localhost --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

prueba

mysqladmin -uroot password 123
purge_relay_logs --user=root --host=localhost  --port=3306 --password=123 -disable_relay_log_purge --workdir=/var/lib/mysql/
crontab -e
0 0 */3 * * sh /etc/auto_clean_relay_log.sh

Inserte la descripción de la imagen aquí

177179180 gerente + dos esclavos
Inserte la descripción de la imagen aquí

vim / etc / mha / master_ip_failover cambia automáticamente el script

    #!/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.181.111/24';

                my $key = '0';

                my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";

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

                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 \"`;

                }

                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";

                }

vim / etc / mha / master_ip_online_change Cambiar manualmente los scripts

    #!/usr/bin/env perl

                use strict;

                use warnings FATAL =>'all';

                use Getopt::Long;

                my $vip = '192.168.181.111/24'; # Virtual IP

                my $key = "0";

                my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";

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

                my $exit_code = 0;

                my (

                $command, $orig_master_is_new_slave, $orig_master_host,

                $orig_master_ip, $orig_master_port, $orig_master_user,

                $orig_master_password, $orig_master_ssh_user, $new_master_host,

                $new_master_ip, $new_master_port, $new_master_user,

                $new_master_password, $new_master_ssh_user,

                );

                GetOptions(

                'command=s' => \$command,

                'orig_master_is_new_slave' => \$orig_master_is_new_slave,
           'orig_master_host=s' => \$orig_master_host,

                'orig_master_ip=s' => \$orig_master_ip,

                'orig_master_port=i' => \$orig_master_port,

                'orig_master_user=s' => \$orig_master_user,

                'orig_master_password=s' => \$orig_master_password,

                'orig_master_ssh_user=s' => \$orig_master_ssh_user,

                '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,

                'new_master_ssh_user=s' => \$new_master_ssh_user,

                );

                exit &main();

                sub main {
    
    

                #print "\n\nIN SCRIPT TEST====$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 at global catalog database,
              # invalidate orig_master_ip here.

                my $exit_code = 1;

                eval {
    
    

                print "\n\n\n***************************************************************\n";

                print "Disabling the VIP - $vip on old master: $orig_master_host\n";

                print "***************************************************************\n\n\n\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 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 {
    
    

                print "\n\n\n***************************************************************\n";

                print "Enabling the VIP - $vip on new master: $new_master_host \n";

                print "***************************************************************\n\n\n\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";

                `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;

                exit 0;
 }

                else {
    
    

                &usage();

                exit 1;

                }

                }

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

                sub start_vip() {
    
    

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

                }

                # A simple system call that disable the VIP on the old_master

                sub stop_vip() {
    
    

                `ssh $orig_master_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=po

                rt -new_master_host=host -new_master_ip=ip -new_master_port=port\n";
 }
   

script de alarma vim / etc / mha / send_report

    #!/usr/bin/perl

                use strict;

                use warnings FATAL => 'all';

                use Mail::Sender;

                use Getopt::Long;

                #new_master_host and new_slave_hosts are set only when recovering master succeeded

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

my $smtp='smtp.qq.com';

my $mail_from='[email protected]';

my $mail_user='[email protected]';

                my $mail_pass='xxxxxxxxxxxx';

my $mail_to=['[email protected]'];

                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,

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

            

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;

            }

            # Do whatever you want here

            exit 0;

177 gerente:

 /etc/mha/manager/bin/masterha_check_ssh --conf=/etc/mha/app1.cnf

Inserte la descripción de la imagen aquí

 /etc/mha/manager/bin/masterha_check_repl --conf=/etc/mha/app1.cnf

Inserte la descripción de la imagen aquí

Las siguientes condiciones son correctas

178 maestros

ifconfig ens33:0 192.168.126.111 netmask 255.255.255.0
130 manager

Inserte la descripción de la imagen aquí

puesta en marcha

nohup /etc/mha/manager/bin/masterha_manager --conf=/etc/mha/app1.cnf --ignore_last_failover >/tmp/mha_manager.log < /dev/null 2>&1 &

Comprobar estado

/etc/mha/manager/bin/masterha_check_status --conf=/etc/mha/app1.cnf

prueba

tail -300f /etc/mha/app1/manager.log

Inserte la descripción de la imagen aquí

Cerrar 178 mysql principal

Inserte la descripción de la imagen aquí

179 servidores de

Vip pasó a la máquina 179 y se convirtió en el nuevo maestro, y luego activó el mariadb del antiguo maestro 178, actuando como maestro-esclavo para 178 y 179

Show master status;

Inserte la descripción de la imagen aquí

178
Inserte la descripción de la imagen aquí

177
Modificar el archivo de configuración
Inserte la descripción de la imagen aquí

Iniciar mha:

 nohup /etc/mha/manager/bin/masterha_manager --conf=/etc/mha/app1.cnf --ignore_last_failover >/tmp/mha_manager.log < /dev/null 2>&1 &

Puede ver el estado del maestro actual:

/etc/mha/manager/bin/masterha_check_status --conf=/etc/mha/app1.cnf

Luego ve a cerrar 178mariadb:

Systemctl stop mariadb

Vaya al servidor 178 para verificar, VIP pasó a 179, y 179 se convirtió en el nuevo maestro.
Luego abra el mariadb de 178 para actuar como maestro y esclavo para 178 y 179.

Solución de informe de error: mensaje de error:

Inserte la descripción de la imagen aquí

solución:

grant all on *.* to tom@'192.168.126.%' identified by '123';

Supongo que te gusta

Origin blog.csdn.net/cui_love_/article/details/115359696
Recomendado
Clasificación