2020-08-08第十五周

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

[root@localhost data]# cat mysqlback.sh 
#!/bin/bash

path=/data
file_name=all.`date +%F`.sql
read -p "Do you use mysqldump or xtraback full backup?(mysqldump/xtraback) " nin
  if [[ $nin = "mysqldump" ]];then
    mysqldump -A --master-data=2 > $path/$file_name -uroot -p123456
   echo "Back-up succcess!"
  elif [[ $nin = "xtraback" ]];then
    xtrabackup --backup --target-dir=$path/xtra.`date +%F` >/dev/null 2>&1 -uroot -p123456  不输出复制内容 如需要输出请将>/dev/null 2>&1删除
    echo "Back-up succcess!"
  else
    echo "Please enter mysqldump or xtraback!"
    exit 0
  fi

测试

root@localhost data]# bash mysqlback.sh 
Do you use mysqldump or xtraback full backup?(mysqldump/xtraback) xtraback
Back-up succcess!
[root@localhost data]# bash mysqlback.sh 
Do you use mysqldump or xtraback full backup?(mysqldump/xtraback) mysqldump
Back-up succcess!
[root@localhost data]# bash mysqlback.sh 
Do you use mysqldump or xtraback full backup?(mysqldump/xtraback) 111
Please enter mysqldump or xtraback!

2、配置Mysql主从同步

主机配置
启用二进制日志 为当前节点设置一个全局惟一的ID号
[mysqld]
server_id=1
log-bin=/data/logbin/mysql-bin
log-basename=master 可选项,设置datadir中日志名称,确保不依赖主机名
[root@localhost backup]# mysql -uroot -p   创建有复制权限的用户账号并查看二进制日志位置
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'172.16.100.%' IDENTIFIED BY '123456';
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       496 |
| mysql-bin.000002 |      1973 |
| mysql-bin.000003 |       404 |
+------------------+-----------+
3 rows in set (0.00 sec)

[root@localhost backup]# systemctl restart mariadb 重启服务使配置生效
从机配置
启动中继日志

[mysqld]
server_id=2 为当前节点设置一个全局惟的ID号
read_only=ON 设置数据库只读
relay_log=relay-log relay log的文件路径,默认值hostname-relay-bin
relay_log_index=relay-log.index 默认值hostname-relay-bin.index
 使用有复制权限的用户账号连接至主服务器,并启动复制线程
MariaDB [(none)]> CHANGE MASTER TO
  MASTER_HOST='192.168.50.127',  主机ip
  MASTER_USER='repluser',              具有复制权限的账户
  MASTER_PASSWORD='123456',    账户密码
  MASTER_PORT=3306,                     端口
  MASTER_LOG_FILE='mysql-bin.000003',    复制时的二进制日志位置
  MASTER_LOG_POS=404;

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.50.127
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 404
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             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: 404
              Relay_Log_Space: 825
              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)

3、使用MHA实现Mysql高可用。

本次实验需要4台机器 1台管理主机 1主两从数据库服务器
1 数据主机配置:

 [root@localhost ~]# cat /etc/my.cnf
[mysqld]
log-bin
server_id=1
skip_name_resolve=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

MariaDB [(none)]> grant all on *.* to mhauser@'172.16.100.%'identified by 'magedu';    创建管理用户
ariaDB [(none)]> grant replication slave on *.* to repluser@'172.16.100.%' identified by 'magedu'; 创建复制用户

MariaDB [(none)]> select user,host,password from mysql.user;
+----------+-----------------------+-------------------------------------------+
| user     | host                  | password                                  |
+----------+-----------------------+-------------------------------------------+
| root     | localhost             |                                           |
| root     | localhost.localdomain |                                           |
| root     | 127.0.0.1             |                                           |
| root     | ::1                   |                                           |
|          | localhost             |                                           |
|          | localhost.localdomain |                                           |
| repluser | 172.16.100.%          | *6B8CCC83799A26CD19D7AD9AEEADBCD30D8A8664 |
| mhauser  | 172.16.100.%          | *6B8CCC83799A26CD19D7AD9AEEADBCD30D8A8664 |
+----------+-----------------------+-------------------------------------------+
8 rows in set (0.00 sec)

从节点配置(两台除了server_id其它一样)

[root@localhost .ssh]# cat /etc/my.cnf
[mysqld]
server_id=x
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

启动三台服务器的mariadb

[root@localhost ~]# systemctl start mariadb

搭建主从复制(两台从一样)

MariaDB [(none)]> CHANGE MASTER TO 
MASTER_HOST='172.16.100.46', 
MASTER_USER='repluser', 
MASTER_PASSWORD='magedu', 
MASTER_LOG_FILE='mariadb-bin.000001', 
MASTER_LOG_POS=245;

MariaDB [(none)]> start slave; 启动
MariaDB [(none)]> show slave status\G   查看状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.100.47
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000002
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000002
             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: 245
              Relay_Log_Space: 827
              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: 2
1 row in set (0.00 sec)

管理主机配置
先配好相应的yum源并开启epel

yum -y install mha*.rpm
mkdir /etc/mastermha/

配置管理节点

vim /etc/mastermha/app1.cnf 
[server default]
user=mhauser
password=magedu
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=magedu
ping_interval=1
[server1]
hostname=172.16.100.46
candidate_master=1
[server2]
hostname=172.16.100.47
candidate_master=1
[server3]
hostname=172.16.100.48

搭建免密登录

ssh-keygen
ssh-copy-id 172.16.100.43
将生成的公钥分发到各管理节点
 scp -r .ssh 172.16.100.46:/root/
 scp -r .ssh 172.16.100.47:/root/
 scp -r .ssh 172.16.100.48:/root/

各节点安装node包

yum install mha4mysql-node-0.56-0.el6.noarch.rpm

Mha验证和启动

masterha_check_ssh --conf=/etc/mastermha/app1.cnf
masterha_check_repl --conf=/etc/mastermha/app1.cnf
masterha_manager --conf=/etc/mastermha/app1.cnf

设置故障检测
关闭主数据库
查看报错日志

cat /data/mastermha/app1/manager.log
Tue Sep  1 23:06:50 2020 - [info] MHA::MasterMonitor version 0.56.
Tue Sep  1 23:06:51 2020 - [info] GTID failover mode = 0
Tue Sep  1 23:06:51 2020 - [info] Dead Servers:
Tue Sep  1 23:06:51 2020 - [info] Alive Servers:
Tue Sep  1 23:06:51 2020 - [info]   172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:06:51 2020 - [info]   172.16.100.47(172.16.100.47:3306)
Tue Sep  1 23:06:51 2020 - [info]   172.16.100.48(172.16.100.48:3306)
Tue Sep  1 23:06:51 2020 - [info] Alive Slaves:
Tue Sep  1 23:06:51 2020 - [info]   172.16.100.47(172.16.100.47:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:06:51 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:06:51 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Sep  1 23:06:51 2020 - [info]   172.16.100.48(172.16.100.48:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:06:51 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:06:51 2020 - [info] Current Alive Master: 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:06:51 2020 - [info] Checking slave configurations..
Tue Sep  1 23:06:51 2020 - [info] Checking replication filtering settings..
Tue Sep  1 23:06:51 2020 - [info]  binlog_do_db= , binlog_ignore_db= 
Tue Sep  1 23:06:51 2020 - [info]  Replication filtering check ok.
Tue Sep  1 23:06:51 2020 - [info] GTID (with auto-pos) is not supported
Tue Sep  1 23:06:51 2020 - [info] Starting SSH connection tests..
Tue Sep  1 23:06:54 2020 - [info] All SSH connection tests passed successfully.
Tue Sep  1 23:06:54 2020 - [info] Checking MHA Node version..
Tue Sep  1 23:06:55 2020 - [info]  Version check ok.
Tue Sep  1 23:06:55 2020 - [info] Checking SSH publickey authentication settings on the current master..
Tue Sep  1 23:06:55 2020 - [info] HealthCheck: SSH to 172.16.100.46 is reachable.
Tue Sep  1 23:06:55 2020 - [info] Master MHA Node version is 0.56.
Tue Sep  1 23:06:55 2020 - [info] Checking recovery script configurations on 172.16.100.46(172.16.100.46:3306)..
Tue Sep  1 23:06:55 2020 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql,/var/log/mysql --output_file=/data/mastermha/app1//save_binary_logs_test --manager_version=0.56 --start_file=mariadb-bin.000003 
Tue Sep  1 23:06:55 2020 - [info]   Connecting to root@172.16.100.46(172.16.100.46:22).. 
  Creating /data/mastermha/app1 if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to mariadb-bin.000003
Tue Sep  1 23:06:55 2020 - [info] Binlog setting check done.
Tue Sep  1 23:06:55 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Tue Sep  1 23:06:55 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=172.16.100.47 --slave_ip=172.16.100.47 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=5.5.65-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Tue Sep  1 23:06:55 2020 - [info]   Connecting to root@172.16.100.47(172.16.100.47:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mariadb-relay-bin.000008
    Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000008
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Tue Sep  1 23:06:55 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=172.16.100.48 --slave_ip=172.16.100.48 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=5.5.65-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Tue Sep  1 23:06:55 2020 - [info]   Connecting to root@172.16.100.48(172.16.100.48:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mariadb-relay-bin.000008
    Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000008
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Tue Sep  1 23:06:56 2020 - [info] Slaves settings check done.
Tue Sep  1 23:06:56 2020 - [info] 
172.16.100.46(172.16.100.46:3306) (current master)
 +--172.16.100.47(172.16.100.47:3306)
 +--172.16.100.48(172.16.100.48:3306)

Tue Sep  1 23:06:56 2020 - [warning] master_ip_failover_script is not defined.
Tue Sep  1 23:06:56 2020 - [warning] shutdown_script is not defined.
Tue Sep  1 23:06:56 2020 - [info] Set master ping interval 1 seconds.
Tue Sep  1 23:06:56 2020 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Tue Sep  1 23:06:56 2020 - [info] Starting ping health check on 172.16.100.46(172.16.100.46:3306)..
Tue Sep  1 23:06:56 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
Tue Sep  1 23:23:02 2020 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
Tue Sep  1 23:23:02 2020 - [info] Executing SSH check script: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql,/var/log/mysql --output_file=/data/mastermha/app1//save_binary_logs_test --manager_version=0.56 --binlog_prefix=mariadb-bin
Tue Sep  1 23:23:02 2020 - [warning] HealthCheck: SSH to 172.16.100.46 is NOT reachable.
Tue Sep  1 23:23:04 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '172.16.100.46' (4))
Tue Sep  1 23:23:04 2020 - [warning] Connection failed 2 time(s)..
Tue Sep  1 23:23:05 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '172.16.100.46' (4))
Tue Sep  1 23:23:05 2020 - [warning] Connection failed 3 time(s)..
Tue Sep  1 23:23:06 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '172.16.100.46' (4))
Tue Sep  1 23:23:06 2020 - [warning] Connection failed 4 time(s)..
Tue Sep  1 23:23:06 2020 - [warning] Master is not reachable from health checker!
Tue Sep  1 23:23:06 2020 - [warning] Master 172.16.100.46(172.16.100.46:3306) is not reachable!
Tue Sep  1 23:23:06 2020 - [warning] SSH is NOT reachable.
Tue Sep  1 23:23:06 2020 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/mastermha/app1.cnf again, and trying to connect to all servers to check server status..
Tue Sep  1 23:23:06 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Sep  1 23:23:06 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Tue Sep  1 23:23:06 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Tue Sep  1 23:23:07 2020 - [info] GTID failover mode = 0
Tue Sep  1 23:23:07 2020 - [info] Dead Servers:
Tue Sep  1 23:23:07 2020 - [info]   172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:07 2020 - [info] Alive Servers:
Tue Sep  1 23:23:07 2020 - [info]   172.16.100.47(172.16.100.47:3306)
Tue Sep  1 23:23:07 2020 - [info]   172.16.100.48(172.16.100.48:3306)
Tue Sep  1 23:23:07 2020 - [info] Alive Slaves:
Tue Sep  1 23:23:07 2020 - [info]   172.16.100.47(172.16.100.47:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:07 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:07 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Sep  1 23:23:07 2020 - [info]   172.16.100.48(172.16.100.48:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:07 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:07 2020 - [info] Checking slave configurations..
Tue Sep  1 23:23:07 2020 - [info] Checking replication filtering settings..
Tue Sep  1 23:23:07 2020 - [info]  Replication filtering check ok.
Tue Sep  1 23:23:07 2020 - [info] Master is down!
Tue Sep  1 23:23:07 2020 - [info] Terminating monitoring script.
Tue Sep  1 23:23:07 2020 - [info] Got exit code 20 (Master dead).
Tue Sep  1 23:23:07 2020 - [info] MHA::MasterFailover version 0.56.
Tue Sep  1 23:23:07 2020 - [info] Starting master failover.
Tue Sep  1 23:23:07 2020 - [info] 
Tue Sep  1 23:23:07 2020 - [info] * Phase 1: Configuration Check Phase..
Tue Sep  1 23:23:07 2020 - [info] 
Tue Sep  1 23:23:08 2020 - [info] GTID failover mode = 0
Tue Sep  1 23:23:08 2020 - [info] Dead Servers:
Tue Sep  1 23:23:08 2020 - [info]   172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:08 2020 - [info] Checking master reachability via MySQL(double check)...
Tue Sep  1 23:23:09 2020 - [info]  ok.
Tue Sep  1 23:23:09 2020 - [info] Alive Servers:
Tue Sep  1 23:23:09 2020 - [info]   172.16.100.47(172.16.100.47:3306)
Tue Sep  1 23:23:09 2020 - [info]   172.16.100.48(172.16.100.48:3306)
Tue Sep  1 23:23:09 2020 - [info] Alive Slaves:
Tue Sep  1 23:23:09 2020 - [info]   172.16.100.47(172.16.100.47:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:09 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:09 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Sep  1 23:23:09 2020 - [info]   172.16.100.48(172.16.100.48:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:09 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:09 2020 - [info] Starting Non-GTID based failover.
Tue Sep  1 23:23:09 2020 - [info] 
Tue Sep  1 23:23:09 2020 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Sep  1 23:23:09 2020 - [info] 
Tue Sep  1 23:23:09 2020 - [info] * Phase 2: Dead Master Shutdown Phase..
Tue Sep  1 23:23:09 2020 - [info] 
Tue Sep  1 23:23:09 2020 - [info] Forcing shutdown so that applications never connect to the current master..
Tue Sep  1 23:23:09 2020 - [warning] master_ip_failover_script is not set. Skipping invalidating dead master IP address.
Tue Sep  1 23:23:09 2020 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Tue Sep  1 23:23:10 2020 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 3: Master Recovery Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] The latest binary log file/position on all slaves is mariadb-bin.000003:143246
Tue Sep  1 23:23:10 2020 - [info] Latest slaves (Slaves that received relay log files to the latest):
Tue Sep  1 23:23:10 2020 - [info]   172.16.100.47(172.16.100.47:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:10 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:10 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Sep  1 23:23:10 2020 - [info]   172.16.100.48(172.16.100.48:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:10 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:10 2020 - [info] The oldest binary log file/position on all slaves is mariadb-bin.000003:143246
Tue Sep  1 23:23:10 2020 - [info] Oldest slaves:
Tue Sep  1 23:23:10 2020 - [info]   172.16.100.47(172.16.100.47:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:10 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:10 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Sep  1 23:23:10 2020 - [info]   172.16.100.48(172.16.100.48:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:10 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [warning] Dead Master is not SSH reachable. Could not save it's binlogs. Transactions that were not sent to the latest slave (Read_Master_Log_Pos to the tail of the dead master's binlog) were lost.
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 3.3: Determining New Master Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
Tue Sep  1 23:23:10 2020 - [info] All slaves received relay logs to the same position. No need to resync each other.
Tue Sep  1 23:23:10 2020 - [info] Searching new master from slaves..
Tue Sep  1 23:23:10 2020 - [info]  Candidate masters from the configuration file:
Tue Sep  1 23:23:10 2020 - [info]   172.16.100.47(172.16.100.47:3306)  Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Sep  1 23:23:10 2020 - [info]     Replicating from 172.16.100.46(172.16.100.46:3306)
Tue Sep  1 23:23:10 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Sep  1 23:23:10 2020 - [info]  Non-candidate masters:
Tue Sep  1 23:23:10 2020 - [info]  Searching from candidate_master slaves which have received the latest relay log events..
Tue Sep  1 23:23:10 2020 - [info] New master is 172.16.100.47(172.16.100.47:3306)
Tue Sep  1 23:23:10 2020 - [info] Starting master failover..
Tue Sep  1 23:23:10 2020 - [info] 
From:
172.16.100.46(172.16.100.46:3306) (current master)
 +--172.16.100.47(172.16.100.47:3306)
 +--172.16.100.48(172.16.100.48:3306)

To:
172.16.100.47(172.16.100.47:3306) (new master)
 +--172.16.100.48(172.16.100.48:3306)
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 3.3: New Master Diff Log Generation Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info]  This server has all relay logs. No need to generate diff files from the latest slave.
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 3.4: Master Log Apply Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
Tue Sep  1 23:23:10 2020 - [info] Starting recovery on 172.16.100.47(172.16.100.47:3306)..
Tue Sep  1 23:23:10 2020 - [info]  This server has all relay logs. Waiting all logs to be applied.. 
Tue Sep  1 23:23:10 2020 - [info]   done.
Tue Sep  1 23:23:10 2020 - [info]  All relay logs were successfully applied.
Tue Sep  1 23:23:10 2020 - [info] Getting new master's binlog name and position..
Tue Sep  1 23:23:10 2020 - [info]  mariadb-bin.000002:245
Tue Sep  1 23:23:10 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.16.100.47', MASTER_PORT=3306, MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=245, MASTER_USER='repluser', MASTER_PASSWORD='xxx';
Tue Sep  1 23:23:10 2020 - [warning] master_ip_failover_script is not set. Skipping taking over new master IP address.
Tue Sep  1 23:23:10 2020 - [info] Setting read_only=0 on 172.16.100.47(172.16.100.47:3306)..
Tue Sep  1 23:23:10 2020 - [info]  ok.
Tue Sep  1 23:23:10 2020 - [info] ** Finished master recovery successfully.
Tue Sep  1 23:23:10 2020 - [info] * Phase 3: Master Recovery Phase completed.
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 4: Slaves Recovery Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
Tue Sep  1 23:23:10 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info] -- Slave diff file generation on host 172.16.100.48(172.16.100.48:3306) started, pid: 15723. Check tmp log /data/mastermha/app1//172.16.100.48_3306_20200901232307.log if it takes time..
Tue Sep  1 23:23:11 2020 - [info] 
Tue Sep  1 23:23:11 2020 - [info] Log messages from 172.16.100.48 ...
Tue Sep  1 23:23:11 2020 - [info] 
Tue Sep  1 23:23:10 2020 - [info]  This server has all relay logs. No need to generate diff files from the latest slave.
Tue Sep  1 23:23:11 2020 - [info] End of log messages from 172.16.100.48.
Tue Sep  1 23:23:11 2020 - [info] -- 172.16.100.48(172.16.100.48:3306) has the latest relay log events.
Tue Sep  1 23:23:11 2020 - [info] Generating relay diff files from the latest slave succeeded.
Tue Sep  1 23:23:11 2020 - [info] 
Tue Sep  1 23:23:11 2020 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
Tue Sep  1 23:23:11 2020 - [info] 
Tue Sep  1 23:23:11 2020 - [info] -- Slave recovery on host 172.16.100.48(172.16.100.48:3306) started, pid: 15725. Check tmp log /data/mastermha/app1//172.16.100.48_3306_20200901232307.log if it takes time..
Tue Sep  1 23:23:12 2020 - [info] 
Tue Sep  1 23:23:12 2020 - [info] Log messages from 172.16.100.48 ...
Tue Sep  1 23:23:12 2020 - [info] 
Tue Sep  1 23:23:11 2020 - [info] Starting recovery on 172.16.100.48(172.16.100.48:3306)..
Tue Sep  1 23:23:11 2020 - [info]  This server has all relay logs. Waiting all logs to be applied.. 
Tue Sep  1 23:23:11 2020 - [info]   done.
Tue Sep  1 23:23:11 2020 - [info]  All relay logs were successfully applied.
Tue Sep  1 23:23:11 2020 - [info]  Resetting slave 172.16.100.48(172.16.100.48:3306) and starting replication from the new master 172.16.100.47(172.16.100.47:3306)..
Tue Sep  1 23:23:11 2020 - [info]  Executed CHANGE MASTER.
Tue Sep  1 23:23:11 2020 - [info]  Slave started.
Tue Sep  1 23:23:12 2020 - [info] End of log messages from 172.16.100.48.
Tue Sep  1 23:23:12 2020 - [info] -- Slave recovery on host 172.16.100.48(172.16.100.48:3306) succeeded.
Tue Sep  1 23:23:12 2020 - [info] All new slave servers recovered successfully.
Tue Sep  1 23:23:12 2020 - [info] 
Tue Sep  1 23:23:12 2020 - [info] * Phase 5: New master cleanup phase..
Tue Sep  1 23:23:12 2020 - [info] 
Tue Sep  1 23:23:12 2020 - [info] Resetting slave info on the new master..
Tue Sep  1 23:23:12 2020 - [info]  172.16.100.47: Resetting slave info succeeded.
Tue Sep  1 23:23:12 2020 - [info] Master failover to 172.16.100.47(172.16.100.47:3306) completed successfully.
Tue Sep  1 23:23:12 2020 - [info] 

----- Failover Report -----

app1: MySQL Master failover 172.16.100.46(172.16.100.46:3306) to 172.16.100.47(172.16.100.47:3306) succeeded

Master 172.16.100.46(172.16.100.46:3306) is down!

Check MHA Manager logs at localhost.localdomain:/data/mastermha/app1/manager.log for details.

Started automated(non-interactive) failover.
The latest slave 172.16.100.47(172.16.100.47:3306) has all relay logs for recovery.
Selected 172.16.100.47(172.16.100.47:3306) as a new master.
172.16.100.47(172.16.100.47:3306): OK: Applying all logs succeeded.  
172.16.100.48(172.16.100.48:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
172.16.100.48(172.16.100.48:3306): OK: Applying all logs succeeded. Slave started, replicating from 172.16.100.47(172.16.100.47:3306)
172.16.100.47(172.16.100.47:3306): Resetting slave info succeeded.
Master failover to 172.16.100.47(172.16.100.47:3306) completed successfully.
[root@localhost yum.repos.d]# 

发现已将172.16.100.47从设置为主数据库
查看从数据库状态

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.100.47
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000002
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000002
             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: 245
              Relay_Log_Space: 827
              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: 2
1 row in set (0.00 sec)

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,841评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,415评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,904评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,051评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,055评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,255评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,729评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,377评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,517评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,420评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,467评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,144评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,735评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,812评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,029评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,528评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,126评论 2 341