MySQL server clusters how to build MHA?

Foreword

  • Currently in terms of high availability MySQL MHA is a relatively mature solution
  • But often an error in the process of building, and the MHA build a comprehensive master-slave replication, so the installation to be thinking clearly before MHA

A: MHA Overview

1.1: What is MHA?

  • Japan's DeNA company youshimaton (now working in Facebook Inc.) Development
  • Excellent as a failover at the primary and high availability environment from MySQL high availability software upgrade
  • Failover support
  • In MySQL failover process, the MHA can be done automatically in 30 seconds from 0 to the fault switching operation of the database, and performing the handover procedure failure, the MHA to ensure consistency of the data to the maximum extent, in order to achieve real in the sense of availability
  • MHA also offers online master library function switch can be switched safely to the main library currently running a new main library (library by lifting from the main library), can be completed within about 0.5-2 seconds

1.2: MHA composition

  • MHA Manager (management node)
    • For receiving an external signal, to monitor the operating state of the lower data node
  • MHA Node (node ​​data)
    • Work units, responsible for specific work

1.3: MHA What are the characteristics?

  • Automatic failover process, MHA attempt to save the binary log from the primary server goes down, the greatest degree of assurance without losing data
  • Use MySQL55 semi-synchronous replication, can greatly reduce the risk of data loss

1.4: The reason MHA formed

  • Single point of failure of traditional master-slave architecture mysql problem

    mark

  • Traditional architecture, there is only one main server mysql, so when there is a single point of failure, the entire server cluster will be crippled

  • To address this situation, we need when the primary server goes down, the re-establishment of a master server, which is responsible for monitoring the work

Two: MHA experiment

2.1: experimental environment

2.2: Topology

mark

2.3: Purpose

  • , Carried out by MHA monitoring MySQL database in the event of a fault automatically switch does not affect business
  • When the primary database fails, an alternative main database automatically become the master library

2.4: experiment

  • Because the operating system is centos7, so you want to download MHA 0.57 version
2.4.1: install gmake to compile software
[root@slave2 ~]# mount.cifs //192.168.11.11/ccc /mnt
Password for root@//192.168.11.11/ccc:  
[root@slave2 ~]# cd /mnt/shuju数据库/mha
[root@slave2 mha]# 
[root@slave2 mha]# tar zxvf cmake-2.8.6.tar.gz -C /opt
[root@slave2 mha]# cd /opt/cmake-2.8.6/
[root@slave2 cmake-2.8.6]# ./configure 
[root@slave1 cmake-2.8.6]# gmake
[root@slave1 cmake-2.8.6]# gmake install
2.4.2: build mysql master-slave replication environment
  • https://blog.csdn.net/CN_TangZheng/article/details/103897251

  • Remember to turn off the firewall

  • Called master-slave replication environment mysql synchronization user: myslave Password: 123456

  • Additional adjustment

    • All mha database authorized users authority to operate the database, each a mysql database to be set up

      '//下面三条授权按理论是不用添加的,但是做案例实验环境时候诵过MHA检查MySQL主从有报错,报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权;'
      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服务器的授权增加一个给mha管理'
      mysql> grant all privileges on *.* to 'mha'@'192.168.79.%' identified by 'manager';
      '//主备mysql服务器添加用户和权限'
      '//192.168.79.0段的从服务器拥有复制权限,可以使用myslave身份123456密码复制所有的库和表'
      mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.79.%' IDENTIFIED BY '123456';
      
    • slave1 slave2 and two from the database to read-only

      mysql> set global read_only=1;
      
    • All slave node configuration binary logs, comment out the default character set of client segments

      [root@slave1 ~]# vim /etc/my.cnf
          [mysqld]段落添加
      log-bin=slave1-bin	'//添加二进制日志'
      所以slave节点都要配置,我就不一一展示了
          '//从服务器上都注释掉client段的默认字符集'
      [root@slave1 ~]# vim /etc/my.cnf
      [client]
      #default-character-set=utf8	'//注释掉此'
      
    • Create a soft link

      所有节点创建软连接
      ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/
      创建mysql软连接
      ln -s /usr/local/mysql/bin/mysql /usr/local/bin/
      
2.4.3: All nodes installed environment and Node Manager components and assemblies
  • Mha be installed on all servers dependent on the environment, showing only here mha_manager installation

    [root@mha_manager ~]# yum install epel-release --nogpgcheck -y
    [root@mha_manager ~]# yum install -y perl-DBD-MySQL \
    perl-Config-Tiny \
    perl-Log-Dispatch \
    perl-Parallel-ForkManager \
    perl-ExtUtils-CBuilder \
    perl-ExtUtils-MakeMaker \
    perl-CPAN
    
  • All server components are installed node installation, display only here to install the master server

  • [root@master ~]# tar zxvf /mnt/shuju数据库/mha/mha4mysql-node-0.57.tar.gz 
    [root@master ~]# cd mha4mysql-node-0.57/
    [root@master mha4mysql-node-0.57]# yum install perl-Module-Install -y
    [root@master mha4mysql-node-0.57]# perl Makefile.PL 
    ==> Auto-install the 1 mandatory module(s) from CPAN? [y] y	'//输入y'
    [root@master mha4mysql-node-0.57]# make
    [root@master mha4mysql-node-0.57]# make install
    
  • Only mha_manager server installation manager components

    [root@mha_manager ~]# tar zxvf /mnt/shuju数据库/mha/mha4mysql-manager-0.57.tar.gz 
    [root@mha_manager ~]# cd mha4mysql-manager-0.57/
    [root@mha_manager mha4mysql-manager-0.57]# perl Makefile.PL 
    [root@mha_manager mha4mysql-manager-0.57]# make
    [root@mha_manager mha4mysql-manager-0.57]# make install
    
2.4.4: SSH face interaction between the configuration node landing
  • mha_manager host configuration validation key to all nodes in the database

    [root@mha_manager ~]# ssh-keygen -t rsa
    	'//创建非对称密钥对'
        '//因为想要免密登陆,所以三个都直接回车就行,不需要设置密码'
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    '//上传秘钥到节点服务器'
    [root@mha_manager ~]# ssh-copy-id 192.168.79.134
    Are you sure you want to continue connecting (yes/no)? yes
    [email protected]'s password: 	//输入密码'
        '//相同方法建立其他服务器'
    [root@mha_manager ~]# ssh-copy-id 192.168.79.135
    [root@mha_manager ~]# ssh-copy-id 192.168.79.136
    
  • Arranged on the master database and node slave1 secret authentication-free slave2

    [root@master mha4mysql-node-0.57]# ssh-keygen -t rsa	'//创建非对称密钥对'
        '//用上述方法建立其他服务器'
    [root@master mha4mysql-node-0.57]# ssh-copy-id 192.168.79.135
    [root@master mha4mysql-node-0.57]# ssh-copy-id 192.168.79.136
    
  • On slave1 node configuration database to the master secret authentication-free and slave2

    [root@slave1 ~]# ssh-keygen -t rsa
    [root@slave1 ~]# ssh-copy-id 192.168.79.134
    [root@slave1 ~]# ssh-copy-id 192.168.79.136
    
  • On slave2 node configuration database to the master secret authentication-free and slave1

    [root@slave2 ~]# ssh-keygen -t rsa
    [root@slave2 ~]# ssh-copy-id 192.168.79.134
    [root@slave2 ~]# ssh-copy-id 192.168.79.135
    
2.4.5: Configuration MHA-Manager component
  • Copy the relevant script to / usr / local / bin directory

    [root@mha_manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
    [root@mha_manager samples]# ls -l /usr/local/bin/scripts/
    -rwxr-xr-x. 1 1001 1001  3648 5月  31 2015 master_ip_failover  自动切换时 VIP管理的脚本
    -rwxr-xr-x. 1 1001 1001  9870 5月  31 2015 master_ip_online_change  在线切换VIP的管理 
    -rwxr-xr-x. 1 1001 1001 11867 5月  31 2015 power_manager  故障发生后关闭主机的脚本
    -rwxr-xr-x. 1 1001 1001  1360 5月  31 2015 send_report  因故障切换后发送报警的脚本
    [root@mha_manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/	'//自动切换时 VIP管理的脚本'
    
  • Modify master_ip_failover script

    [root@mha_manager ~]# 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.79.100';
    my $brdc = '192.168.79.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 software to create a directory and copy the configuration file

    [root@mha_manager ~]# mkdir /etc/masterha
    [root@mha_manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
    [root@mha_manager ~]# 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=123456
    repl_user=myslave
    secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.79.135 -s 192.168.79.136
    shutdown_script=""
    ssh_user=root
    user=mha
    
    [server1]
    hostname=192.168.79.134
    port=3306
    
    [server2]
    candidate_master=1
    hostname=192.168.79.135
    check_repl_delay=0
    port=3306
    
    [server3]
    hostname=192.168.79.136
    port=3306
    
2.4.6: Test SSH connection from the master mysql
  • Verification key pair

    [root@mha_manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
    Fri Jan 10 01:14:10 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
    Fri Jan 10 01:14:10 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
    Fri Jan 10 01:14:10 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
    Fri Jan 10 01:14:10 2020 - [info] Starting SSH connection tests..
    Fri Jan 10 01:14:11 2020 - [debug] 
    Fri Jan 10 01:14:10 2020 - [debug]  Connecting via SSH from [email protected].135(192.168.79.135:22) to [email protected].136(192.168.79.136:22)..
    Fri Jan 10 01:14:11 2020 - [debug]   ok.
    Fri Jan 10 01:14:12 2020 - [debug] 
    Fri Jan 10 01:14:10 2020 - [debug]  Connecting via SSH from [email protected].136(192.168.79.136:22) to [email protected].135(192.168.79.135:22)..
    Fri Jan 10 01:14:11 2020 - [debug]   ok.
    Fri Jan 10 01:14:12 2020 - [info] All SSH connection tests passed successfully.
    
  • Test mysql master-slave connection

    [root@mha_manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
    
  • Related error, please refer to my blog

    https://blog.csdn.net/CN_TangZheng/article/details/103925515

2.4.7: Configuring Virtual IP, start MHA
  • Add a virtual IP

    [root@master ~]# /sbin/ifconfig ens33:1 192.168.79.100/24
    
  • Start MHA

    [root@mha_manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > 
    [root@mha_manager ~]# /var/log/masterha/app1/manager.log 2>&1 &
    '//查看到当前的master节点是谁'
    [root@mha_manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
    '//查看当前日志信息'
    [root@mha_manager ~]# cat /var/log/masterha/app1/manager.log 
    
2.4.8: Analog fault
  • Start monitoring observation logging

    [root@mha_manager ~]# tailf /var/log/masterha/app1/manager.log 
    
  • Turn off the master server

    [root@master ~]# pkill -9 mysqld 
    

2.5: experimental verification

2.5.1: Master changed successfully
  • In this case found that the primary server has been changed

    slave1 View

    [root@slave1 ~]# ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.79.135  netmask 255.255.255.0  broadcast 192.168.79.255
            inet6 fe80::159a:a8d1:5769:74d0  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:34:57:c1  txqueuelen 1000  (Ethernet)
            RX packets 347068  bytes 28229347 (26.9 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 528201  bytes 67755670 (64.6 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.79.100  netmask 255.255.255.0  broadcast 192.168.79.255
            ether 00:0c:29:34:57:c1  txqueuelen 1000  (Ethernet)
    
  • mha_manager View

    '//会动态显示后续信息'
    Generating relay diff files from the latest slave succeeded.
    192.168.79.136(192.168.79.136:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.79.135(192.168.79.135:3306)
    192.168.79.135(192.168.79.135:3306): Resetting slave info succeeded.
    Master failover to 192.168.79.135(192.168.79.135:3306) completed successfully.
    
2.5.2: Try to connect from the client through the virtual IP database
  • Mysql installed directly on mha_manager

    [root@mha_manager ~]# yum install mysql -y
    
  • Vip extraction on the database server

    [root@slave1 ~]# mysql -uroot -p
    Enter password: 
    mysql> grant all on *.* to 'root'@'%' identified by 'abc123';
    
  • Log on with a virtual IP on the client (mha_manager)

    [root@mha_manager ~]# mysql -h 192.168.79.100 -uroot -pabc123
    MySQL [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | yiku               |
    | mysql              |
    | performance_schema |
    | erku              |
    | sanku               |
    | sys                |
    +--------------------+
    8 rows in set (0.00 sec)
    
Published 94 original articles · won praise 60 · views 10000 +

Guess you like

Origin blog.csdn.net/CN_TangZheng/article/details/103925969