MySQL MHA application practice (scheme practice)

Posted by itsjareds on Sun, 20 Oct 2019 17:10:18 +0200

1. Environment and conditions
Master slave schema environment already exists

host name IP address Master-slave role MHA role
leo1 192.168.3.2 Master MHA-node
leo2 192.168.3.3 slave MHA-node
leo3 192.168.3.4 slave MHA-node,MHA-manager
vip 192.168.3.6

Master-slave ensures the master-slave status is normal

mysql>show slave status\G;
IO And SQL Process is YES

Parameter change

relay_log_purge = 0                 #Do not automatically delete the relay log to facilitate downtime and data repair
log_bin = /data/log/leo-bin      #Open binlog from the database for data repair
expire_logs_days=7
log-slave-updates=1

2. Configure SSH
When MHA is running, it will connect to other node servers through SSH service to detect or copy data, so password free login is required in the cluster.
Execute the following commands in leo1-3 respectively:

    ssh-keygen -t dsa -p ' '  -f  ~/.ssh/id_dsa  > /dev/null 2>&1   ###Create key pair
    ssh-copy-id  -i ~/.ssh/id_dsa.pub 192.168.3.2
    ssh-copy-id  -i ~/.ssh/id_dsa.pub 192.168.3.3
    ssh-copy-id  -i ~/.ssh/id_dsa.pub 192.168.3.4

3. Install MHA Node
Install MHA Node for all mysql nodes

        1)install perl language
         yum install perl-DBD-MySQL  -y   ###Because MHA uses perl to develop
         2)install MHA Node
         rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
     //Download address
         https://code.google.com/archive/p/mysql-master-ha/downloads
         3)Create command soft connection for easy use:
         ls -s /app/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
         ls -s /app/mysql/bin/mysql  /usr/bin/mysql
         4)inspect MHA account
         grant all privileges on *.* to mha@'192.168.3%'  identified by 'mha';
         select user,host from mysql.user;

4,MHA-Manager
Manager can be installed on any machine.

    1)Use epel Source installation perl Environmental Science
        wget -O /etc/yum.repo.d/epel.repo  http://mirrors.aliyum.com/repo/eprl-6.repo
    2)Install management node package
        yum intall -u perl-Config-Tiny* epel-release perl-Log-Dispatch* perl-Parallel* per-Time*
    3)install MHA-Node
            rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
    4)install MHA-Manger
        rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

5. Configure MHA management node

        mkdir -p /etc/mha
        mkdir -p /var/log/mha/app1
        vi /etc/mha/app1.cnf
        [server default]
        manager_log=/var/log/mha/app1/manager.log   
        manager_workdir=/var/log/mha/app1.log
        mater_binlog_dir=/app/mysql/data   ##MHA saves binlog path of main database
        user=mha                                          ##Authorization of mysql database
        password=mha
        ping_interval=2                                ##Monitoring ping packet interval
        repl_user=rep                                  ##Primary and secondary replication users
        repl_password=leo123
        ssh_user=root
        repot_script=/usr/local/send_report      ##Trigger script after failure
        secondary_check_script=/usr/local/bin/masterha_secondary_check -s leo3 -s leo2 --user=root --master_host=leo1 --master_ip=192.168.3.2 --master_port=3306
        ########When manager cannot contact leo1, he will check the status of leo1 through leo2 and leo3.
        shutdown_script=""
        [server1]
        hostname=192.168.3.2
        port=3306
        [server2]
        hostname=192.168.3.4
        port=3306
        candidate_master=1             ##After setting this parameter, server2 will be upgraded to the main database first.
        check_repl_delay=0              ##Master-slave replication delay of MHA
        [server3]
        hostname=192.168.3.3
        port=3306

6. Startup and test
1) detect ssh password free login

    master_check_ssh -conf=/etc/mha/app1.cnf
2) detect the master-slave copy status
    master_check_repl -conf=/etc/mha/app1.cnf
3) start MHA
    nohup master_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failver < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
    ##Remove? Dead? Master? Conf delete the master information in the configuration file
    ##Ignore last failover

7. Configure VIP drift

   1)utilize keepalived
    2)Fill in the configuration file with the following parameters
    master_ip_failover_script=/usr/local/bin/master_ip_failover
    3)Add the following parameters to the script
    my = $vip= '192.168.3.6';
    my $key=  0
    my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
    my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

    chmod +x /etc/mha/master_ip_failover

Topics: MySQL ssh RPM Database