第十五周

1、编写脚本,持让用户自主选择,使用mysqldump还是xtraback全量备份

备注:前提是安装好mariadb-server以及xtraback(epel源)

#!/bin/bash

PATH=/data
FILE_NAME=all.sql
read -p "Do you want to back up [y/n]? " answer
case $answer in
y)
        echo "Please select the way to backup:"
        echo "1)mysqldump"
        echo "2)xtrabackup"
        read -p "input the number " method
        if [[ $method = 1 ]];then
                /usr/bin/mysqldump -A --master-data=2 > $PATH/$FILE_NAME
                echo "Back-up success!"                                                                                                                                                    
        elif [[ $method = 2 ]];then
                /usr/bin/xtrabackup --backup --target-dir=$PATH/xtra
        else
                echo "please select correct number !"
                exit 0
        fi
        ;;
n)
        exit 10
        ;;
*)
        echo "please input correct answer !"
        ;;
esac
exit

(1)选择mysqldump备份

[root@centos01 ~]#bash mysql_back_up.sh 
Do you want to back up [y/n]? y
Please select the way to backup:
1)mysqldump
2)xtrabackup
input the number 1
Back-up success!
[root@centos01 ~]#ll /data/all.sql 
-rw-r--r-- 1 root root 515055 Jun 16 14:50 /data/all.sql

(2)选择xtraback备份

[root@centos01 ~]#bash mysql_back_up.sh 
Do you want to back up [y/n]? y
Please select the way to backup:
1)mysqldump
2)xtrabackup
input the number 2
sh: perl: command not found
200616 14:51:40 Connecting to MySQL server host: localhost, user: (null), password: not set, port: 0, socket: /data/mysql/mysql.sock
Using server version 5.5.65-MariaDB
/usr/bin/xtrabackup version 2.3.6 based on MySQL server 5.6.24 Linux (x86_64) (revision id: )
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /data/mysql
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup:   innodb_data_home_dir = ./
xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup:   innodb_log_group_home_dir = ./
xtrabackup:   innodb_log_files_in_group = 2
xtrabackup:   innodb_log_file_size = 5242880
200616 14:51:40 >> log scanned up to (1597945)
xtrabackup: Generating a list of tablespaces
200616 14:51:40 [01] Copying ./ibdata1 to /data/xtra/ibdata1
200616 14:51:41 [01]        ...done
200616 14:51:41 >> log scanned up to (1597945)
200616 14:51:41 Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
200616 14:51:41 Executing FLUSH TABLES WITH READ LOCK...
200616 14:51:41 Starting to backup non-InnoDB tables and files
200616 14:51:41 [01] Copying ./mysql/db.frm to /data/xtra/mysql/db.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/db.MYI to /data/xtra/mysql/db.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/db.MYD to /data/xtra/mysql/db.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/host.frm to /data/xtra/mysql/host.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/host.MYI to /data/xtra/mysql/host.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/host.MYD to /data/xtra/mysql/host.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/user.frm to /data/xtra/mysql/user.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/user.MYI to /data/xtra/mysql/user.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/user.MYD to /data/xtra/mysql/user.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/func.frm to /data/xtra/mysql/func.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/func.MYI to /data/xtra/mysql/func.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/func.MYD to /data/xtra/mysql/func.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/plugin.frm to /data/xtra/mysql/plugin.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/plugin.MYI to /data/xtra/mysql/plugin.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/plugin.MYD to /data/xtra/mysql/plugin.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/servers.frm to /data/xtra/mysql/servers.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/servers.MYI to /data/xtra/mysql/servers.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/servers.MYD to /data/xtra/mysql/servers.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/tables_priv.frm to /data/xtra/mysql/tables_priv.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/tables_priv.MYI to /data/xtra/mysql/tables_priv.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/tables_priv.MYD to /data/xtra/mysql/tables_priv.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/columns_priv.frm to /data/xtra/mysql/columns_priv.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/columns_priv.MYI to /data/xtra/mysql/columns_priv.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/columns_priv.MYD to /data/xtra/mysql/columns_priv.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_topic.frm to /data/xtra/mysql/help_topic.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_topic.MYI to /data/xtra/mysql/help_topic.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_topic.MYD to /data/xtra/mysql/help_topic.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_category.frm to /data/xtra/mysql/help_category.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_category.MYI to /data/xtra/mysql/help_category.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_category.MYD to /data/xtra/mysql/help_category.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_relation.frm to /data/xtra/mysql/help_relation.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_relation.MYI to /data/xtra/mysql/help_relation.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_relation.MYD to /data/xtra/mysql/help_relation.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_keyword.frm to /data/xtra/mysql/help_keyword.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_keyword.MYI to /data/xtra/mysql/help_keyword.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/help_keyword.MYD to /data/xtra/mysql/help_keyword.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_name.frm to /data/xtra/mysql/time_zone_name.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_name.MYI to /data/xtra/mysql/time_zone_name.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_name.MYD to /data/xtra/mysql/time_zone_name.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone.frm to /data/xtra/mysql/time_zone.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone.MYI to /data/xtra/mysql/time_zone.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone.MYD to /data/xtra/mysql/time_zone.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_transition.frm to /data/xtra/mysql/time_zone_transition.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_transition.MYI to /data/xtra/mysql/time_zone_transition.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_transition.MYD to /data/xtra/mysql/time_zone_transition.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_transition_type.frm to /data/xtra/mysql/time_zone_transition_type.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_transition_type.MYI to /data/xtra/mysql/time_zone_transition_type.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_transition_type.MYD to /data/xtra/mysql/time_zone_transition_type.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_leap_second.frm to /data/xtra/mysql/time_zone_leap_second.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_leap_second.MYI to /data/xtra/mysql/time_zone_leap_second.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/time_zone_leap_second.MYD to /data/xtra/mysql/time_zone_leap_second.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/proc.frm to /data/xtra/mysql/proc.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/proc.MYI to /data/xtra/mysql/proc.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/proc.MYD to /data/xtra/mysql/proc.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/procs_priv.frm to /data/xtra/mysql/procs_priv.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/procs_priv.MYI to /data/xtra/mysql/procs_priv.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/procs_priv.MYD to /data/xtra/mysql/procs_priv.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/general_log.frm to /data/xtra/mysql/general_log.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/general_log.CSM to /data/xtra/mysql/general_log.CSM
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/general_log.CSV to /data/xtra/mysql/general_log.CSV
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/slow_log.frm to /data/xtra/mysql/slow_log.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/slow_log.CSM to /data/xtra/mysql/slow_log.CSM
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/slow_log.CSV to /data/xtra/mysql/slow_log.CSV
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/event.frm to /data/xtra/mysql/event.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/event.MYI to /data/xtra/mysql/event.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/event.MYD to /data/xtra/mysql/event.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/ndb_binlog_index.frm to /data/xtra/mysql/ndb_binlog_index.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/ndb_binlog_index.MYI to /data/xtra/mysql/ndb_binlog_index.MYI
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/ndb_binlog_index.MYD to /data/xtra/mysql/ndb_binlog_index.MYD
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/proxies_priv.frm to /data/xtra/mysql/proxies_priv.frm
200616 14:51:41 [01]        ...done
200616 14:51:41 [01] Copying ./mysql/proxies_priv.MYI to /data/xtra/mysql/proxies_priv.MYI
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./mysql/proxies_priv.MYD to /data/xtra/mysql/proxies_priv.MYD
200616 14:51:42 [01]        ...done
200616 14:51:42 [00] Writing test/db.opt
200616 14:51:42 [00]        ...done
200616 14:51:42 [01] Copying ./performance_schema/db.opt to /data/xtra/performance_schema/db.opt
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/cond_instances.frm to /data/xtra/performance_schema/cond_instances.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/events_waits_current.frm to /data/xtra/performance_schema/events_waits_current.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/events_waits_history.frm to /data/xtra/performance_schema/events_waits_history.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/events_waits_history_long.frm to /data/xtra/performance_schema/events_waits_history_long.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/events_waits_summary_by_instance.frm to /data/xtra/performance_schema/events_waits_summary_by_instance.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/events_waits_summary_by_thread_by_event_name.frm to /data/xtra/performance_schema/events_waits_summary_by_thread_by_event_name.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/events_waits_summary_global_by_event_name.frm to /data/xtra/performance_schema/events_waits_summary_global_by_event_name.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/file_instances.frm to /data/xtra/performance_schema/file_instances.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/file_summary_by_event_name.frm to /data/xtra/performance_schema/file_summary_by_event_name.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/file_summary_by_instance.frm to /data/xtra/performance_schema/file_summary_by_instance.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/mutex_instances.frm to /data/xtra/performance_schema/mutex_instances.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/performance_timers.frm to /data/xtra/performance_schema/performance_timers.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/rwlock_instances.frm to /data/xtra/performance_schema/rwlock_instances.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/setup_consumers.frm to /data/xtra/performance_schema/setup_consumers.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/setup_instruments.frm to /data/xtra/performance_schema/setup_instruments.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/setup_timers.frm to /data/xtra/performance_schema/setup_timers.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 [01] Copying ./performance_schema/threads.frm to /data/xtra/performance_schema/threads.frm
200616 14:51:42 [01]        ...done
200616 14:51:42 Finished backing up non-InnoDB tables and files
200616 14:51:42 [00] Writing xtrabackup_binlog_info
200616 14:51:42 [00]        ...done
200616 14:51:42 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '1597945'
xtrabackup: Stopping log copying thread.
.200616 14:51:42 >> log scanned up to (1597945)

200616 14:51:42 Executing UNLOCK TABLES
200616 14:51:42 All tables unlocked
200616 14:51:42 Backup created in directory '/data/xtra'
MySQL binlog position: filename 'mysql_bin.000005', position '245'
200616 14:51:42 [00] Writing backup-my.cnf
200616 14:51:42 [00]        ...done
200616 14:51:42 [00] Writing xtrabackup_info
200616 14:51:42 [00]        ...done
xtrabackup: Transaction log of lsn (1597945) to (1597945) was copied.
200616 14:51:42 completed OK!

(3)其他选择

[root@centos01 ~]#bash mysql_back_up.sh 
Do you want to back up [y/n]? n

[root@centos01 ~]#bash mysql_back_up.sh 
Do you want to back up [y/n]? y
Please select the way to backup:
1)mysqldump
2)xtrabackup
input the number 3
please select correct number !

[root@centos01 ~]#bash mysql_back_up.sh 
Do you want to back up [y/n]? o
please input correct answer !

2、配置Mysql主从同步

(1)修改主服务器配置文件

[root@centos7 ~]#vim /etc/my.cnf
server-id=1                         #添加

(2)重启服务

[root@centos7 ~]#systemctl restart mariadb.service

(3)创建账号并查看二进制日志位置

MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.44.%' identified by '520109';
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |      8156 |
| mysql-bin.000002 |     30373 |
| mysql-bin.000003 |   1038814 |
| mysql-bin.000004 |      1042 |
| mysql-bin.000005 |       264 |
| mysql-bin.000006 |     30373 |
| mysql-bin.000007 |   1038814 |
| mysql-bin.000008 |       264 |
| mysql-bin.000009 |       400 |
+------------------+-----------+

(4)修改从服务器配置文件并重启

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2                    #添加
[root@localhost ~]# systemctl restart mariadb.service

(5)配置从服务器并查看

MariaDB [(none)]> CHANGE MASTER TO
->   MASTER_HOST='192.168.44.70',
->   MASTER_USER='repluser',
->   MASTER_PASSWORD='520109',
->   MASTER_PORT=3306,
->   MASTER_LOG_FILE='mysql-bin.000009',
->   MASTER_LOG_POS=400;
MariaDB [(none)]> show slave status\G
 Slave_IO_State: 
              Master_Host: 192.168.44.70
              Master_User: repluser
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: mysql-bin.000009
      Read_Master_Log_Pos: 400
           Relay_Log_File: mariadb-relay-bin.000001
            Relay_Log_Pos: 4
    Relay_Master_Log_File: mysql-bin.000009
         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: 400
          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

(6)启动从服务再查看

MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.44.70
              Master_User: repluser
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: mysql-bin.000009
      Read_Master_Log_Pos: 483
           Relay_Log_File: mariadb-relay-bin.000002
            Relay_Log_Pos: 612
    Relay_Master_Log_File: mysql-bin.000009
         Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
          Replicate_Do_DB: 
      Replicate_Ignore_DB: 
       Replicate_Do_Table: 
   Replicate_Ignore_Table: 
  Replicate_Wild_Do_Table: 

3、使用MHA实现Mysql高可用

提前将所需MHA包传送至各个主机,管理主机两个,其他均只需要node包

1、配置好主从服务器的配置文件

主服务器配置如下

[mysqld]
server-id=79
log-bin
skip_name_resolve

从服务器配置如下

[mysqld]
server-id=72
log-bin
read-only
skip_name_resolve
relay_log_purge=0 

2、启动主从服务器

[root@localhost ~]# systemctl start mariadb.service

3、主服务器添加远程连接用户和管理节点

MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.44.%' identified by '110110';
MariaDB [(none)]> grant all on *.* to mhauser@'192.168.44.%' identified by '110110';
MariaDB [(none)]> select host,user,password from mysql.user;
+-----------------------+----------+-------------------------------------------+
| host                  | user     | password                                  |
+-----------------------+----------+-------------------------------------------+
| localhost             | root     |                                           |
| localhost.localdomain | root     |                                           |
| 127.0.0.1             | root     |                                           |
| ::1                   | root     |                                           |
| localhost             |          |                                           |
| localhost.localdomain |          |                                           |
| 192.168.44.%          | repluser | *BEF29CC9D1F7509710900A5782022DDC6092632B |
| 192.168.44.%          | mhauser  | *BEF29CC9D1F7509710900A5782022DDC6092632B |
+-----------------------+----------+-------------------------------------------+
8 rows in set (0.00 sec)

4、配置从服务器(两台同样)

MariaDB [(none)]> CHANGE MASTER TO 
-> MASTER_HOST='192.168.44.79',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='110110',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mariadb-bin.000001',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.02 sec)
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.44.79
              Master_User: repluser
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: mariadb-bin.000001
      Read_Master_Log_Pos: 695
           Relay_Log_File: mariadb-relay-bin.000002
            Relay_Log_Pos: 981
    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: 695
          Relay_Log_Space: 1277
          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: 79
1 row in set (0.00 sec)

5、各主机安装MHA对应包

[root@centos7 ~]# yum install mha*.rpm                    #利用yum避免依赖性错误

6、配置所有主机间的key验证

[root@centos7 ~]# 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:
SHA256:b5ywn20liAWbWzgtCN6lZCHA2EH+EC97YRyoziRt3us [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| ==+o ..         |
|..++.oo o        |
| o+.== + *       |
|o.o*..+ * +      |
|=o..o   SB .     |
| o...   o=... .  |
|     .  . =  o   |
|    .    o o.    |
|   .E     o..    |
+----[SHA256]-----+
[root@centos7 ~]# ssh-copy-id 192.168.44.71
[root@centos7 ~]# cd .ssh
[root@centos7 .ssh]# cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQComu6VuYN0h+Ir0dQvzUnW3u04O3PwiBDnwDMG/6fHmgPQWSp6NanajURC5b3OQ+eQnoqvrlfMbKXlu4bdByylBQ4xvcrmMzpRfj62HXjdo0g1yacp4/R6A1OYekdKsxMeYfPh9kEnFiHljPR04rf66bfPJ9X/7SfwPiJoepQbpxGYLe9qXWPjDYPgaEsgYrqq1LmvgfAmhs07/WyOsDdr6E8PL0b16BXeRxfVTip417WUNt3mJ2Rv3n91gHrl5LDFElfzZGlWuJW4hayZppeoc/KcKzOdmdK/cxdYzC5VcZa3PjX3tM7dr+HP+Pq6vD6pyVoyD4bRQ75j0VshGfn1 [email protected]
[root@centos7 ~]# scp -r .ssh 192.168.44.79:/root/
[root@centos7 ~]# scp -r .ssh 192.168.44.72:/root/
[root@centos7 ~]# scp -r .ssh 192.168.44.73:/root/

7、配置管理节点

[root@centos7 ~]# vim /etc/mha/app1.cnf
[server default]
user=mhauser
password=110110
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=110110
ping_interval=1
[server1]
hostname=192.168.44.79
candidate_master=1
[server2]
hostname=192.168.44.72
[server3]
hostname=192.168.44.73
candidate_master=1

8、MHA验证

[root@centos7 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf                     #ssh连接检查
Mon Mar 16 22:42:13 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Mar 16 22:42:13 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Mar 16 22:42:13 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Mon Mar 16 22:42:13 2020 - [info] Starting SSH connection tests..
Mon Mar 16 22:42:14 2020 - [debug] 
Mon Mar 16 22:42:13 2020 - [debug]  Connecting via SSH from [email protected](192.168.44.79:22) to [email protected](192.168.44.72:22)..
Warning: Permanently added '192.168.44.72' (ECDSA) to the list of known hosts.
Mon Mar 16 22:42:13 2020 - [debug]   ok.
Mon Mar 16 22:42:13 2020 - [debug]  Connecting via SSH from [email protected](192.168.44.79:22) to [email protected](192.168.44.73:22)..
Warning: Permanently added '192.168.44.73' (ECDSA) to the list of known hosts.
Mon Mar 16 22:42:14 2020 - [debug]   ok.
Mon Mar 16 22:42:14 2020 - [debug] 
Mon Mar 16 22:42:13 2020 - [debug]  Connecting via SSH from [email protected](192.168.44.72:22) to [email protected](192.168.44.79:22)..
Mon Mar 16 22:42:14 2020 - [debug]   ok.
Mon Mar 16 22:42:14 2020 - [debug]  Connecting via SSH from [email protected](192.168.44.72:22) to [email protected](192.168.44.73:22)..
Warning: Permanently added '192.168.44.73' (ECDSA) to the list of known hosts.
Mon Mar 16 22:42:14 2020 - [debug]   ok.
Mon Mar 16 22:42:15 2020 - [debug] 
Mon Mar 16 22:42:14 2020 - [debug]  Connecting via SSH from [email protected](192.168.44.73:22) to [email protected](192.168.44.79:22)..
Mon Mar 16 22:42:14 2020 - [debug]   ok.
Mon Mar 16 22:42:14 2020 - [debug]  Connecting via SSH from [email protected](192.168.44.73:22) to [email protected](192.168.44.72:22)..
Mon Mar 16 22:42:15 2020 - [debug]   ok.
Mon Mar 16 22:42:15 2020 - [info] All SSH connection tests passed successfully.

[root@centos7 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf                       #复制检查
.......
Mon Mar 16 22:47:27 2020 - [info] Checking replication health on 192.168.44.72..
Mon Mar 16 22:47:27 2020 - [info]  ok.
Mon Mar 16 22:47:27 2020 - [info] Checking replication health on 192.168.44.73..
Mon Mar 16 22:47:27 2020 - [info]  ok.
Mon Mar 16 22:47:27 2020 - [warning] master_ip_failover_script is not defined.
Mon Mar 16 22:47:27 2020 - [warning] shutdown_script is not defined.
Mon Mar 16 22:47:27 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

9、MHA启动

[root@centos7 ~]# masterha_manager --conf=/etc/mha/app1.cnf
Mon Mar 16 22:51:28 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Mar 16 22:51:28 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Mar 16 22:51:28 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..

处于持续监控状态

10、模拟主服务器出故障

先跑一个生成较大数据库的脚本

MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table testlog (id int auto_increment primary key,name char(10),age int default 20);
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> 
MariaDB [test]> delimiter $$
MariaDB [test]> 
MariaDB [test]> create procedure  sp_testlog() 
    -> begin  
    -> declare i int;
    -> set i = 1; 
    -> while i <= 100000 
    -> do  insert into testlog(name,age) values (concat('wang',i),i); 
    -> set i = i +1; 
    -> end while; 
    -> end$$
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> 
MariaDB [test]> delimiter ;
MariaDB [test]> call sp_testlog;

未完成中间直接“断电”模拟宕机,此时可以发现管理主机监控发生改变并退出

Mon Mar 16 22:56:03 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Mar 16 22:56:03 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Mar 16 22:56:03 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..

查看日志

[root@centos7 ~]# tail -f /data/mastermha/app1/manager.log 

Started automated(non-interactive) failover.
The latest slave 192.168.44.72(192.168.44.72:3306) has all relay logs for recovery.
Selected 192.168.44.73(192.168.44.73:3306) as a new master.
192.168.44.73(192.168.44.73:3306): OK: Applying all logs succeeded.
192.168.44.72(192.168.44.72:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.44.72(192.168.44.72:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.44.73(192.168.44.73:3306)
192.168.44.73(192.168.44.73:3306): Resetting slave info succeeded.
Master failover to 192.168.44.73(192.168.44.73:3306) completed successfully.

提示192.168.44.73成为新的主服务器,验证查看

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

在73上创建新的库,72上复制也成功

提示:在从服务器提升为主服务器后,必须人为去修改配置文件,将"read-only"删掉或注释掉
为防止监控前台运行被人为关掉,可改为后台执行

你可能感兴趣的:(第十五周)