1、主从复制及主主复制的实现
环境:Centos8*2+mariadb
master:10.0.0.8
slave:10.0.0.18
主节点:
[root@master ~]# dnf install -y mariadb-server
[root@master ~]# vi /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=8
log_bin
[root@master ~]# systemctl start mariadb
[root@master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-MariaDB-log MariaDB Server
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 master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 28198 |
| mariadb-bin.000002 | 344 |
+--------------------+-----------+
2 rows in set (0.000 sec)
#创建复制账号
MariaDB [(none)]> grant replication slave on *.* to repluser@'10.0.0.%' identified by 'magedu';
Query OK, 0 rows affected (0.001 sec)
#slave节点
[root@slave ~]# dnf -y install mariadb-server
[root@slave ~]# vi /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=18
[root@slave ~]# systemctl start mariadb
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.8',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='magedu',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=344;
Query OK, 0 rows affected (0.015 sec)
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
主主复制
master1 10.0.0.8 master2 10.0.0.18
#在第一个master节点上
[root@master1 ~]# vi /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=8
log-bin
auto_increment_offset=1
auto_increment_increment=2
[root@master1 ~]# systemctl start mariadb
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 28430 |
| mariadb-bin.000002 | 344 |
+--------------------+-----------+
2 rows in set (0.000 sec)
MariaDB [(none)]> grant replication slave on *.* to repluser@'10.0.0.%' identified by 'magedu';
#master2上
[root@master2 ~]# vi /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=18
log-bin
auto_increment_offset=2
auto_increment_increment=2
[root@master2 ~]# systemctl start mariadb
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.8',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='magedu',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=344;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.8
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000002
Read_Master_Log_Pos: 546
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 759
Relay_Master_Log_File: mariadb-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#查看二进制日志
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 28430 |
| mariadb-bin.000002 | 344 |
+--------------------+-----------+
2 rows in set (0.000 sec)
#在master1上
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.18',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='magedu',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=344;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.18
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000002
Read_Master_Log_Pos: 344
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 557
Relay_Master_Log_File: mariadb-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2、xtrabackup实现全量+增量+binlog恢复库
3、MyCAT实现MySQL读写分离
Mycat :10.0.08
master:10.0.0.18
slave:10.0.0.28
1、创建Mysql主从
a、在master和slave上
dnf install -y mariadb-server
修改主从配置文件
master:
[mysqld]
server-id=18
log-bin
slave
[mysqld]
server-id=28
启动数据库
systemctl start mariadb
b、master上创建复制账号
MariaDB [(none)]> grant replication slave on *.* to repluser@'10.0.0.%' identified by 'magedu';
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 28198 |
| mariadb-bin.000002 | 541 |
+--------------------+--------
c、在slave上
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.18',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='magedu',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=541;
MariaDB [(none)]> start slave;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.18
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000002
Read_Master_Log_Pos: 662
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 678
Relay_Master_Log_File: mariadb-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2、在代理10.0.0.8上安装mycat
[root@mycat ~]# yum install -y java
[root@mycat ~]# mkdir /app
[root@mycat ~]# tar xvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /app
#配置环境变量
[root@mycat mycat]# echo 'PATH=/app/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@mycat mycat]# source /etc/profile.d/mycat.sh
#启动mycat
[root@mycat /]# mycat start
Starting Mycat-server..
#可以看到打开多个端口,其中8066端口用于连接MyCAT
[root@mycat /]# ss -ntpl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=680,fd=4),("systemd",pid=1,fd=24))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=768,fd=4))
LISTEN 0 128 0.0.0.0:15128 0.0.0.0:* users:(("rpc.statd",pid=872,fd=8))
LISTEN 0 1 127.0.0.1:32000 0.0.0.0:* users:(("java",pid=30481,fd=4))
LISTEN 0 100 *:8066 *:* users:(("java",pid=30481,fd=91))
LISTEN 0 128 [::]:45250 [::]:* users:(("rpc.statd",pid=872,fd=10))
LISTEN 0 100 *:9066 *:* users:(("java",pid=30481,fd=87))
LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=680,fd=6),("systemd",pid=1,fd=31))
LISTEN 0 50 *:23728 *:* users:(("java",pid=30481,fd=68))
LISTEN 0 50 *:11670 *:* users:(("java",pid=30481,fd=66))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=768,fd=6))
LISTEN 0 50 *:1984 *:* users:(("java",pid=30481,fd=67))
#在Centos7的客户端上测试
[root@centos7 ~]# mysql -uroot -p123456 -h 10.0.0.8 -P8066
MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
3、在mycat 服务器上修改server.xml文件配置Mycat的连接信息
4、修改schema.xml实现读写分离策略
[root@mycat /]#vi /app/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="mycat" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="host1" url="10.0.0.18:3306" user="root" password="123456">
<readHost host="host2" url="10.0.0.28:3306" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
#重启mycat
[root@mycat conf]# mycat restart
#查看日志
[root@mycat logs]# tail -f wrapper.log
STATUS | wrapper | 2021/09/10 11:27:57 | --> Wrapper Started as Daemon
STATUS | wrapper | 2021/09/10 11:27:58 | Launching a JVM...
INFO | jvm 1 | 2021/09/10 11:27:59 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2021/09/10 11:27:59 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2021/09/10 11:27:59 |
INFO | jvm 1 | 2021/09/10 11:28:03 | MyCAT Server startup successfully. see logs in logs/mycat.log
STATUS | wrapper | 2021/09/10 12:27:03 | TERM trapped. Shutting down.
STATUS | wrapper | 2021/09/10 12:27:05 | <-- Wrapper Stopped
STATUS | wrapper | 2021/09/10 12:27:05 | --> Wrapper Started as Daemon
STATUS | wrapper | 2021/09/10 12:27:06 | Launching a JVM...
INFO | jvm 1 | 2021/09/10 12:27:07 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2021/09/10 12:27:07 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2021/09/10 12:27:07 |
INFO | jvm 1 | 2021/09/10 12:27:08 | MyCAT Server startup successfully. see logs in logs/mycat.log
5、在后端主服务器创建用户并对mycat授权
MariaDB [(none)]> create database mycat;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> GRANT ALL ON *.* TO 'root'@'10.0.0.%' IDENTIFIED BY '123456' ;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> use mycat;
MariaDB [mycat]> create table t1(id int);
6、在Centos7客户端上连接并测试
[root@centos7 ~]# mysql -uroot -p123456 -h 10.0.0.8 -P8066
MySQL [(none)]> show databases;
MySQL [(none)]> use TESTDB
MySQL [TESTDB]> show tables;
+-----------------+
| Tables_in_mycat |
+-----------------+
| t1 |
MySQL [TESTDB]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 28 |
+-------------+
1 row in set (0.01 sec)
MySQL [TESTDB]> select @@hostname;
+------------+
| @@hostname |
+------------+
| slave |
4、MHA实现
四台机器:
10.0.0.7 Centos7 MHA管理节点
10.0.0.8 Centos8 master
10.0.0.18 Centos8 slave1
10.0.0.28 Centos8 slave2
在管理节点上安装两个包mha4mysql-manager和mha4mysql-node
[root@mha-manager ~]# yum install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
[root@mha-manager ~]# yum -y install mha4mysql-node-0.58-0.el7.centos.noarch
在所有MySQL服务器上安装mha4mysql-node包
[root@master ~]# yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm
在所有节点实现相互之间ssh key验证
[root@mha-manager ~]#ssh-keygen
[root@mha-manager ~]#ssh-copy-id 127.0.0.1
[root@mha-manager ~]#rsync -av .ssh 10.0.0.8:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.18:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.28:/root/
在管理节点建立配置文件
[root@mha-manager ~]# mkdir /etc/mastermha
[root@mha-manager ~]# 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
master_ip_failover_script=/usr/local/bin/master_ip_failover
report_script=/usr/local/bin/sendmail.sh
check_repl_delay=0
master_binlog_dir=/data/mysql/
[server1]
hostname=10.0.0.8
candidate_master=1
[server2]
hostname=10.0.0.18
candidate_master=1
[server3]
hostname=10.0.0.28
相关脚本
[root@mha-manager ~]#cat /usr/local/bin/sendmail.sh
#!/bin/bash
echo "MySQL is down" | mail -s "MHA Warning" root@wangxiaochun.com
[root@mha-manager ~]# vim /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = '10.0.0.100/24';
my $gateway = '10.0.0.254';
my $interface = 'eth0';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
chmod +x /usr/local/bin/master_ip_failover
实现master
[root@master /]# mkdir /data/mysql/
[root@master /]# useradd -r -s /sbin/nologin mysql
[root@master /]# chown mysql.mysql /data/mysql/
[root@master /]# dnf -y install mysql-server
[root@master /]# vim /etc/my.cnf
[mysqld]
server_id=1
log-bin=/data/mysql/mysql-bin
skip_name_resolve=1
general_log
[root@master /]# systemctl enable --now mysqld
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 | 179 | No |
| mysql-bin.000002 | 156 | No |
+------------------+-----------+-----------+
mysql> create user repluser@'10.0.0.%' identified by 'magedu';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to repluser@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)
mysql> create user mhauser@'10.0.0.%' identified by 'magedu';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to mhauser@'10.0.0.%';
Query OK, 0 rows affected (0.01 sec)
#配置VIP
[root@master /]# ifconfig eth0:1 10.0.0.100/24
实现slave
[root@slave1 ~]# mkdir /data/mysql
[root@slave1 ~]# useradd -r -s /sbin/nologin mysql
[root@slave1 mysql]# chown mysql.mysql /data/mysql/
[root@slave1 ~]# dnf -y install mysql-server
[root@slave ~]#vim /etc/my.cnf
[mysqld]
server_id=2 #不同节点此值各不相同
log-bin=/data/mysql/mysql-bin
read_only
relay_log_purge=0
skip_name_resolve=1 #禁止反向解析
general_log #方便观察的设置,生产无需启用
[root@slave ~]#systemctl enable --now mysqld
mysql> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.8',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='magedu',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=156;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.8
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1201
Relay_Log_File: slave1-relay-bin.000002
Relay_Log_Pos: 1369
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
检查MHA的环境
[root@mha-manager ~]# masterha_check_ssh --conf=/etc/mastermha/app1.cnf
Fri Sep 10 17:46:01 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep 10 17:46:01 2021 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Fri Sep 10 17:46:01 2021 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Fri Sep 10 17:46:01 2021 - [info] Starting SSH connection tests..
Fri Sep 10 17:46:02 2021 - [debug]
Fri Sep 10 17:46:01 2021 - [debug] Connecting via SSH from root@10.0.0.8(10.0.0.8:22) to root@10.0.0.18(10.0.0.18:22)..
Fri Sep 10 17:46:02 2021 - [debug] ok.
Fri Sep 10 17:46:02 2021 - [debug] Connecting via SSH from root@10.0.0.8(10.0.0.8:22) to root@10.0.0.28(10.0.0.28:22)..
Warning: Permanently added '10.0.0.28' (ECDSA) to the list of known hosts.
Fri Sep 10 17:46:02 2021 - [debug] ok.
Fri Sep 10 17:46:03 2021 - [debug]
Fri Sep 10 17:46:01 2021 - [debug] Connecting via SSH from root@10.0.0.18(10.0.0.18:22) to root@10.0.0.8(10.0.0.8:22)..
Fri Sep 10 17:46:02 2021 - [debug] ok.
Fri Sep 10 17:46:02 2021 - [debug] Connecting via SSH from root@10.0.0.18(10.0.0.18:22) to root@10.0.0.28(10.0.0.28:22)..
Warning: Permanently added '10.0.0.28' (ECDSA) to the list of known hosts.
Fri Sep 10 17:46:02 2021 - [debug] ok.
Fri Sep 10 17:46:03 2021 - [debug]
Fri Sep 10 17:46:02 2021 - [debug] Connecting via SSH from root@10.0.0.28(10.0.0.28:22) to root@10.0.0.8(10.0.0.8:22)..
Fri Sep 10 17:46:02 2021 - [debug] ok.
Fri Sep 10 17:46:02 2021 - [debug] Connecting via SSH from root@10.0.0.28(10.0.0.28:22) to root@10.0.0.18(10.0.0.18:22)..
Fri Sep 10 17:46:03 2021 - [debug] ok.
Fri Sep 10 17:46:03 2021 - [info] All SSH connection tests passed successfully.
[root@mha-manager ~]# masterha_check_repl --conf=/etc/mastermha/app1.cnf
Fri Sep 10 17:53:53 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep 10 17:53:53 2021 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Fri Sep 10 17:53:53 2021 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Fri Sep 10 17:53:53 2021 - [info] MHA::MasterMonitor version 0.58.
Fri Sep 10 17:53:54 2021 - [info] GTID failover mode = 0
Fri Sep 10 17:53:54 2021 - [info] Dead Servers:
Fri Sep 10 17:53:54 2021 - [info] Alive Servers:
Fri Sep 10 17:53:54 2021 - [info] 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 17:53:54 2021 - [info] 10.0.0.18(10.0.0.18:3306)
Fri Sep 10 17:53:54 2021 - [info] 10.0.0.28(10.0.0.28:3306)
Fri Sep 10 17:53:54 2021 - [info] Alive Slaves:
Fri Sep 10 17:53:54 2021 - [info] 10.0.0.18(10.0.0.18:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 17:53:54 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 17:53:54 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep 10 17:53:54 2021 - [info] 10.0.0.28(10.0.0.28:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 17:53:54 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 17:53:54 2021 - [info] Current Alive Master: 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 17:53:54 2021 - [info] Checking slave configurations..
Fri Sep 10 17:53:54 2021 - [info] Checking replication filtering settings..
Fri Sep 10 17:53:54 2021 - [info] binlog_do_db= , binlog_ignore_db=
Fri Sep 10 17:53:54 2021 - [info] Replication filtering check ok.
Fri Sep 10 17:53:54 2021 - [info] GTID (with auto-pos) is not supported
Fri Sep 10 17:53:54 2021 - [info] Starting SSH connection tests..
Fri Sep 10 17:53:56 2021 - [info] All SSH connection tests passed successfully.
Fri Sep 10 17:53:56 2021 - [info] Checking MHA Node version..
Fri Sep 10 17:53:57 2021 - [info] Version check ok.
Fri Sep 10 17:53:57 2021 - [info] Checking SSH publickey authentication settings on the current master..
Fri Sep 10 17:53:57 2021 - [info] HealthCheck: SSH to 10.0.0.8 is reachable.
Fri Sep 10 17:53:58 2021 - [info] Master MHA Node version is 0.56.
Fri Sep 10 17:53:58 2021 - [info] Checking recovery script configurations on 10.0.0.8(10.0.0.8:3306)..
Fri Sep 10 17:53:58 2021 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/ --output_file=/data/mastermha/app1//save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000002
Fri Sep 10 17:53:58 2021 - [info] Connecting to root@10.0.0.8(10.0.0.8:22)..
Creating /data/mastermha/app1 if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /data/mysql/, up to mysql-bin.000002
Fri Sep 10 17:53:58 2021 - [info] Binlog setting check done.
Fri Sep 10 17:53:58 2021 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Sep 10 17:53:58 2021 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=10.0.0.18 --slave_ip=10.0.0.18 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=8.0.21 --manager_version=0.58 --relay_dir=/var/lib/mysql --current_relay_log=slave1-relay-bin.000002 --slave_pass=xxx
Fri Sep 10 17:53:58 2021 - [info] Connecting to root@10.0.0.18(10.0.0.18:22)..
Checking slave recovery environment settings..
Relay log found at /var/lib/mysql, up to slave1-relay-bin.000002
Temporary relay log file is /var/lib/mysql/slave1-relay-bin.000002
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Fri Sep 10 17:53:59 2021 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=10.0.0.28 --slave_ip=10.0.0.28 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=8.0.21 --manager_version=0.58 --relay_dir=/var/lib/mysql --current_relay_log=slave2-relay-bin.000002 --slave_pass=xxx
Fri Sep 10 17:53:59 2021 - [info] Connecting to root@10.0.0.28(10.0.0.28:22)..
Checking slave recovery environment settings..
Relay log found at /var/lib/mysql, up to slave2-relay-bin.000002
Temporary relay log file is /var/lib/mysql/slave2-relay-bin.000002
Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Fri Sep 10 17:53:59 2021 - [info] Slaves settings check done.
Fri Sep 10 17:53:59 2021 - [info]
10.0.0.8(10.0.0.8:3306) (current master)
+--10.0.0.18(10.0.0.18:3306)
+--10.0.0.28(10.0.0.28:3306)
Fri Sep 10 17:53:59 2021 - [info] Checking replication health on 10.0.0.18..
Fri Sep 10 17:53:59 2021 - [info] ok.
Fri Sep 10 17:53:59 2021 - [info] Checking replication health on 10.0.0.28..
Fri Sep 10 17:53:59 2021 - [info] ok.
Fri Sep 10 17:53:59 2021 - [info] Checking master_ip_failover_script status:
Fri Sep 10 17:53:59 2021 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=10.0.0.8 --orig_master_ip=10.0.0.8 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 10.0.0.100/24;/sbin/arping -I eth0 -c 3 -s 10.0.0.100/24 10.0.0.254 >/dev/null 2>&1===
Checking the Status of the script.. OK
Fri Sep 10 17:53:59 2021 - [info] OK.
Fri Sep 10 17:53:59 2021 - [warning] shutdown_script is not defined.
Fri Sep 10 17:53:59 2021 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
[root@mha-manager ~]# masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
启动MHA
#开启MHA,默认是前台运行,生产环境一般为后台执行
[root@mha-manager ~]# nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null
#查看状态
[root@mha-manager ~]# masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:28012) is running(0:PING_OK), master:10.0.0.8
模拟故障
#当 master down机后,mha管理程序自动退出
Fri Sep 10 18:07:07 2021 - [warning] Got error on MySQL select ping: 1053 (Server shutdown in progress)
Fri Sep 10 18:07:07 2021 - [info] Executing SSH check script: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/ --output_file=/data/mastermha/app1//save_binary_logs_test --manager_version=0.58 --binlog_prefix=mysql-bin
Fri Sep 10 18:07:07 2021 - [info] HealthCheck: SSH to 10.0.0.8 is reachable.
Fri Sep 10 18:07:08 2021 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '10.0.0.8' (111))
Fri Sep 10 18:07:08 2021 - [warning] Connection failed 2 time(s)..
Fri Sep 10 18:07:09 2021 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '10.0.0.8' (111))
Fri Sep 10 18:07:09 2021 - [warning] Connection failed 3 time(s)..
Fri Sep 10 18:07:10 2021 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '10.0.0.8' (111))
Fri Sep 10 18:07:10 2021 - [warning] Connection failed 4 time(s)..
Fri Sep 10 18:07:10 2021 - [warning] Master is not reachable from health checker!
Fri Sep 10 18:07:10 2021 - [warning] Master 10.0.0.8(10.0.0.8:3306) is not reachable!
Fri Sep 10 18:07:10 2021 - [warning] SSH is reachable.
Fri Sep 10 18:07:10 2021 - [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..
Fri Sep 10 18:07:10 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep 10 18:07:10 2021 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Fri Sep 10 18:07:10 2021 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Fri Sep 10 18:07:11 2021 - [info] GTID failover mode = 0
Fri Sep 10 18:07:11 2021 - [info] Dead Servers:
Fri Sep 10 18:07:11 2021 - [info] 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:11 2021 - [info] Alive Servers:
Fri Sep 10 18:07:11 2021 - [info] 10.0.0.18(10.0.0.18:3306)
Fri Sep 10 18:07:11 2021 - [info] 10.0.0.28(10.0.0.28:3306)
Fri Sep 10 18:07:11 2021 - [info] Alive Slaves:
Fri Sep 10 18:07:11 2021 - [info] 10.0.0.18(10.0.0.18:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:11 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:11 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep 10 18:07:11 2021 - [info] 10.0.0.28(10.0.0.28:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:11 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:11 2021 - [info] Checking slave configurations..
Fri Sep 10 18:07:11 2021 - [info] Checking replication filtering settings..
Fri Sep 10 18:07:11 2021 - [info] Replication filtering check ok.
Fri Sep 10 18:07:11 2021 - [info] Master is down!
Fri Sep 10 18:07:11 2021 - [info] Terminating monitoring script.
Fri Sep 10 18:07:11 2021 - [info] Got exit code 20 (Master dead).
Fri Sep 10 18:07:11 2021 - [info] MHA::MasterFailover version 0.58.
Fri Sep 10 18:07:11 2021 - [info] Starting master failover.
Fri Sep 10 18:07:11 2021 - [info]
Fri Sep 10 18:07:11 2021 - [info] * Phase 1: Configuration Check Phase..
Fri Sep 10 18:07:11 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] GTID failover mode = 0
Fri Sep 10 18:07:12 2021 - [info] Dead Servers:
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:12 2021 - [info] Checking master reachability via MySQL(double check)...
Fri Sep 10 18:07:12 2021 - [info] ok.
Fri Sep 10 18:07:12 2021 - [info] Alive Servers:
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.18(10.0.0.18:3306)
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.28(10.0.0.28:3306)
Fri Sep 10 18:07:12 2021 - [info] Alive Slaves:
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.18(10.0.0.18:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:12 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:12 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.28(10.0.0.28:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:12 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:12 2021 - [info] Starting Non-GTID based failover.
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] ** Phase 1: Configuration Check Phase completed.
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] * Phase 2: Dead Master Shutdown Phase..
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] Forcing shutdown so that applications never connect to the current master..
Fri Sep 10 18:07:12 2021 - [info] Executing master IP deactivation script:
Fri Sep 10 18:07:12 2021 - [info] /usr/local/bin/master_ip_failover --orig_master_host=10.0.0.8 --orig_master_ip=10.0.0.8 --orig_master_port=3306 --command=stopssh --ssh_user=root
IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 10.0.0.100/24;/sbin/arping -I eth0 -c 3 -s 10.0.0.100/24 10.0.0.254 >/dev/null 2>&1===
Disabling the VIP on old master: 10.0.0.8
Fri Sep 10 18:07:12 2021 - [info] done.
Fri Sep 10 18:07:12 2021 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Fri Sep 10 18:07:12 2021 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] * Phase 3: Master Recovery Phase..
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] The latest binary log file/position on all slaves is mysql-bin.000002:11797
Fri Sep 10 18:07:12 2021 - [info] Latest slaves (Slaves that received relay log files to the latest):
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.18(10.0.0.18:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:12 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:12 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.28(10.0.0.28:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:12 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:12 2021 - [info] The oldest binary log file/position on all slaves is mysql-bin.000002:11797
Fri Sep 10 18:07:12 2021 - [info] Oldest slaves:
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.18(10.0.0.18:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:12 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:12 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep 10 18:07:12 2021 - [info] 10.0.0.28(10.0.0.28:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:12 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:12 2021 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
Fri Sep 10 18:07:12 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] Fetching dead master's binary logs..
Fri Sep 10 18:07:13 2021 - [info] Executing command on the dead master 10.0.0.8(10.0.0.8:3306): save_binary_logs --command=save --start_file=mysql-bin.000002 --start_pos=11797 --binlog_dir=/data/mysql/ --output_file=/data/mastermha/app1//saved_master_binlog_from_10.0.0.8_3306_20210910180711.binlog --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.58
Creating /data/mastermha/app1 if not exists.. ok.
Concat binary/relay logs from mysql-bin.000002 pos 11797 to mysql-bin.000002 EOF into /data/mastermha/app1//saved_master_binlog_from_10.0.0.8_3306_20210910180711.binlog ..
Binlog Checksum enabled
Dumping binlog format description event, from position 0 to 156.. ok.
No need to dump effective binlog data from /data/mysql//mysql-bin.000002 (pos starts 11797, filesize 11797). Skipping.
Binlog Checksum enabled
/data/mastermha/app1//saved_master_binlog_from_10.0.0.8_3306_20210910180711.binlog has no effective data events.
Event not exists.
Fri Sep 10 18:07:13 2021 - [info] Additional events were not found from the orig master. No need to save.
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] * Phase 3.3: Determining New Master Phase..
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
Fri Sep 10 18:07:13 2021 - [info] All slaves received relay logs to the same position. No need to resync each other.
Fri Sep 10 18:07:13 2021 - [info] Searching new master from slaves..
Fri Sep 10 18:07:13 2021 - [info] Candidate masters from the configuration file:
Fri Sep 10 18:07:13 2021 - [info] 10.0.0.18(10.0.0.18:3306) Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Fri Sep 10 18:07:13 2021 - [info] Replicating from 10.0.0.8(10.0.0.8:3306)
Fri Sep 10 18:07:13 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep 10 18:07:13 2021 - [info] Non-candidate masters:
Fri Sep 10 18:07:13 2021 - [info] Searching from candidate_master slaves which have received the latest relay log events..
Fri Sep 10 18:07:13 2021 - [info] New master is 10.0.0.18(10.0.0.18:3306)
Fri Sep 10 18:07:13 2021 - [info] Starting master failover..
Fri Sep 10 18:07:13 2021 - [info]
From:
10.0.0.8(10.0.0.8:3306) (current master)
+--10.0.0.18(10.0.0.18:3306)
+--10.0.0.28(10.0.0.28:3306)
To:
10.0.0.18(10.0.0.18:3306) (new master)
+--10.0.0.28(10.0.0.28:3306)
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] * Phase 3.4: New Master Diff Log Generation Phase..
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] * Phase 3.5: Master Log Apply Phase..
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
Fri Sep 10 18:07:13 2021 - [info] Starting recovery on 10.0.0.18(10.0.0.18:3306)..
Fri Sep 10 18:07:13 2021 - [info] This server has all relay logs. Waiting all logs to be applied..
Fri Sep 10 18:07:13 2021 - [info] done.
Fri Sep 10 18:07:13 2021 - [info] All relay logs were successfully applied.
Fri Sep 10 18:07:13 2021 - [info] Getting new master's binlog name and position..
Fri Sep 10 18:07:13 2021 - [info] mysql-bin.000002:12040
Fri Sep 10 18:07:13 2021 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.18', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=12040, MASTER_USER='repluser', MASTER_PASSWORD='xxx';
Fri Sep 10 18:07:13 2021 - [info] Executing master IP activate script:
Fri Sep 10 18:07:13 2021 - [info] /usr/local/bin/master_ip_failover --command=start --ssh_user=root --orig_master_host=10.0.0.8 --orig_master_ip=10.0.0.8 --orig_master_port=3306 --new_master_host=10.0.0.18 --new_master_ip=10.0.0.18 --new_master_port=3306 --new_master_user='mhauser' --new_master_password=xxx
Unknown option: new_master_user
Unknown option: new_master_password
IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 10.0.0.100/24;/sbin/arping -I eth0 -c 3 -s 10.0.0.100/24 10.0.0.254 >/dev/null 2>&1===
Enabling the VIP - 10.0.0.100/24 on the new master - 10.0.0.18
Fri Sep 10 18:07:13 2021 - [info] OK.
Fri Sep 10 18:07:13 2021 - [info] Setting read_only=0 on 10.0.0.18(10.0.0.18:3306)..
Fri Sep 10 18:07:13 2021 - [info] ok.
Fri Sep 10 18:07:13 2021 - [info] ** Finished master recovery successfully.
Fri Sep 10 18:07:13 2021 - [info] * Phase 3: Master Recovery Phase completed.
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] * Phase 4: Slaves Recovery Phase..
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
Fri Sep 10 18:07:13 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] -- Slave diff file generation on host 10.0.0.28(10.0.0.28:3306) started, pid: 28764. Check tmp log /data/mastermha/app1//10.0.0.28_3306_20210910180711.log if it takes time..
Fri Sep 10 18:07:14 2021 - [info]
Fri Sep 10 18:07:14 2021 - [info] Log messages from 10.0.0.28 ...
Fri Sep 10 18:07:14 2021 - [info]
Fri Sep 10 18:07:13 2021 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
Fri Sep 10 18:07:14 2021 - [info] End of log messages from 10.0.0.28.
Fri Sep 10 18:07:14 2021 - [info] -- 10.0.0.28(10.0.0.28:3306) has the latest relay log events.
Fri Sep 10 18:07:14 2021 - [info] Generating relay diff files from the latest slave succeeded.
Fri Sep 10 18:07:14 2021 - [info]
Fri Sep 10 18:07:14 2021 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
Fri Sep 10 18:07:14 2021 - [info]
Fri Sep 10 18:07:14 2021 - [info] -- Slave recovery on host 10.0.0.28(10.0.0.28:3306) started, pid: 28766. Check tmp log /data/mastermha/app1//10.0.0.28_3306_20210910180711.log if it takes time..
Fri Sep 10 18:07:15 2021 - [info]
Fri Sep 10 18:07:15 2021 - [info] Log messages from 10.0.0.28 ...
Fri Sep 10 18:07:15 2021 - [info]
Fri Sep 10 18:07:14 2021 - [info] Starting recovery on 10.0.0.28(10.0.0.28:3306)..
Fri Sep 10 18:07:14 2021 - [info] This server has all relay logs. Waiting all logs to be applied..
Fri Sep 10 18:07:14 2021 - [info] done.
Fri Sep 10 18:07:14 2021 - [info] All relay logs were successfully applied.
Fri Sep 10 18:07:14 2021 - [info] Resetting slave 10.0.0.28(10.0.0.28:3306) and starting replication from the new master 10.0.0.18(10.0.0.18:3306)..
Fri Sep 10 18:07:15 2021 - [info] Executed CHANGE MASTER.
Fri Sep 10 18:07:15 2021 - [info] Slave started.
Fri Sep 10 18:07:15 2021 - [info] End of log messages from 10.0.0.28.
Fri Sep 10 18:07:15 2021 - [info] -- Slave recovery on host 10.0.0.28(10.0.0.28:3306) succeeded.
Fri Sep 10 18:07:15 2021 - [info] All new slave servers recovered successfully.
Fri Sep 10 18:07:15 2021 - [info]
Fri Sep 10 18:07:15 2021 - [info] * Phase 5: New master cleanup phase..
Fri Sep 10 18:07:15 2021 - [info]
Fri Sep 10 18:07:15 2021 - [info] Resetting slave info on the new master..
Fri Sep 10 18:07:16 2021 - [info] 10.0.0.18: Resetting slave info succeeded.
Fri Sep 10 18:07:16 2021 - [info] Master failover to 10.0.0.18(10.0.0.18:3306) completed successfully.
Fri Sep 10 18:07:16 2021 - [info]
----- Failover Report -----
app1: MySQL Master failover 10.0.0.8(10.0.0.8:3306) to 10.0.0.18(10.0.0.18:3306) succeeded
Master 10.0.0.8(10.0.0.8:3306) is down!
Check MHA Manager logs at mha-manager:/data/mastermha/app1/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 10.0.0.8(10.0.0.8:3306)
The latest slave 10.0.0.18(10.0.0.18:3306) has all relay logs for recovery.
Selected 10.0.0.18(10.0.0.18:3306) as a new master.
10.0.0.18(10.0.0.18:3306): OK: Applying all logs succeeded.
10.0.0.18(10.0.0.18:3306): OK: Activated master IP address.
10.0.0.28(10.0.0.28:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.0.0.28(10.0.0.28:3306): OK: Applying all logs succeeded. Slave started, replicating from 10.0.0.18(10.0.0.18:3306)
10.0.0.18(10.0.0.18:3306): Resetting slave info succeeded.
Master failover to 10.0.0.18(10.0.0.18:3306) completed successfully.
Fri Sep 10 18:07:16 2021 - [info] Sending mail..
#验证VIP漂移至新的Master上
[root@Centos8 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.18 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fecf:8562 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:cf:85:62 txqueuelen 1000 (Ethernet)
RX packets 11199 bytes 11712142 (11.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4589 bytes 853043 (833.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
ether 00:0c:29:cf:85:62 txqueuelen 1000 (Ethernet)
5、Ansible常用模块总结
Command 模块
功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项
注意:此命令不支持 $VARNAME < > | ; & 等,可能用shell模块实现
注意:此模块不具有幂等性
ansible websrvs -m command -a 'service vsftpd start'
Shell 模块
功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, >
注意:此模块不具有幂等性
ansible websrvs -m shell -a 'echo centos | passwd --stdin wang'
Script 模块
功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)
注意:此模块不具有幂等性
ansible websrvs -m script -a /data/test.sh
Copy 模块
功能:从ansible服务器主控端复制文件到远程主机
注意: src=file 如果是没指明路径,则为当前目录或当前目录下的files目录下的file文件
ansible websrvs -m copy -a "src=/root/test1.sh dest=/tmp/test2.sh owner=wang
mode=600 backup=yes"
File 模块
功能:设置文件属性,创建软链接等
#创建空文件
ansible all -m file -a 'path=/data/test.txt state=touch'
ansible all -m file -a 'path=/data/test.txt state=absent'
ansible all -m file -a "path=/root/test.sh owner=wang mode=755"
#创建目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql
group=mysql"
#创建软链接
ansible all -m file -a 'src=/data/testfile path|dest|name=/data/testfile-link
state=link
Cron 模块
功能:计划任务
支持时间:minute,hour,day,month,weekday
#创建任务
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql"
job=/root/mysql_backup.sh'
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com
&>/dev/null' name=Synctime"
#禁用计划任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
&>/dev/null' name=Synctime disabled=yes"
#启用计划任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
&>/dev/null' name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name='backup mysql' state=absent"
ansible websrvs -m cron -a 'state=absent name=Synctime'
Yum 和 Apt 模块
功能:
yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本
apt 模块管理 Debian 相关版本的软件包
ansible websrvs -m yum -a 'name=httpd state=present' #安装
ansible websrvs -m yum -a 'name=nginx state=present enablerepo=epel' #启用epel源
进行安装
ansible websrvs -m yum -a 'name=* state=lastest exclude=kernel*,foo*' #升级除
kernel和foo开头以外的所有包
ansible websrvs -m yum -a 'name=httpd state=absent' #删除