1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。
mysql备份工具介绍
1.mysqldump工具 推荐使用
mysqldump是mysql自带的备份工具,简单,易用
mysqldump命令主要参数有:
-A:备份全库
-F, --flush-logs :备份前滚动日志,锁定表完成后,执行flush logs命令,生成新的
二进制日志文件,配合-A 或 -B 选项时,会导致刷新多次数据库。建议在同一时刻
执行转储和日志刷新,可通过和--single-transaction或-x,--master-data 一起使
用实现,此时只刷新一次日志
--compact 去掉注释,适合调试,生产不使用
-d, --no-data 只备份表结构
-t, --no-create-info 只备份数据,不备份create table
-n,--no-create-db 不备份create database,可被-A或-B覆盖
--flush-privileges 备份mysql或相关时需要使用
-f, --force 忽略SQL错误,继续执行
--hex-blob 使用十六进制符号转储二进制列,当有包括BINARY,
VARBINARY,BLOB,BIT的数据类型的列时使用,避免乱码
-q, --quick 不缓存查询,直接输出,加快备份速度
--single-transaction参数的作用,设置事务的隔离级别为可重复读,即REPEATABLE READ,这样能保证在一个事务中所有相同的查询读取到同样的数据,也就大概保证了在dump期间,如果其他innodb引擎的线程修改了表的数据并提交,对该dump线程的数据并无影响,在这期间不会锁表。
#常用备份策略
#1. mysqldump -uusername -ppassword -A --single-transaction --master-data=1 --default-character-set=utf8 <$BACKUP/fullbak_$BACKUP_TIME.sql
2.xtrabackup备份工具
支持增量备份;只支持innodb引擎备份。
#使用时需要安装
#yum install percona-xtrabackup|xtrabackup 在EPEL源中,需要配置epel源
常用选项:
--user:该选项表示备份账号
--password:该选项表示备份的密码
--host:该选项表示备份数据库的地址
--databases:该选项接受的参数为数据库名,如果要指定多个数据库,彼此间需要以空格隔开;
如:"xtra_test dba_test",同时,在指定某数据库时,也可以只指定其中的某张表。
如:"mydatabase.mytable"。该选项对innodb引擎表无效,还是会备份所有innodb表
--defaults-file:该选项指定从哪个文件读取MySQL配置,必须放在命令行第一个选项位置
--incremental:该选项表示创建一个增量备份,需要指定--incremental-basedir
--incremental-basedir:该选项指定为前一次全备份或增量备份的目录,与--incremental同时使用
--incremental-dir:该选项表示还原时增量备份的目录
--include=name:指定表名,格式:databasename.tablename
#全备命令直接使用如下
xtrabackup --user=$username --password=$password=password --backup --target-dir=/backup/
脚本实现
[root@master-mariadb bakup]# vim /data/backup.sh
#!/bin/bash
#
read -p "database user name is:" USER
read -p "database password is:" PASSWORD
#backup backup_dir
BACKUP_DIR=/data/bak
#directory is not exsit to make
[ -d $BACKUP_DIR ]|| mkdir $BACKUP_DIR
#clear bakcup dir
[ -d /backup ] && rm /backup/* -rf && echo -e "\033[32mclear backup dir !!!\033[0m"
#print meanu
while true;do
cat <<EOF
Please input a number to choice you back up tool:
1.mysqldump full backup
2.xtrabackup full backup
3.quit
EOF
read -p "Your choose: " choose
#select to backup tools backup all
case "$choose" in
1)
mysqldump -u${USER} -p${PASSWORD} -A --single-transaction --master-data=1 --default-character-set=utf8 > ${BACKUP_DIR}/f
ullbak_$(date +%F_%T).sql && echo -e "\033[31mmysqldump backup all is successful!\033[0m"
;;
2)
xtrabackup --user=${USER} --password=${PASSWORD} --backup --target-dir=/backup/ >& /dev/null && echo -e "\033[33mxtraback
up bakcup all is sucessful!\033[0m"
;;
3)
echo "Bye!" && exit 2
;;
*)
echo "Choose Error !" && exit 3
;;
esac
"/data/backup.sh" 45L, 1050C written
#测试成功
#注意用户名和密码一定要给正确的,此脚本不检测用户名密码 输错会报错
[root@master-mariadb bakup]# bash /data/backup.sh
database user name is:root
database password is:centos
clear backup dir !!!
Please input a number to choice you back up tool:
1.mysqldump full backup
2.xtrabackup full backup
3.quit
Your choose: 1
mysqldump backup all is successful!
Please input a number to choice you back up tool:
1.mysqldump full backup
2.xtrabackup full backup
3.quit
Your choose: 2
xtrabackup bakcup all is sucessful!
Please input a number to choice you back up tool:
1.mysqldump full backup
2.xtrabackup full backup
3.quit
Your choose: 3
Bye!
[root@master-mariadb bakup]# ll /backup/
-rw-r----- 1 root root 386 Jul 8 14:38 backup-my.cnf
-rw-r----- 1 root root 18874368 Jul 8 14:38 ibdata1
drwx------ 2 root root 4096 Jul 8 14:38 mysql
drwx------ 2 root root 4096 Jul 8 14:38 performance_schema
drwx------ 2 root root 19 Jul 8 14:38 test
-rw-r----- 1 root root 113 Jul 8 14:38 xtrabackup_checkpoint
-rw-r----- 1 root root 445 Jul 8 14:38 xtrabackup_info
-rw-r----- 1 root root 2560 Jul 8 14:38 xtrabackup_logfile
[root@master-mariadb bakup]# ll /data/bak
-rw-r--r-- 1 root root 515093 Jul 8 14:18 fullbak_2020-07-08_14:18:38.sql
2、配置Mysql主从同步。
主从复制介绍
MySQL主从复制,是其自带的功能,它是MySQL的单机的架构的有效扩展,可实现读写分离,有效降低主服务器的压力;
可以实现的功能:
- 可以实现主从复制,读写分离,需要配合前端的调度器;
- 可以实现复制过滤,分别都可以再主从节点上配置;
- 可以实现复制过程中,数据加密需要开启ssl;
主从复制原理
图片.png
主节点启动dump Thread进程:为每个Slave的I/O Thread启动一个dump线程,用于向其发送binary log events
从节点先启动I/O Thread进程:向Master请求二进制日志事件,并保存于中继日志中 然后启动SQL Thread进程:从中继日志中读取日志事件,在本地完成重放
主从复制架构
图片.png
图片.png
主从复制的实现:明文传输方式
规划:
主机 | 作用 |
---|---|
192.168.37.7 | master-mariadb |
192.168.37.17 | slave-mariadb |
准备阶段:
1.安装mariadb服务器
#192.168.37.7和192.168.37.17都需要安装
yum install mariadb-server -y
#示例直接yum安装即可,生产场景可以考虑二进制,或源码编译安装
[root@mysql-master ~]# hostnamectl set-hostname master-mariadb
#设置主机名 非必要,只为了好记
主从复制的实现:
1.主节点的配置
#1.主配置文件/etc/my.cnf的配置
#主要是server-id=num要不一样
#主节点一定开启二进制日志,生成中将二进制日志,和数据库要分开存放
[root@master-mariadb yum.repos.d]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
server-id=7
log-bin
socket=/var/lib/mysql/mysql.sock
#2. 主服务器导入实验数据库hellodb
[root@master-mariadb test]# mysql < hellodb_innodb.sql
[root@master-mariadb test]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB 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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
#3.创建复制用户
#创建用户具有replication slave权限
MariaDB [(none)]> grant replication slave on *.* to repluse@'192.168.37.%' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+---------+----------------+-------------------------------------------+
| user | host | password |
+---------+----------------+-------------------------------------------+
| root | localhost | |
| root | master-mariadb | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | master-mariadb | |
| repluse | 192.168.37.% | *128977E278358FF80A246B5046F51043A2B1FCED |
+---------+----------------+-------------------------------------------+
7 rows in set (0.00 sec)
#主节点备份数据,恢复到从节点,保证主从节点的一致性
[root@master-mariadb test]# mysqldump -A --single-transaction --master-data=1 > /data/bak/allbak.sql
[root@master-mariadb test]# ls -lh /data/bak/allbak.sql
-rw-r--r-- 1 root root 510K Jul 1 16:06 /data/bak/allbak.sql
#拷贝备份至目标从服务器
[root@master-mariadb test]# scp /data/bak/allbak.sql 192.168.37.17:/data/bak/
root@192.168.37.17's password:
allbak.sql 100% 510KB 509.6KB/s 00:00
2.从节点的配置
#server-id要不同,read-only可以加也可以不加,log-bin开不开没有强制要求
[root@slave-mariadb1 yum.repos.d]# vim /etc/my.cnf
[mysqld]
server-id=17
read-only
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#修改复制过来的全备文件,导入数据库
#备份文件修改change master to段
CHANGE MASTER TO
MASTER_HOST='192.168.37.7',
#只想主节点的ip
MASTER_USER='repluser',
#复制的用户
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
#注意是port
MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=7
809;
#二进制日志的位置信息
#改好以后直接开启数据库,导入数据
[root@slave-mariadb1 yum.repos.d]# systemctl start mariadb
[root@slave-mariadb1 yum.repos.d]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB 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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> source /data/bak/allbak.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
#以下查看复制的状态,主服务器的ip,端口啦,要检查一下是否配置正确
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.37.7
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 7809
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 7809
Relay_Log_Space: 245
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
1 row in set (0.00 sec)
MariaDB [test]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 17 |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [test]> show variables like 'read_only';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | ON |
+---------------+-------+
1 row in set (0.00 sec)
#开启复制进程
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
#再次查看状态
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.37.7
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 7809
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Connecting
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: 7809
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: 1045
Last_IO_Error: error connecting to master 'repluser@192.168.37.7:3306' - retry-time: 60 retries: 86400 message: Access denied for user 'repluser'@'192.168.37.17' (using password: YES)
#错误是主节点上的复制账户对不上
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
1 row in set (0.00 sec)
主从服务器复制测试
MariaDB [(none)]> create database db1;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
#37.17测试
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
主从复制的实现通信加密传输
1.生成各种CA的证书,主,从节点证书
[root@master-mariadb test]# mkdir /etc/my.cnf.d/ssl
MariaDB [(none)]> quit
Bye
[root@master-mariadb ssl]# cd /etc/my.cnf.d/ssl
[root@master-mariadb ssl]# ls
cacert.pem master.crt master.key slave.csr
cakey.pem master.csr slave.crt slave.key
[root@master-mariadb ssl]# rm -rf *
[root@master-mariadb ssl]# ls
'[root@master-mariadb ssl]# ls
[root@master-mariadb ssl]# openssl genrsa 2048 > cakey.pem
Generating RSA private key, 2048 bit long modulus
......................................+++
.....................................................................+++
e is 65537 (0x10001)
[root@master-mariadb ssl]# cat cakey.pem
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAsStzQiQGi7WC1gPkIDNjj3UQ3mqa0CI7aZ+ztuva7eAxQ+b3
4DWnCzJVe6ou1wHqvOXqwZw5M95sejgFwsh0/l3GZtsiG81TIhrneW9KMspu/4F7
HxV5Zfw2nyGhuRF8OSiFnR436xqAXcICXSK8+NiGKUm6rg8wx6+Z3TMQawMy/Caa
rUQtUPWiRw4SPlR3MfhNU+peX7qrqECA/T/AAEMbd5/WLIXGGoz8fdW7Bn9OlxME
kWSj+tGYzN+cBFmf7YOMHyZ3Lk0idu3kl7WXo6WBjVZtuhk++4b7Wn3kpFElp32N
JKlgOpZKsKQxZ30VhcVEWWtIxpp1s5mAju2yrQIDAQABAoIBAB0HU4E4yeHGQcOz
K4OjJ+LveW4X5XnIwhdSVW3ZQyvKmgfYJjEUWWRaQOtcPQR5ie6ddVN3t1+qR/fW
igsaMSEkWEeuC+6mO8LQybB57ZxY0ssGehfjutpc8s4sAoWCOepbm1jdBKh+1R2H
lj+QW4oiYAkPzYiLcKCnQvydS0ldHQarYjxaWaTAHPFaUIkmu3lwX/4n66ve3gP7
zuXvm7CkjHBNxnAK+W8v+JxvebTt3PrYkgi+Xh2bmwSfai+j1c3oLWUXDkt6iS6U
+SN5ETDMSRx3ZHogNp8Or0JQNN8+4Q1Oj8eUMIifN8pbVrN4eg3dru/BD836+xTH
CRGzRYECgYEA2L8gY42xFzG1JUQ6YJ4OttN/fbygTFBPLOmLH/B73hgqVdxXTPo1
xgk1klHgjQiWaxRNNE0+x9He2Jcv9JxKOT8NsJ/6ssqWG746GbwgdK3YNQoiG+HR
oSSfAPSsmqv6CRWTkVyia14hPUz7VhIRTkkTz0N/qE9Io9pWIlDIG50CgYEA0UFy
6B86ENnVf8oOcI3WWnhp/uDEnXweuy8xSAX7fNEPo5bwc4GGXoUsUNfk0lbML9TX
uH1bDTlz2z1OCAfvOsX3tbP9rJV5aA9JnPpU/VfMlfihiOFe0cgYCHzQBV63tNvs
oLUAzY10nQPXXkthQIebRDnTCKwVek/X5gVl7lECgYBtANnKF1UrY/1Ey6ZMNtER
kxX35yf6mRzoal9kZa31yNNpsNFazi7C9JiziGfUpa/4FBKrrflXxq064fXlH8CY
P6Sj4iDTT9zkAOv5S0pLQ66LJhh3Sz8AuokYkvRHKRwq+2XmYd7w/jrbOF7iL13y
r/+9aaXGwHC4rrJIubWZZQKBgA8yyb3CRt1idPdLHbFfG+qGRGyWtrEZisHds56f
kJyIMrKXC+/XwIKs+YMFq0D7vOjcdK6ZrDtRqPCLi1dZ/C4wosU3xXz7fk3ojlgw
dmvQLLPX0yO1+sUR220CY7mAH6erMYyamOxVEds1pDbdErs1HjneWU2P8h9oHS4/
xh+BAoGAU5kEvA+cXy6F60LUuI3tNBp7YcNk3Rm4QerjaD/pcLProkMtE/g6EMZ5
NvAibgaDS37ny5NMUKqh9ds1Pnu5IEWde9wlu4dsTcTWJ90UglIp5JBWHqk1NtEH
ROlPgQZKDUiNzWuTNhaB7iHC4ZsYSlYn836gFsA03ZoVyZ90RlA=
-----END RSA PRIVATE KEY-----
[root@master-mariadb ssl]# openssl req -new -x509 -key cakey.pem -out cacert.pem -days 3650
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:devops
Common Name (eg, your name or your server's hostname) []:ca.magedu.com
Email Address []:
[root@master-mariadb ssl]# ll
total 8
-rw-r--r-- 1 root root 1330 Jul 2 16:34 cacert.pem
-rw-r--r-- 1 root root 1675 Jul 2 16:32 cakey.pem
[root@master-mariadb ssl]# openssl req -newkey rsa:1024 -days 365 -nodes -keyout master.key > master.csr
Generating a 1024 bit RSA private key
.................................................................++++++
.......++++++
writing new private key to 'master.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:devops
Common Name (eg, your name or your server's hostname) []:master.magedu.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@master-mariadb ssl]# ls
cacert.pem cakey.pem master.csr master.key
[root@master-mariadb ssl]# openssl x509 -req -in master.csr -CA cacert.pem -CAkey cakey.pem -set_serial 01 > master.crt
Signature ok
subject=/C=CN/ST=beijing/L=beijing/O=magedu/OU=devops/CN=master.magedu.com
Getting CA Private Key
[root@master-mariadb ssl]# openssl req -newkey rsa:1024 -nodes -keyout slave.key > slave.csr
Generating a 1024 bit RSA private key
................++++++
..++++++
writing new private key to 'slave.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:devops
Common Name (eg, your name or your server's hostname) []:slave.magedu.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@master-mariadb ssl]# openssl x509 -req -in slave.csr -CA cacert.pem -CAkey cakey.pem -set_serial 02 > slave.crt
Signature ok
subject=/C=CN/ST=beijing/L=beijing/O=magedu/OU=devops/CN=slave.magedu.com
Getting CA Private Key
[root@master-mariadb ssl]# ll
total 32
-rw-r--r-- 1 root root 1383 Jul 2 15:37 cacert.pem
-rw-r--r-- 1 root root 1679 Jul 2 15:35 cakey.pem
-rw-r--r-- 1 root root 1054 Jul 2 15:42 master.crt
-rw-r--r-- 1 root root 660 Jul 2 15:40 master.csr
-rw-r--r-- 1 root root 916 Jul 2 15:40 master.key
-rw-r--r-- 1 root root 1054 Jul 2 15:46 slave.crt
-rw-r--r-- 1 root root 660 Jul 2 15:44 slave.csr
-rw-r--r-- 1 root root 916 Jul 2 15:44 slave.key
2.主节点的配置
#主配置文件配置
[root@master-mariadb ssl]# vim /etc/my.cnf
[mysqld]
server-id=7
log-bin
#主要与上面生成的证书对应
ssl-ca=/etc/my.cnf.d/ssl/cacert.pem
ssl-cert=/etc/my.cnf.d/ssl/master.crt
ssl-key=/etc/my.cnf.d/ssl/master.key
#重启服务以加密方式连接数据库
[root@master-mariadb ssl]# systemctl restart mariadb
[root@master-mariadb ssl]# mysql --ssl-ca=cacert.pem --ssl-cert=master.crt --ssl-key=master.key
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.65-MariaDB 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)]> status;
--------------
mysql Ver 15.1 Distrib 5.5.65-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 5
Current database:
Current user: root@localhost
SSL: Cipher in use is DHE-RSA-AES256-GCM-SHA384
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 5.5.65-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 1 min 58 sec
Threads: 3 Questions: 27 Slow queries: 0 Opens: 0 Flush tables: 2 Open tables: 26 Queries per second avg: 0.228
--------------
#拷贝密钥文件给从节点
[root@master-mariadb ssl]# scp -r /etc/my.cnf.d/ssl/ 192.168.37.17:/etc/my.cnf.d/ssl/
root@192.168.37.17's password:
cakey.pem 100% 1679 1.6KB/s 00:00
cacert.pem 100% 1383 1.4KB/s 00:00
master.csr 100% 660 0.6KB/s 00:00
master.key 100% 916 0.9KB/s 00:00
master.crt 100% 1054 1.0KB/s 00:00
slave.csr 100% 660 0.6KB/s 00:00
slave.key 100% 916 0.9KB/s 00:00
slave.crt 100% 1054 1.0KB/s 00:00
#添加用户连接强制要求加密连接
MariaDB [(none)]> grant replication slave on *.* to repluser2@'192.168.37.%' identified by 'centos' require ssl;
Query OK, 0 rows affected (0.00 sec)
3.从节点的配置
[root@slave-mariadb1 ssl]# vim /etc/my.cnf
[mysqld]
server-id=17
#指定key的位置
ssl-ca=/etc/my.cnf.d/ssl/cacert.pem
ssl-cert=/etc/my.cnf.d/ssl/slave.crt
ssl-key=/etc/my.cnf.d/ssl/slave.key
"/etc/my.cnf" 25L, 718C written
[root@slave-mariadb1 ssl]# systemctl restart mariadb
#整理change master to
CHANGE MASTER TO
MASTER_HOST='192.168.37.7',
MASTER_USER='repluser2',
MASTER_PASSWORD='centos',
MASTER_SSL=1,
MASTER_LOG_FILE='mariadb-bin.000005',
MASTER_LOG_POS=245,
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.37.7',
-> MASTER_USER='repluser2',
-> MASTER_PASSWORD='centos',
-> MASTER_SSL=1,
#强制要求加密
-> MASTER_LOG_FILE='mariadb-bin.000005',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 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.37.7
Master_User: repluser2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000005
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
Relay_Master_Log_File: mariadb-bin.000005
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: Yes
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: 7
1 row in set (0.01 sec)
3、使用MHA实现Mysql高可用。
什么是MHA?
MHA:Master High Availability,对主节点进行监控,可实现自动故障转移至其它从节点;通过提升某一从节点为新的主节点,基于主从复制实现,还需要客户端配合实现,MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库。
MHA软件由两部分组成,Manager工具包和Node工具包 即MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一台独立的机器上,它可以管理多个master-slave集群,也可以部署在一台slave节点上。当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。MHA Manager和MHA node的通信方式采用SSH key验证方式;
MHA简单架构
图片.png
MHA的实现
以单组主从复制为例,搭建MHA集群架构,实现主服务器宕机,或故障时,自动转移至其它从节点;
规划
主机 | 作用 |
---|---|
192.168.37.17 | MASTER-MYSQL |
192.168.37.7 | MHA manager |
192.168.37.27 | SLAVE1-MYSQL |
192.168.37.37 | SLAVE2-MYSQL |
准备阶段:
主机设置主机名,便于管理,可做可不做
#192.168.37.7主机名设置为mha-manager
[root@master-mariadb ~]# hostnamectl set-hostname mha-manager
[root@master-mariadb ~]# logout
[root@mha-manager ~]#
#192.168.37.17主机名设置为master
[root@slave-mariadb1 ~]# hostnamectl set-hostname master
[root@slave-mariadb1 ~]# logout
[root@master ~]#
#192.168.37.27主机名设置为slave1
[root@slave-mariadb2 ~]# hostnamectl set-hostname slave1
[root@slave-mariadb2 ~]# logout
[root@slave1 ~]#
#192.168.37.37主机名设置为slave2
[root@slave1 ~]# hostnamectl set-hostname slave2
[root@slave1 ~]# logout
[root@slave2 ~]#
主从复制的实现
master节点的配置
#1. 主配置文件
[root@master ~]# vim /etc/my.cnf
skip_name_resolve
[mysqld]
server-id=17
log-bin
skip_name_resolve
#2. 启动服务
[root@master ~]# systemctl start mariadb
[root@master test]# rz
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring hellodb_innodb.sql...
100% 7 KB 7 KB/sec 00:00:01 0 Errors
[root@master test]# mysql < hellodb_innodb.sql
#导入测试数据库
#创建主从复制用户,以及mha用户
[root@master test]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.65-MariaDB 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)]> grant replication slave on *.* to repluser@'192.168.37.%' identified by 'magedu';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> grant all on *.* to mhauser@'192.168.37.%' identified by 'magedu';
Query OK, 0 rows affected (0.00 sec)
#主从复制全备,并拷贝至各从节点
[root@master test]# mysqldump -A --single-transaction --master-data=1 > /data/all.sql
#拷贝至从节点
MariaDB [(none)]> quit
Bye
[root@master test]# mysqldump -A --single-transaction --master-data=1 > /data/all.sql
[root@master test]# scp /data/all.sql 192.168.37.27:/data/
root@192.168.37.27's password:
all.sql 100% 510KB 509.8KB/s 00:00
[root@master test]# scp /data/all.sql 192.168.37.37:/data/
root@192.168.37.37's password:
all.sql 100% 510KB 509.8KB/s 00:00
[root@master test]# mysql -e "show databases"
#导入的测试数据库成功
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
[root@master test]# mysql -e "select user,password from mysql.user"
+----------+-------------------------------------------+
| user | password |
+----------+-------------------------------------------+
| root | |
| root | |
| root | |
| root | |
| | |
| | |
| repluser | *128977E278358FF80A246B5046F51043A2B1FCED |
| mhauser | *128977E278358FF80A246B5046F51043A2B1FCED |
+----------+-------------------------------------------+
slave节点的配置
#192.168.37.27
[root@slave1 ~]# vim /etc/my.cnf
[mysqld]
server-id=27
log-bin
read-only
relay_log_purge=0
skip_name_resolve
[root@slave1 ~]# systemctl start mariadb
#修改change master to并导入从库
[root@slave1 ~]# vim /data/all.sql
CHANGE MASTER TO
master_host='192.168.37.17',
master_port=3306,
master_user="repluser",
master_password="centos",
MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=7950;
[root@slave1 ~]# mysql < /data/all.sql
[root@slave1 ~]# mysql -e "show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
[root@slave1 ~]# mysql -e "select user,password from mysql.user"
+----------+-------------------------------------------+
| user | password |
+----------+-------------------------------------------+
| root | |
| root | |
| root | |
| root | |
| | |
| | |
| repluser | *128977E278358FF80A246B5046F51043A2B1FCED |
| mhauser | *128977E278358FF80A246B5046F51043A2B1FCED |
+----------+-------------------------------------------+
#启动主从复制
[root@slave1 ~]# mysql -e "start slave"
[root@slave1 ~]# mysql -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.37.17
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 7950
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
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: 7950
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: 17
#192.168.37.37
[root@slave2 ~]# vim /etc/my.cnf
[mysqld]
server-id=37
log-bin
read-only
relay_log_purge=0
skip_name_resolve
[root@slave2 ~]# systemctl start mariadb
[root@slave2 ~]# vim /data/all.sql
CHANGE MASTER TO
master_host='192.168.37.17',
master_port=3306,
master_user="repluser",
master_password="centos",
MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=7950;
[root@slave2 ~]# mysql < /data/all.sql
[root@slave2 ~]# mysql -e "select user,password from mysql.user"
+----------+-------------------------------------------+
| user | password |
+----------+-------------------------------------------+
| root | |
| root | |
| root | |
| root | |
| | |
| | |
| repluser | *128977E278358FF80A246B5046F51043A2B1FCED |
| mhauser | *128977E278358FF80A246B5046F51043A2B1FCED |
+----------+-------------------------------------------+
[root@slave2 ~]# mysql -e "start slave"
[root@slave2 ~]# mysql -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.37.17
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 7950
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
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: 7950
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: 17
MHA的实现
安装MHA包
root@mha-manager test]# rz
#上传mha的manager包和node包
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring mha4mysql-manager-0.56-0.el6.noarch.rpm...
0% 0 KB 1007 bytes/sec 00:00:00 ETA 0 Error 1% 1 KB 1 KB/sec 00:01:24 ETA 0 Error 29% 25 KB 25 KB/sec 00:00:02 ETA 0 Error 62% 53 KB 53 KB/sec 00:00:00 ETA 0 Error 100% 85 KB 85 KB/sec 00:00:01 0 Errors
Transferring mha4mysql-node-0.56-0.el6.noarch.rpm...
0% 0 KB 993 bytes/sec 00:00:00 ETA 0 Error 54% 19 KB 19 KB/sec 00:00:00 ETA 0 Error 100% 35 KB 35 KB/sec 00:00:01 0 Errors
[root@mha-manager test]# ls
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
[root@mha-manager test]# yum install mha*.rpm
#mha manager全都要装,安装需要依赖epel源 直接用阿里的就好
Loaded plugins: fastestmirror, langpacks
Installed:
mha4mysql-manager.noarch 0:0.56-0.el6
mha4mysql-node.noarch 0:0.56-0.el6
Dependency Installed:
perl-Class-Load.noarch 0:0.20-3.el7
perl-Config-Tiny.noarch 0:2.14-7.el7
perl-Data-OptList.noarch 0:0.107-9.el7
perl-Email-Date-Format.noarch 0:1.002-15.el7
perl-IO-Socket-IP.noarch 0:0.21-5.el7
perl-IO-Socket-SSL.noarch 0:1.94-7.el7
perl-List-MoreUtils.x86_64 0:0.33-9.el7
perl-Log-Dispatch.noarch 0:2.41-1.el7.1
perl-MIME-Lite.noarch 0:3.030-1.el7
perl-MIME-Types.noarch 0:1.38-2.el7
perl-Mail-Sender.noarch 0:0.8.23-1.el7
perl-Mail-Sendmail.noarch 0:0.79-21.el7
perl-MailTools.noarch 0:2.12-2.el7
perl-Module-Implementation.noarch 0:0.06-6.el7
perl-Module-Runtime.noarch 0:0.013-4.el7
perl-Mozilla-CA.noarch 0:20130114-5.el7
perl-Net-LibIDN.x86_64 0:0.12-15.el7
perl-Net-SMTP-SSL.noarch 0:1.01-13.el7
perl-Net-SSLeay.x86_64 0:1.55-6.el7
perl-Package-DeprecationManager.noarch 0:0.13-7.el7
perl-Package-Stash.noarch 0:0.34-2.el7
perl-Package-Stash-XS.x86_64 0:0.26-3.el7
perl-Parallel-ForkManager.noarch 0:1.18-2.el7
perl-Params-Util.x86_64 0:1.07-6.el7
perl-Params-Validate.x86_64 0:1.08-4.el7
perl-Sub-Install.noarch 0:0.926-6.el7
perl-Sys-Syslog.x86_64 0:0.33-3.el7
perl-Time-HiRes.x86_64 4:1.9725-3.el7
perl-TimeDate.noarch 1:2.30-2.el7
perl-Try-Tiny.noarch 0:0.12-2.el7
Complete!
主,从复制的主,从节点安装node包
192.168.37.17主节点安装node包
[root@master test]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
Installed:
mha4mysql-node.noarch 0:0.56-0.el6
Complete!
#192.168.37.27从节点安装node包
[root@slave1 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
Installed:
mha4mysql-node.noarch 0:0.56-0.el6
Complete!
#192.168.37.37从节点安装node包 无依赖关系直接rpm安装就好
[root@slave2 data]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mha4mysql-node-0.56-0.el6 ################################# [100%]
SSH key验证生成所需key
#生成所需key可以在其任意一台主机生成,拷贝之所需节点就好
[root@mha-manager test]# 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:
16:46:e7:2e:41:00:51:17:c5:62:d1:6a:3d:f4:4e:9d root@mha-manager
The key's randomart image is:
+--[ RSA 2048]----+
| o+o.**o |
| +oo+ |
| .+=.. . . |
| .o+o o E |
| .S .+ |
| . . . |
| |
| |
| |
+-----------------+
[root@mha-manager test]# ls =a ~/.ssh/
ls: cannot access =a: No such file or directory
/root/.ssh/:
id_rsa id_rsa.pub
[root@mha-manager test]# ssh-copy-id 192.168.37.7
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.37.7's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.37.7'"
and check to make sure that only the key(s) you wanted were added.
[root@mha-manager ~]# cd .ssh/
[root@mha-manager .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
#拷贝到各节点目录
[root@mha-manager ~]# scp -r .ssh 192.168.37.17:/root/
The authenticity of host '192.168.37.17 (192.168.37.17)' can't be established.
ECDSA key fingerprint is 82:01:ab:34:04:e9:98:28:c2:10:0b:79:9a:60:19:06.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.37.17' (ECDSA) to the list of known hosts.
root@192.168.37.17's password:
id_rsa 100% 1679 1.6KB/s 00:00
id_rsa.pub 100% 398 0.4KB/s 00:00
known_hosts 100% 349 0.3KB/s 00:00
authorized_keys 100% 398 0.4KB/s 00:00
[root@mha-manager ~]# scp -r .ssh 192.168.37.27:/root/
The authenticity of host '192.168.37.27 (192.168.37.27)' can't be established.
ECDSA key fingerprint is 82:01:ab:34:04:e9:98:28:c2:10:0b:79:9a:60:19:06.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.37.27' (ECDSA) to the list of known hosts.
root@192.168.37.27's password:
id_rsa 100% 1679 1.6KB/s 00:00
id_rsa.pub 100% 398 0.4KB/s 00:00
known_hosts 100% 524 0.5KB/s 00:00
authorized_keys 100% 398 0.4KB/s 00:00
[root@mha-manager ~]# scp -r .ssh 192.168.37.37:/root/
The authenticity of host '192.168.37.37 (192.168.37.37)' can't be established.
ECDSA key fingerprint is 82:01:ab:34:04:e9:98:28:c2:10:0b:79:9a:60:19:06.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.37.37' (ECDSA) to the list of known hosts.
root@192.168.37.37's password:
id_rsa 100% 1679 1.6KB/s 00:00
id_rsa.pub 100% 398 0.4KB/s 00:00
known_hosts 100% 699 0.7KB/s 00:00
authorized_keys 100% 398 0.4KB/s 00:00
#基于key方式的登录测试
[root@master ~]# ssh 192.168.37.7
Last login: Mon Jul 6 16:28:08 2020 from 192.168.37.17
管理节点建立配置文件
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=192.168.37.17
candidate_master=1
[server2]
hostname=192.168.37.27
candidate_master=1
[server3]
hostname=192.168.37.37
#mha验证并启动
#1.sshkey验证通过
[root@mha-manager ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
Mon Jul 6 16:51:10 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Jul 6 16:51:10 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Mon Jul 6 16:51:10 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Mon Jul 6 16:51:10 2020 - [info] Starting SSH connection tests..
Mon Jul 6 16:51:12 2020 - [debug]
Mon Jul 6 16:51:10 2020 - [debug] Connecting via SSH from root@192.168.37.27(192.168.37.27:22) to root@192.168.37.17(192.168.37.17:22)..
Mon Jul 6 16:51:11 2020 - [debug] ok.
Mon Jul 6 16:51:11 2020 - [debug] Connecting via SSH from root@192.168.37.27(192.168.37.27:22) to root@192.168.37.37(192.168.37.37:22)..
Warning: Permanently added '192.168.37.37' (ECDSA) to the list of known hosts.
Mon Jul 6 16:51:12 2020 - [debug] ok.
Mon Jul 6 16:51:12 2020 - [debug]
Mon Jul 6 16:51:10 2020 - [debug] Connecting via SSH from root@192.168.37.17(192.168.37.17:22) to root@192.168.37.27(192.168.37.27:22)..
Warning: Permanently added '192.168.37.27' (ECDSA) to the list of known hosts.
Mon Jul 6 16:51:10 2020 - [debug] ok.
Mon Jul 6 16:51:10 2020 - [debug] Connecting via SSH from root@192.168.37.17(192.168.37.17:22) to root@192.168.37.37(192.168.37.37:22)..
Warning: Permanently added '192.168.37.37' (ECDSA) to the list of known hosts.
Mon Jul 6 16:51:12 2020 - [debug] ok.
Mon Jul 6 16:51:13 2020 - [debug]
Mon Jul 6 16:51:11 2020 - [debug] Connecting via SSH from root@192.168.37.37(192.168.37.37:22) to root@192.168.37.17(192.168.37.17:22)..
Mon Jul 6 16:51:12 2020 - [debug] ok.
Mon Jul 6 16:51:12 2020 - [debug] Connecting via SSH from root@192.168.37.37(192.168.37.37:22) to root@192.168.37.27(192.168.37.27:22)..
Mon Jul 6 16:51:12 2020 - [debug] ok.
Mon Jul 6 16:51:13 2020 - [info] All SSH connection tests passed successfully.
#2.检查主从复制是否有问题,是MHA到各从节点的连接情况
#数据库连接37.37有问题
[root@mha-manager ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
Tue Jul 7 10:21:18 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jul 7 10:21:18 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:21:18 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:21:18 2020 - [info] MHA::MasterMonitor version 0.56.
Tue Jul 7 10:21:18 2020 - [error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln301] Got MySQL error when connecting 192.168.37.37(192.168.37.37:3306) :1130:Host '192.168.37.7' is not allowed to connect to this MariaDB server, but this is not a MySQL crash. Check MySQL server settings.
at /usr/share/perl5/vendor_perl/MHA/ServerManager.pm line 297.
Tue Jul 7 10:21:19 2020 - [error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln309] Got fatal error, stopping operations
Tue Jul 7 10:21:19 2020 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln424] Error happened on checking configurations. at /usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm line 326.
Tue Jul 7 10:21:19 2020 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln523] Error happened on monitoring servers.
Tue Jul 7 10:21:19 2020 - [info] Got exit code 1 (Not master dead).
MySQL Replication Health is NOT OK!
#问题解决 37.37授权用户和密码 并执行flush privileges
[root@mha-manager ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
Tue Jul 7 10:23:33 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jul 7 10:23:33 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:23:33 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:23:33 2020 - [info] MHA::MasterMonitor version 0.56.
Tue Jul 7 10:23:34 2020 - [info] GTID failover mode = 0
Tue Jul 7 10:23:34 2020 - [info] Dead Servers:
Tue Jul 7 10:23:34 2020 - [info] Alive Servers:
Tue Jul 7 10:23:34 2020 - [info] 192.168.37.17(192.168.37.17:3306)
Tue Jul 7 10:23:34 2020 - [info] 192.168.37.27(192.168.37.27:3306)
Tue Jul 7 10:23:34 2020 - [info] 192.168.37.37(192.168.37.37:3306)
Tue Jul 7 10:23:34 2020 - [info] Alive Slaves:
Tue Jul 7 10:23:34 2020 - [info] 192.168.37.27(192.168.37.27:3306) Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Jul 7 10:23:34 2020 - [info] Replicating from 192.168.37.17(192.168.37.17:3306)
Tue Jul 7 10:23:34 2020 - [info] 192.168.37.37(192.168.37.37:3306) Version=5.5.65-MariaDB (oldest major version between slaves) log-bin:enabled
Tue Jul 7 10:23:34 2020 - [info] Replicating from 192.168.37.17(192.168.37.17:3306)
Tue Jul 7 10:23:34 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 7 10:23:34 2020 - [info] Current Alive Master: 192.168.37.17(192.168.37.17:3306)
Tue Jul 7 10:23:34 2020 - [info] Checking slave configurations..
Tue Jul 7 10:23:34 2020 - [warning] relay_log_purge=0 is not set on slave 192.168.37.27(192.168.37.27:3306).
Tue Jul 7 10:23:34 2020 - [warning] relay_log_purge=0 is not set on slave 192.168.37.37(192.168.37.37:3306).
Tue Jul 7 10:23:34 2020 - [info] Checking replication filtering settings..
Tue Jul 7 10:23:34 2020 - [info] binlog_do_db= , binlog_ignore_db=
Tue Jul 7 10:23:34 2020 - [info] Replication filtering check ok.
Tue Jul 7 10:23:34 2020 - [info] GTID (with auto-pos) is not supported
Tue Jul 7 10:23:34 2020 - [info] Starting SSH connection tests..
Tue Jul 7 10:23:37 2020 - [info] All SSH connection tests passed successfully.
Tue Jul 7 10:23:37 2020 - [info] Checking MHA Node version..
Tue Jul 7 10:23:39 2020 - [info] Version check ok.
Tue Jul 7 10:23:39 2020 - [info] Checking SSH publickey authentication settings on the current master..
Tue Jul 7 10:23:40 2020 - [info] HealthCheck: SSH to 192.168.37.17 is reachable.
Tue Jul 7 10:23:41 2020 - [info] Master MHA Node version is 0.56.
Tue Jul 7 10:23:41 2020 - [info] Checking recovery script configurations on 192.168.37.17(192.168.37.17:3306)..
Tue Jul 7 10:23:41 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.000001
Tue Jul 7 10:23:41 2020 - [info] Connecting to root@192.168.37.17(192.168.37.17:22)..
Creating /data/mastermha/app1 if not exists.. Creating directory /data/mastermha/app1.. done.
ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to mariadb-bin.000001
Tue Jul 7 10:23:41 2020 - [info] Binlog setting check done.
Tue Jul 7 10:23:41 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Tue Jul 7 10:23:41 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=192.168.37.27 --slave_ip=192.168.37.27 --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 Jul 7 10:23:41 2020 - [info] Connecting to root@192.168.37.27(192.168.37.27:22)..
Creating directory /data/mastermha/app1/.. done.
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.000002
Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Tue Jul 7 10:23:42 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mhauser' --slave_host=192.168.37.37 --slave_ip=192.168.37.37 --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 Jul 7 10:23:42 2020 - [info] Connecting to root@192.168.37.37(192.168.37.37:22)..
Creating directory /data/mastermha/app1/.. done.
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.000002
Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Tue Jul 7 10:23:43 2020 - [info] Slaves settings check done.
Tue Jul 7 10:23:43 2020 - [info]
192.168.37.17(192.168.37.17:3306) (current master)
+--192.168.37.27(192.168.37.27:3306)
+--192.168.37.37(192.168.37.37:3306)
Tue Jul 7 10:23:43 2020 - [info] Checking replication health on 192.168.37.27..
Tue Jul 7 10:23:43 2020 - [info] ok.
Tue Jul 7 10:23:43 2020 - [info] Checking replication health on 192.168.37.37..
Tue Jul 7 10:23:43 2020 - [info] ok.
Tue Jul 7 10:23:43 2020 - [warning] master_ip_failover_script is not defined.
Tue Jul 7 10:23:43 2020 - [warning] shutdown_script is not defined.
Tue Jul 7 10:23:43 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
#启动MHA 前台执行
[root@mha-manager ~]# masterha_manager --conf=/etc/mha/app1.cnf
Tue Jul 7 10:29:19 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jul 7 10:29:19 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:29:19 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
测试MHA可用
#断开主节点服务器,模拟主服务器宕机
[root@master ~]# ifconfig eth0 down
#查看mhamanager前台进程是否停止
[root@mha-manager ~]# masterha_manager --conf=/etc/mha/app1.cnf
Tue Jul 7 10:29:19 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jul 7 10:29:19 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:29:19 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:39:58 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jul 7 10:39:58 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jul 7 10:39:58 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
#检查日志发现37.37变成了新的主节点
#less /data/mastermha/app1/manager.log
Master 192.168.37.17(192.168.37.17:3306) is down!
Check MHA Manager logs at mha-manager:/data/mastermha/app1/manager.log for details.
Started automated(non-interactive) failover.
The latest slave 192.168.37.27(192.168.37.27:3306) has all relay logs for recovery.
Selected 192.168.37.37(192.168.37.37:3306) as a new master.
192.168.37.37(192.168.37.37:3306): OK: Applying all logs succeeded.
192.168.37.27(192.168.37.27:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.37.27(192.168.37.27:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.37.37(192.168.37.37:3306)
192.168.37.37(192.168.37.37:3306): Resetting slave info succeeded.
Master failover to 192.168.37.37(192.168.37.37:3306) completed successfully.
#主从复制测试
#192.168.37.37
[root@slave2 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 55
Server version: 5.5.65-MariaDB 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 slave status\G
Empty set (0.00 sec)
#已经没有复制信息了,变成主节点了
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.02 sec)
MariaDB [(none)]> create database testmha;
Query OK, 1 row affected (0.02 sec)
#192.168.37.27已经实现复制
#主节点已经变成37.37
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.37.37
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 522482
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 620
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: 522482
Relay_Log_Space: 916
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: 37
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
| testmha |
+--------------------+
6 rows in set (0.03 sec)