1. 程式人生 > >MariaDB的備份與主從、高可用實踐

MariaDB的備份與主從、高可用實踐

1、編寫指令碼,支援讓使用者自主選擇,使用mysqldump還是xtraback全量備份。

[root@test-centos7-node1 scripts]# cat chose_backup_mysql.sh 
#!/bin/bash
#
#************************************************************************
#Author:                qiuhom
#QQ:                    467697313
#mail:                  [email protected]
#Date:                  2020-01-12
#FileName:             chose_backup_mysql.sh
#URL:                   https://www.cnblogs.com/qiuhom-1874/
#Description:         
#Copyright (C):        2020 All rights reserved
#************************************************************************

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

fun_mysqldump(){
        if `which mysqldump &> /dev/null` ;then
                mysqldump_cmd=`which mysqldump`
        else
                yum_cmd_path=`which yum`
                $yum_cmd_path  install mariadb -y &> /dev/null
                [ $? -eq 0 ] && echo "mysqldump is installed ,please reselect " && exit 1
        fi
        if [ "$passwd" != "null" ];then
                ${mysqldump_cmd} -u$user -p$passwd -h$host -A --compact > $backup_file
        else
                 ${mysqldump_cmd} -u$user  -h$host -A --compact > $backup_file
        fi
        [ $? -eq 0 ] && action " backup successful ,please cat $backup_file" /bin/true || action "mariadb  backup is failed " /bin/false
        exit 0
}

try_connection_mariadb(){
        if [ "$2" != "null" ];then
                if `mysql -u"$1" -p"$2" -h"$3" -e "show databases"  &> /dev/null` ;then
                        user=$1
                        passwd=$2
                        host=$3
                else
                        echo "connection is lose , please check user or passwd or host " && exit 3
                fi
        else
                if `mysql -u"$1" -h"$3" -e "show databases"  &> /dev/null` ;then
                        user=$1
                        host=$3
                else
                        echo "connection is lose , please check user  or host " && exit 3
                fi
        fi
}

check_backup_path(){
        [ $# -eq 0 ] && backup_file=""
        if [[ $1 =~ ^(\/.*\/)$ ]];then
                [ ! -e $1 ] && mkdir -p $1
                backup_file="${1}all_backup.sql"
        elif [[ $1 =~ ^([^\/].*\/)$  ]];then
                backup_path="${PWD}/$1"
                [ ! -e ${backup_path} ] && mkdir -p ${backup_path}
                backup_file="${backup_path}all_backup.sql"
        elif [[ $1 =~ ^(\/.*[^\/]$) ]];then
                dir=`dirname $1`
                [ ! -e "$dir" ] && mkdir -p $dir
                backup_file="$1"
        elif [[ $1 =~ ^([^\/].*[^\/]$) ]];then
                backup_path="${PWD}/$1"
            [ ! -e `dirname ${backup_path}` ] && mkdir -p `dirname ${backup_path}`
                backup_file="${backup_path}"
        else
                echo "you input backup file is error" && exit 4
        fi
}

set_default_user_pass_host(){
        [ "$1" != "" ] && user=$user || user=$USER
        [ "$2" != "" ] && passwd=$passwd || passwd="null"
        [ "$3" != "" ] && host=$host || host="localhost"
}

input_user_passwd_host(){
        read -p "please input  user(default $USER):" user
        read -p "please input  passwd(default 'null'):" passwd
        read -p "please input  host(default 127.0.0.1):" host
}

fun_xtrabackup(){
        [ ! -e `which xtrabackup &> /dev/null` ] && yum install percona-xtrabackup -y &> /dev/null
        [ ! -e $backup_dir ] && mkdir -p $backup_dir
        if [ "$passwd" != "null" ];then
                xtrabackup --user=$1 --password=$2 --host=$3 --backup --target-dir=$4 &> /dev/null
        else
                xtrabackup --user=$1 --host=$3 --backup --target-dir=$4 &> /dev/null
        fi
        [ $? -eq 0 ] && action "xtrabackup completed OK!" /bin/true || action "xtrabackup completed failed" /bin/false 
        exit
}

check_target_dir(){
        [ $# -eq 0 ] && backup_dir=""
        if [[ $1 =~ ^(\/.*\/)$ ]];then
                [ ! -e $1 ] && mkdir -p $1
                backup_dir=$1
        elif [[ $1 =~ ^([^\/].*\/)$  ]];then
                backup_path="${PWD}/$1"
                [ ! -e ${backup_path} ] && mkdir -p ${backup_path}
                backup_dir="${backup_path}"
        else
                echo "target-dir must is directory " && exit 5
        fi
}


while true
        do
                cat << EOF
                        Please input a number choose you backup tool
                        1.mysqldump
                        2.xtrabackup
                        3.quit
EOF
        read -p "you choose:" choose
        case $choose in
                1)
                        input_user_passwd_host
                        set_default_user_pass_host $user $passwd $host
                        if try_connection_mariadb $user $passwd $host ;then
                                read -p "please input mariadb backup file path(default $HOME/backup.all.sql):" backupfile_path
                                [ "$backupfile_path" != "" ] && check_backup_path $backupfile_path || backup_file="$HOME/backup.all.sql"
                                fun_mysqldump $user $passwd $host $backup_file
                        fi 
                        ;;
                2)
                        input_user_passwd_host
                        set_default_user_pass_host $user $passwd $host
                        if try_connection_mariadb $user $passwd $host ;then
                                read -p "please input target-dir (default $HOME/backup/):" target_dir
                                [ "$target_dir" != "" ] && check_target_dir $target_dir || backup_dir="$HOME/backup/"
                                fun_xtrabackup $user $passwd $host $backup_dir
                        fi
                        ;;
                3)
                        echo "bye !!" && exit 6
                        ;;
                *)
                        echo "choose error" && exit 7
                        ;;
        esac

        done



[root@test-centos7-node1 scripts]# 

  驗證:

[root@test-centos7-node1 scripts]# ls /
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  snap  srv  sys  tmp  usr  var
[root@test-centos7-node1 scripts]# sh chose_backup_mysql.sh 
                        Please input a number choose you backup tool
                        1.mysqldump
                        2.xtrabackup
                        3.quit
you choose:1
please input  user(default root):test
please input  passwd(default 'null'):admin
please input  host(default 127.0.0.1):192.168.0.10
please input mariadb backup file path(default /root/backup.all.sql):/backup/mariadb/all.sql
 backup successful ,please cat /backup/mariadb/all.sql     [  OK  ]
[root@test-centos7-node1 scripts]# ll /backup/mariadb/all.sql 
-rw-r--r--. 1 root root 512222 Jan 12 10:01 /backup/mariadb/all.sql
[root@test-centos7-node1 scripts]# ll /
total 20
drwxr-xr-x.   3 root root   21 Jan 12 10:01 backup
lrwxrwxrwx.   1 root root    7 Jan  1 07:19 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jan  1 07:24 boot
drwxr-xr-x.  20 root root 3280 Jan 11 22:55 dev
drwxr-xr-x.  76 root root 8192 Jan 12 08:41 etc
drwxr-xr-x.   2 root root    6 Jan 12 07:27 home
lrwxrwxrwx.   1 root root    7 Jan  1 07:19 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Jan  1 07:19 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Nov  5  2016 media
drwxr-xr-x.   2 root root    6 Nov  5  2016 mnt
drwxr-xr-x.   2 root root    6 Nov  5  2016 opt
dr-xr-xr-x. 129 root root    0 Jan 11 22:54 proc
dr-xr-x---.   5 root root 4096 Jan 12 09:56 root
drwxr-xr-x.  25 root root  720 Jan 12 08:53 run
lrwxrwxrwx.   1 root root    8 Jan  1 07:19 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Jan 11 03:24 snap
drwxr-xr-x.   2 root root    6 Nov  5  2016 srv
dr-xr-xr-x.  13 root root    0 Jan 11 22:55 sys
drwxrwxrwt.   9 root root  280 Jan 12 10:00 tmp
drwxr-xr-x.  13 root root  155 Jan  1 07:19 usr
drwxr-xr-x.  19 root root  267 Jan  1 07:24 var
[root@test-centos7-node1 scripts]# ll /backup/
total 0
drwxr-xr-x. 2 root root 21 Jan 12 10:01 mariadb
[root@test-centos7-node1 scripts]# sh chose_backup_mysql.sh 
                        Please input a number choose you backup tool
                        1.mysqldump
                        2.xtrabackup
                        3.quit
you choose:2
please input  user(default root):       
please input  passwd(default 'null'):
please input  host(default 127.0.0.1):
please input target-dir (default /root/backup/):/backup/xtrabackups/
xtrabackup completed OK!                                   [  OK  ]
[root@test-centos7-node1 scripts]# ll /backup/
total 0
drwxr-xr-x. 2 root root  21 Jan 12 10:01 mariadb
drwxr-xr-x. 6 root root 187 Jan 12 10:03 xtrabackups
[root@test-centos7-node1 scripts]# ll /backup/xtrabackups/
total 18456
-rw-r-----. 1 root root      431 Jan 12 10:03 backup-my.cnf
drwxr-x---. 2 root root      272 Jan 12 10:03 hellodb
-rw-r-----. 1 root root 18874368 Jan 12 10:03 ibdata1
drwxr-x---. 2 root root     4096 Jan 12 10:03 mysql
drwxr-x---. 2 root root     4096 Jan 12 10:03 performance_schema
drwxr-x---. 2 root root       20 Jan 12 10:03 test
-rw-r-----. 1 root root      113 Jan 12 10:03 xtrabackup_checkpoints
-rw-r-----. 1 root root      461 Jan 12 10:03 xtrabackup_info
-rw-r-----. 1 root root     2560 Jan 12 10:03 xtrabackup_logfile
[root@test-centos7-node1 scripts]#

  說明:以上指令碼實現了使用者選擇一款工具的名稱做備份,然後指定連線資料庫的使用者名稱,如果未指定使用者名稱,預設是當前Linux登入使用者的使用者名稱作為連線mariadb資料庫的使用者;指定連線資料庫的密碼,若未指定預設是空;指定資料庫地址,若未指定,預設是localhost 或者127.0.0.1 ,最後還要指定備份到那個的地方,如果使用mysqldump 那麼需要指定其存放檔案的全路徑(包括檔名稱,若只是給定了一個目錄,那麼mysqldump預設會在指定的目錄下建立一個all_backup.sql檔案),若未指定存放檔案的全路徑,則預設放在當前使用者家目錄下,並取名backup.all.sql;如果選擇的是xtrabackup備份工具備份資料,也需要指定其資料庫使用者名稱,密碼,資料庫地址,以及存放備份檔案的目錄,使用者名稱和密碼和資料庫地址 ,若都沒有指定,那麼使用者名稱就是用的當前Linux登入使用者,密碼為空,資料庫地址為localhost或127.0.0.1 同mysqldump 工具的預設值相同。最後就是存放備份資料庫檔案目錄,若未指定預設存放在當前使用者的家目錄的backup下存放。

2、配置Mysql主從同步

   1)準備兩臺mariadb資料庫 centos7上的mariadb為主庫,centos6上的mariadb為從庫   mariadb 編譯安裝請參考https://www.cnblogs.com/qiuhom-1874/p/12111497.html

   2)在主庫上開啟log-bin日誌,並配置主庫和從庫的server-id(組從庫server-id不能相同),如果從庫需要級聯其他從庫需開啟log-bin

  主庫配置檔案

[root@test-centos7-node1 ~]# grep -Eiv ^"#|^$" /etc/my.cnf    
[client]
port            = 3306
socket          = /data/mysql/mysql.sock
[mysqld]
port            = 3306
socket          = /data/mysql/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8
log-bin=mysql-bin
server-id       = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[root@test-centos7-node1 ~]# 

  說明:主庫配置檔案中只需要開啟log-bin和server-id 即可

  從庫配置檔案

[root@test-centos6-node1 ~]# grep -Eiv ^"#|^$" /etc/my.cnf
[client]
port            = 3306
socket          = /data/mysql/mysql.sock
[mysqld]
port            = 3306
socket          = /data/mysql/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8
server-id       = 2
read_only       =ON
relay_log       =relay-log
relay_log_index =relay-log.index
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[root@test-centos6-node1 ~]# 

  說明:從庫需要開啟中繼日誌,並把從庫設定為只讀(普通使用者只能讀,不能修改),server-id 不同於主庫即可。更改了主從庫的配置檔案後需要重啟服務才能生效。

  3)重啟主從庫資料庫,使其配置檔案生效

  主庫

[root@test-centos7-node1 ~]# /etc/init.d/mysqld restart
Restarting mysqld (via systemctl):                         [  OK  ]
[root@test-centos7-node1 ~]# 

  從庫

[root@test-centos6-node1 ~]# /etc/init.d/mysqld restart
Shutting down MariaDB.. SUCCESS! 
Starting MariaDB.200113 09:00:23 mysqld_safe Logging to '/data/mysql/test-centos6-node1.err'.
200113 09:00:23 mysqld_safe Starting mysqld daemon with databases from /data/mysql
 SUCCESS! 
[root@test-centos6-node1 ~]# 

  說明:本次實驗我是原始碼編譯安裝的mariadb,所以啟動都是直接用指令碼啟動。

  4)在主庫上建立用於同步的賬號

MariaDB [(none)]> select user,password,host from mysql.user;
+------+----------+--------------------+
| user | password | host               |
+------+----------+--------------------+
| root |          | localhost          |
| root |          | test-centos7-node1 |
| root |          | 127.0.0.1          |
| root |          | ::1                |
+------+----------+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'rep_user'@'192.168.0.%' identified by 'admin';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,password,host from mysql.user;                                       
+----------+-------------------------------------------+--------------------+
| user     | password                                  | host               |
+----------+-------------------------------------------+--------------------+
| root     |                                           | localhost          |
| root     |                                           | test-centos7-node1 |
| root     |                                           | 127.0.0.1          |
| root     |                                           | ::1                |
| rep_user | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | 192.168.0.%        |
+----------+-------------------------------------------+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> 

  說明:授權只需要給replication slave 許可權即可,有關mysql建立使用者授權可參考https://www.cnblogs.com/qiuhom-1874/p/9741166.html

  5)在從庫上測試建立的賬號是否能夠登入到主庫

[root@test-centos6-node1 ~]# mysql -urep_user -padmin -h192.168.0.10
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.2.19-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

  說明:是能夠連線上主庫的,說明賬號沒有問題。如果建立的賬號無法連線主庫,需要檢查主庫是否開啟了防火牆,檢查賬號是否正確,最後還要檢查主庫的監聽埠等。

  5)在主庫上檢視二進位制檔名和位置點,並記錄

MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       328 |
+------------------+-----------+
1 row in set (0.00 sec)

MariaDB [(none)]> 

  6)在從庫上配置連線主庫用於複製到賬號資訊

MariaDB [(none)]> show slave status\G
Empty set (0.00 sec)

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.0.10',
    ->   MASTER_USER='rep_user',
    ->   MASTER_PASSWORD='admin',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mysql-bin.000001',
    ->   MASTER_LOG_POS=328;
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> show slave status\G                  
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.0.10
                  Master_User: rep_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 328
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 328
              Relay_Log_Space: 256
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
1 row in set (0.00 sec)

MariaDB [(none)]> 

  說明:change master to 這個命令太長了,可用help change master to 檢視其幫助。我們需要配置好主庫地址,用於複製到賬號,密碼,以及主庫的埠,二進位制檔名,二進位制日誌位置點資訊即可,配置好後就可以用show slave status\G 檢視得到剛才我們配置的資訊

  7)從庫開啟同步

MariaDB [(none)]> start slave ;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.10
                  Master_User: rep_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 328
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 555
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 328
              Relay_Log_Space: 858
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)

MariaDB [(none)]> 

  說明:可看到IO執行緒和sql執行緒都已經是yes的了。到此mariadb的主從複製就做好了,接下來測試

測試:在主庫上匯入資料,看看從庫是否能夠及時的同步過來

  1)主庫匯入資料並檢視匯入到資料

[root@test-centos7-node1 ~]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%       7 KB    7 KB/s 00:00:01       0 Errors

[root@test-centos7-node1 ~]# mysql < hellodb_innodb.sql 
[root@test-centos7-node1 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.2.19-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> use hellodb
Database changed
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.00 sec)

MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)

MariaDB [hellodb]> 

  說明:可看到主庫已經有資料生成

  2)從庫檢視資料是否同主庫一致

[root@test-centos6-node1 ~]# mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[root@test-centos6-node1 ~]# mysql -e "use hellodb;show tables;"     
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
[root@test-centos6-node1 ~]# mysql -e "use hellodb;select * from students;"
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
[root@test-centos6-node1 ~]# 

  說明:可看到從庫把主庫裡新加的庫和表都複製過來了

有關mysql主從複製詳細說明請參考https://www.cnblogs.com/qiuhom-1874/p/9762855.html

3、使用MHA實現Mysql高可用。

  1)環境說明 3臺centos7為mariadb資料庫主從複製環境,centos6為mha管理節點,其中node1為主從複製主節點,2、3為從節點

  2)前期準備工作,關閉所有伺服器上的selinu和防火牆

[root@test-centos7-node1 ~]# systemctl stop firewalld
[root@test-centos7-node1 ~]# systemctl is-enabled firewalld
enabled
[root@test-centos7-node1 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@test-centos7-node1 ~]# systemctl is-enabled firewalld
disabled
[root@test-centos7-node1 ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@test-centos7-node1 ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX=disabled
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@test-centos7-node1 ~]# setenforce 0
[root@test-centos7-node1 ~]# getenforce
Permissive
[root@test-centos7-node1 ~]# 

  說明:在主從複製所有節點以及管理節點都關閉防火牆和selinux

[root@test-centos6-node1 ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@test-centos6-node1 ~]# chkconfig iptables off
[root@test-centos6-node1 ~]# chkconfig --list|grep iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@test-centos6-node1 ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config 
[root@test-centos6-node1 ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX=disabled
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@test-centos6-node1 ~]# setenforce 0
[root@test-centos6-node1 ~]# getenforce 
Permissive
[root@test-centos6-node1 ~]# 

  說明:centos6是管理節點,也需要關閉防火牆和selinux,這樣做就是排除後續做實驗,防火牆和selinux帶來的不必要的錯誤。

  3)搭建mariadb的主從複製

  主節點配置檔案

[root@test-centos7-node1 my.cnf.d]# cat /etc/my.cnf.d/master.cnf 
[mysqld]
log-bin
server_id=1
skip_name_resolve=1
[root@test-centos7-node1 my.cnf.d]# 

  說明:可以在server.cnf裡新增以上配置,也可以在my.cnf裡面加,當然也可以單獨建立獨立的配置檔案,這樣方便管理

  從節點配置檔案

root@test-centos7-node2 ~]# cat /etc/my.cnf.d/slave.cnf 
[mysqld]
server_id=2
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1
[root@test-centos7-node2 ~]# 

  說明:從節點需要加上relay_log_purge=0表示不清除中繼日誌。注意這裡需要說明一點的是,在備用的主節點(將來可能成為主的伺服器)上需要開啟log-bin,server_id 的值不同於其他主機即可

[root@test-centos7-node3 ~]# cat /etc/my.cnf.d/slave.cnf 
[mysqld]
server_id=3
read_only
relay_log_purge=0
skip_name_resolve=1
[root@test-centos7-node3 ~]# 

  4)重新啟動所有節點的資料庫服務,在主庫上檢視二進位制日誌檔名和日誌位置點

[root@test-centos7-node1 ~]# systemctl restart mariadb
[root@test-centos7-node1 ~]#
[root@test-centos7-node1 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)

MariaDB [(none)]> 

  說明:之所以檢視主庫二進位制日誌名稱和位置點數方便待會從庫裡配置

  5)主庫建立用於從庫連線主庫複製的賬號

MariaDB [(none)]> grant replication slave on *.* to repuser@'192.168.0.%' identified by 'admin';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host,password from mysql.user;
+---------+--------------------+-------------------------------------------+
| user    | host               | password                                  |
+---------+--------------------+-------------------------------------------+
| root    | localhost          |                                           |
| root    | test-centos7-node1 |                                           |
| root    | 127.0.0.1          |                                           |
| root    | ::1                |                                           |
|         | localhost          |                                           |
|         | test-centos7-node1 |                                           |
| repuser | 192.168.0.%        | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+---------+--------------------+-------------------------------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> 

  6)在從庫上測試主庫剛才建立的賬號是否可登入主庫

[root@test-centos7-node2 ~]# mysql -urepuser -padmin -h192.168.0.10
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show grants for repuser@'192.168.0.%';
+------------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected].%                                                                                               |
+------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'192.168.0.%' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441' |
+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

MariaDB [(none)]> 

  7)在從庫上配置連線主庫進行復制的賬號和二進位制日誌名稱及位置點資訊

[root@test-centos7-node2 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show slave status\G
Empty set (0.01 sec)

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.0.10',
    ->   MASTER_USER='repuser',
    ->   MASTER_PASSWORD='admin',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mariadb-bin.000001',
    ->   MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.08 sec)

MariaDB [(none)]> show slave status\G                    
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.0.10
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 245
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
1 row in set (0.01 sec)

MariaDB [(none)]> 

  說明:兩從庫都執行上面相同的change master to 命令即可

  8)從庫開啟複製

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.10
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 397
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 683
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 397
              Relay_Log_Space: 979
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

MariaDB [(none)]> 

  說明:到此主從複製就大家完成,接下來在主庫上建立用於管理端管理資料庫的賬號

  9)在主庫上建立用於管理端管理資料庫的管理帳號

MariaDB [(none)]> grant all on *.* to repmanage@'192.168.0.%' identified by 'admin';                     
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host,password from mysql.user;                        
+-----------+--------------------+-------------------------------------------+
| user      | host               | password                                  |
+-----------+--------------------+-------------------------------------------+
| root      | localhost          |                                           |
| root      | test-centos7-node1 |                                           |
| root      | 127.0.0.1          |                                           |
| root      | ::1                |                                           |
|           | localhost          |                                           |
|           | test-centos7-node1 |                                           |
| repuser   | 192.168.0.%        | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| repmanage | 192.168.0.%        | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+-----------+--------------------+-------------------------------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> 

  說明:此刻就可以去從節點看剛才在主庫建立的賬號是否同步到從庫裡去了,如果同步了,說明mariadb的主從複製是沒有問題的。

  10)在所有節點上做ssh key驗證包括管理節點上,實現雙向key驗證

[root@test-centos6-node1 ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
99:5f:36:b0:d1:cb:d2:85:f3:fa:65:ac:68:86:0e:e3 root@test-centos6-node1
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|           . .   |
|          o + .  |
|         o * =   |
|        S o B .  |
|         . + o . |
|        o ...   +|
|       . o. oo + |
|        E..o. o  |
+-----------------+
[root@test-centos6-node1 ~]# ssh-copy-id 192.168.0.11
The authenticity of host '192.168.0.11 (192.168.0.11)' can't be established.
RSA key fingerprint is f7:d4:c0:12:41:4a:46:4e:8b:d6:eb:80:06:ca:5e:fe.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.11' (RSA) to the list of known hosts.
[email protected]'s password: 
Now try logging into the machine, with "ssh '192.168.0.11'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@test-centos6-node1 ~]# scp -rp /root/.ssh 192.168.0.10:/root/
The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.
RSA key fingerprint is 7e:4a:a2:53:1b:fa:7b:52:c3:b6:9d:f7:7a:8d:4d:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.10' (RSA) to the list of known hosts.
[email protected]'s password: 
authorized_keys                                                                                                                          100%  405     0.4KB/s   00:00    
id_rsa                                                                                                                                   100% 1675     1.6KB/s   00:00    
id_rsa.pub                                                                                                                               100%  405     0.4KB/s   00:00    
known_hosts                                                                                                                              100%  788     0.8KB/s   00:00    
[root@test-centos6-node1 ~]# scp -rp /root/.ssh 192.168.0.20:/root/
The authenticity of host '192.168.0.20 (192.168.0.20)' can't be established.
RSA key fingerprint is 7e:4a:a2:53:1b:fa:7b:52:c3:b6:9d:f7:7a:8d:4d:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.20' (RSA) to the list of known hosts.
[email protected]'s password: 
authorized_keys                                                                                                                          100%  405     0.4KB/s   00:00    
id_rsa                                                                                                                                   100% 1675     1.6KB/s   00:00    
id_rsa.pub                                                                                                                               100%  405     0.4KB/s   00:00    
known_hosts                                                                                                                              100% 1182     1.2KB/s   00:00    
[root@test-centos6-node1 ~]# scp -rp /root/.ssh 192.168.0.30:/root/
The authenticity of host '192.168.0.30 (192.168.0.30)' can't be established.
RSA key fingerprint is 7e:4a:a2:53:1b:fa:7b:52:c3:b6:9d:f7:7a:8d:4d:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.30' (RSA) to the list of known hosts.
[email protected]'s password: 
authorized_keys                                                                                                                          100%  405     0.4KB/s   00:00    
id_rsa                                                                                                                                   100% 1675     1.6KB/s   00:00    
id_rsa.pub                                                                                                                               100%  405     0.4KB/s   00:00    
known_hosts                                                                                                                              100% 1576     1.5KB/s   00:00    
[root@test-centos6-node1 ~]# 

  說明:這樣在管理端做好了ssh key驗證後,管理端可以任意登入被管理端,同時被管理端也可以連線管理端。到此所有環境的準備都已經準備好了,接下來裝包

  11)在管理端安裝兩個包mha4mysql-manager和mha4mysql-node

[root@test-centos6-node1 ~]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%      85 KB   85 KB/s 00:00:01       0 Errors-0.el6.noarch.rpm...

[root@test-centos6-node1 ~]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%      35 KB   35 KB/s 00:00:01       0 Errorsel6.noarch.rpm...

[root@test-centos6-node1 ~]# ls
mha4mysql-manager-0.56-0.el6.noarch.rpm  mha4mysql-node-0.56-0.el6.noarch.rpm
[root@test-centos6-node1 ~]# yum install mha4mysql-*
Loaded plugins: fastestmirror
Setting up Install Process
Examining mha4mysql-manager-0.56-0.el6.noarch.rpm: mha4mysql-manager-0.56-0.el6.noarch
Marking mha4mysql-manager-0.56-0.el6.noarch.rpm to be installed
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-manager.noarch 0:0.56-0.el6 will be installed
--> Processing Dependency: perl(Config::Tiny) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Config::Tiny) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(DBI) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Log::Dispatch) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Log::Dispatch) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Log::Dispatch::File) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Log::Dispatch::Screen) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Parallel::ForkManager) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Parallel::ForkManager) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Time::HiRes) for package: mha4mysql-manager-0.56-0.el6.noarch
---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed
--> Processing Dependency: perl(DBD::mysql) for package: mha4mysql-node-0.56-0.el6.noarch
--> Running transaction check
---> Package perl-Config-Tiny.noarch 0:2.12-7.1.el6 will be installed
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
---> Package perl-Log-Dispatch.noarch 0:2.27-1.el6 will be installed
--> Processing Dependency: perl(MIME::Lite) for package: perl-Log-Dispatch-2.27-1.el6.noarch
--> Processing Dependency: perl(Mail::Send) for package: perl-Log-Dispatch-2.27-1.el6.noarch
--> Processing Dependency: perl(Mail::Sender) for package: perl-Log-Dispatch-2.27-1.el6.noarch
--> Processing Dependency: perl(Mail::Sendmail) for package: perl-Log-Dispatch-2.27-1.el6.noarch
--> Processing Dependency: perl(Params::Validate) for package: perl-Log-Dispatch-2.27-1.el6.noarch
---> Package perl-Parallel-ForkManager.noarch 0:1.20-1.el6 will be installed
---> Package perl-Time-HiRes.x86_64 4:1.9721-144.el6 will be installed
--> Running transaction check
---> Package perl-MIME-Lite.noarch 0:3.027-2.el6 will be installed
--> Processing Dependency: perl(MIME::Types) >= 1.28 for package: perl-MIME-Lite-3.027-2.el6.noarch
--> Processing Dependency: perl(Email::Date::Format) for package: perl-MIME-Lite-3.027-2.el6.noarch
---> Package perl-Mail-Sender.noarch 0:0.8.16-3.el6 will be installed
---> Package perl-Mail-Sendmail.noarch 0:0.79-12.el6 will be installed
---> Package perl-MailTools.noarch 0:2.04-4.el6 will be installed
--> Processing Dependency: perl(Date::Parse) for package: perl-MailTools-2.04-4.el6.noarch
--> Processing Dependency: perl(Date::Format) for package: perl-MailTools-2.04-4.el6.noarch
---> Package perl-Params-Validate.x86_64 0:0.92-3.el6 will be installed
--> Running transaction check
---> Package perl-Email-Date-Format.noarch 0:1.002-5.el6 will be installed
---> Package perl-MIME-Types.noarch 0:1.28-2.el6 will be installed
---> Package perl-TimeDate.noarch 1:1.16-13.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================
 Package                   Arch   Version             Repository                            Size
=================================================================================================
Installing:
 mha4mysql-manager         noarch 0.56-0.el6          /mha4mysql-manager-0.56-0.el6.noarch 325 k
 mha4mysql-node            noarch 0.56-0.el6          /mha4mysql-node-0.56-0.el6.noarch    102 k
Installing for dependencies:
 perl-Config-Tiny          noarch 2.12-7.1.el6        base                                  23 k
 perl-DBD-MySQL            x86_64 4.013-3.el6         base                                 134 k
 perl-DBI                  x86_64 1.609-4.el6         base                                 705 k
 perl-Email-Date-Format    noarch 1.002-5.el6         base                                  16 k
 perl-Log-Dispatch         noarch 2.27-1.el6          epel                                  71 k
 perl-MIME-Lite            noarch 3.027-2.el6         base                                  82 k
 perl-MIME-Types           noarch 1.28-2.el6          base                                  32 k
 perl-Mail-Sender          noarch 0.8.16-3.el6        epel                                  54 k
 perl-Mail-Sendmail        noarch 0.79-12.el6         epel                                  28 k
 perl-MailTools            noarch 2.04-4.el6          base                                 101 k
 perl-Parallel-ForkManager noarch 1.20-1.el6          epel                                  27 k
 perl-Params-Validate      x86_64 0.92-3.el6          base                                  75 k
 perl-Time-HiRes           x86_64 4:1.9721-144.el6    base                                  49 k
 perl-TimeDate             noarch 1:1.16-13.el6       base                                  37 k

Transaction Summary
=================================================================================================
Install      16 Package(s)

Total size: 1.8 M
Total download size: 1.4 M
Installed size: 3.5 M
Is this ok [y/N]: y
Downloading Packages:
(1/14): perl-Config-Tiny-2.12-7.1.el6.noarch.rpm                          |  23 kB     00:00     
(2/14): perl-DBD-MySQL-4.013-3.el6.x86_64.rpm                             | 134 kB     00:00     
(3/14): perl-DBI-1.609-4.el6.x86_64.rpm                                   | 705 kB     00:00     
(4/14): perl-Email-Date-Format-1.002-5.el6.noarch.rpm                     |  16 kB     00:00     
(5/14): perl-Log-Dispatch-2.27-1.el6.noarch.rpm                           |  71 kB     00:00     
(6/14): perl-MIME-Lite-3.027-2.el6.noarch.rpm                             |  82 kB     00:00     
(7/14): perl-MIME-Types-1.28-2.el6.noarch.rpm                             |  32 kB     00:00     
(8/14): perl-Mail-Sender-0.8.16-3.el6.noarch.rpm                          |  54 kB     00:00     
(9/14): perl-Mail-Sendmail-0.79-12.el6.noarch.rpm                         |  28 kB     00:00     
(10/14): perl-MailTools-2.04-4.el6.noarch.rpm                             | 101 kB     00:00     
(11/14): perl-Parallel-ForkManager-1.20-1.el6.noarch.rpm                  |  27 kB     00:00     
(12/14): perl-Params-Validate-0.92-3.el6.x86_64.rpm                       |  75 kB     00:00     
(13/14): perl-Time-HiRes-1.9721-144.el6.x86_64.rpm                        |  49 kB     00:00     
(14/14): perl-TimeDate-1.16-13.el6.noarch.rpm                             |  37 kB     00:00     
-------------------------------------------------------------------------------------------------
Total                                                            669 kB/s | 1.4 MB     00:02     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : perl-DBI-1.609-4.el6.x86_64                                                  1/16 
  Installing : perl-DBD-MySQL-4.013-3.el6.x86_64                                            2/16 
  Installing : mha4mysql-node-0.56-0.el6.noarch                                             3/16 
  Installing : perl-MIME-Types-1.28-2.el6.noarch                                            4/16 
  Installing : perl-Config-Tiny-2.12-7.1.el6.noarch                                         5/16 
  Installing : perl-Parallel-ForkManager-1.20-1.el6.noarch                                  6/16 
  Installing : perl-Params-Validate-0.92-3.el6.x86_64                                       7/16 
  Installing : 4:perl-Time-HiRes-1.9721-144.el6.x86_64                                      8/16 
  Installing : perl-Mail-Sender-0.8.16-3.el6.noarch                                         9/16 
  Installing : 1:perl-TimeDate-1.16-13.el6.noarch                                          10/16 
  Installing : perl-MailTools-2.04-4.el6.noarch                                            11/16 
  Installing : perl-Mail-Sendmail-0.79-12.el6.noarch                                       12/16 
  Installing : perl-Email-Date-Format-1.002-5.el6.noarch                                   13/16 
  Installing : perl-MIME-Lite-3.027-2.el6.noarch                                           14/16 
  Installing : perl-Log-Dispatch-2.27-1.el6.noarch                                         15/16 
  Installing : mha4mysql-manager-0.56-0.el6.noarch                                         16/16 
  Verifying  : mha4mysql-manager-0.56-0.el6.noarch                                          1/16 
  Verifying  : perl-Email-Date-Format-1.002-5.el6.noarch                                    2/16 
  Verifying  : perl-Mail-Sendmail-0.79-12.el6.noarch                                        3/16 
  Verifying  : mha4mysql-node-0.56-0.el6.noarch                                             4/16 
  Verifying  : perl-DBD-MySQL-4.013-3.el6.x86_64                                            5/16 
  Verifying  : 1:perl-TimeDate-1.16-13.el6.noarch                                           6/16 
  Verifying  : perl-MIME-Lite-3.027-2.el6.noarch                                            7/16 
  Verifying  : perl-Mail-Sender-0.8.16-3.el6.noarch                                         8/16 
  Verifying  : perl-DBI-1.609-4.el6.x86_64                                                  9/16 
  Verifying  : 4:perl-Time-HiRes-1.9721-144.el6.x86_64                                     10/16 
  Verifying  : perl-Params-Validate-0.92-3.el6.x86_64                                      11/16 
  Verifying  : perl-MailTools-2.04-4.el6.noarch                                            12/16 
  Verifying  : perl-Parallel-ForkManager-1.20-1.el6.noarch                                 13/16 
  Verifying  : perl-Config-Tiny-2.12-7.1.el6.noarch                                        14/16 
  Verifying  : perl-Log-Dispatch-2.27-1.el6.noarch                                         15/16 
  Verifying  : perl-MIME-Types-1.28-2.el6.noarch                                           16/16 

Installed:
  mha4mysql-manager.noarch 0:0.56-0.el6            mha4mysql-node.noarch 0:0.56-0.el6           

Dependency Installed:
  perl-Config-Tiny.noarch 0:2.12-7.1.el6           perl-DBD-MySQL.x86_64 0:4.013-3.el6           
  perl-DBI.x86_64 0:1.609-4.el6                    perl-Email-Date-Format.noarch 0:1.002-5.el6   
  perl-Log-Dispatch.noarch 0:2.27-1.el6            perl-MIME-Lite.noarch 0:3.027-2.el6           
  perl-MIME-Types.noarch 0:1.28-2.el6              perl-Mail-Sender.noarch 0:0.8.16-3.el6        
  perl-Mail-Sendmail.noarch 0:0.79-12.el6          perl-MailTools.noarch 0:2.04-4.el6            
  perl-Parallel-ForkManager.noarch 0:1.20-1.el6    perl-Params-Validate.x86_64 0:0.92-3.el6      
  perl-Time-HiRes.x86_64 4:1.9721-144.el6          perl-TimeDate.noarch 1:1.16-13.el6            

Complete!
[root@test-centos6-node1 ~]

  說明:安裝這兩個包需要開啟epel源,因為它們的依賴包有些來自epel源裡

  12)在各個被管理端安裝mha4mysql-node包

[root@test-centos7-node1 ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm
[root@test-centos7-node1 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm 
Loaded plugins: fastestmirror
Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================
 Package                     Arch                Version                    Repository                                      Size
=================================================================================================================================
Installing:
 mha4mysql-node              noarch              0.56-0.el6                 /mha4mysql-node-0.56-0.el6.noarch              102 k

Transaction Summary
=================================================================================================================================
Install  1 Package

Total size: 102 k
Installed size: 102 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mha4mysql-node-0.56-0.el6.noarch                                                                              1/1 
  Verifying  : mha4mysql-node-0.56-0.el6.noarch                                                                              1/1 

Installed:
  mha4mysql-node.noarch 0:0.56-0.el6                                                                                             

Complete!
[root@test-centos7-node1 ~]# 

  說明:到此mha的軟體都已部署完畢,接下來是在管理端建立配置檔案

  13)在管理節點新建配置檔案

[root@test-centos6-node1 ~]# mkdir /etc/mastermh/   
[root@test-centos6-node1 ~]# cd /etc/mastermh/
[root@test-centos6-node1 mastermh]# cat >> mariadb.cnf << EOF
> [server default]
> user=repmanage
> password=admin
> manager_workdir=/data/mastermha/mariadb/
> manager_log=/data/mastermha/mariadb/manager.log
> remote_workdir=/data/mastermha/mariadb/
> ssh_user=root
> repl_user=repuser
> repl_password=admin
> ping_interval=1
> 
> 
> [server1]
> hostname=192.168.0.10
> candidate_master=1
> [server2]
> hostname=192.168.0.20
> candidate_master=1
> [server3]
> hostname=192.168.0.30
> EOF
[root@test-centos6-node1 mastermh]# cat mariadb.cnf 
[server default]
user=repmanage
password=admin
manager_workdir=/data/mastermha/mariadb/
manager_log=/data/mastermha/mariadb/manager.log
remote_workdir=/data/mastermha/mariadb/
ssh_user=root
repl_user=repuser
repl_password=admin
ping_interval=1


[server1]
hostname=192.168.0.10
candidate_master=1
[server2]
hostname=192.168.0.20
candidate_master=1
[server3]
hostname=192.168.0.30
[root@test-centos6-node1 mastermh]# 

  說明:此檔案的檔案可以說任意名稱,只要自己知道就行,也沒有特定規定放在哪個位置,看自己的喜好即可。因為待會啟動mha 我們是要指定配置檔案的路徑,配置檔案中主要配置了用於管理mariadb節點的賬號密碼以及ssh管理的使用者以及主從複製到賬號和密碼資訊還有就是各個節點的地址,candidate_master=1表示將來可能選舉成為主節點。ping_interval=1表示檢測主庫的時間間隔,心跳值;配置檔案中manager的工作目錄和日誌目錄我們不需要提前建立好,它這個目錄只要我們指定了會自動生成的。

  14)mha驗證ssh基於KEY驗證是否正常

[root@test-centos6-node1 ~]# masterha_check_ssh --conf=/etc/mastermh/mariadb.cnf 
Tue Jan 14 08:05:04 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jan 14 08:05:04 2020 - [info] Reading application default configuration from /etc/mastermh/mariadb.cnf..
Tue Jan 14 08:05:04 2020 - [info] Reading server configuration from /etc/mastermh/mariadb.cnf..
Tue Jan 14 08:05:04 2020 - [info] Starting SSH connection tests..
Tue Jan 14 08:05:05 2020 - [debug] 
Tue Jan 14 08:05:04 2020 - [debug]  Connecting via SSH from [email protected](192.168.0.10:22) to [email protected](192.168.0.20:22)..
Warning: Permanently added '192.168.0.20' (ECDSA) to the list of known hosts.
Tue Jan 14 08:05:05 2020 - [debug]   ok.
Tue Jan 14 08:05:05 2020 - [debug]  Connecting via SSH from [email protected](192.168.0.10:22) to [email protected](192.168.0.30:22)..
Warning: Permanently added '192.168.0.30'