# 安装 mariadb
yum install -y mariadb-server
# 设置开机启动并立即启动
systemctl enable --now mariadb
# 导入备份文件
mysql < hellodb_innodb.sql
# 下载 xtrabackup 安装包
wget https://www.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.20/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.20-1.el7.x86_64.rpm
# 安装 xtrabackup
yum install -y percona-xtrabackup-24-2.4.20-1.el7.x86_64.rpm
# 创建备份文件
mkdir /backup
创建脚本文件:
vim auto_backup_mariadb.sh
脚本内容:
#!/bin/bash
dir=/backup/hellodb_`date "+%F_%T"`
echo $dir
baktype='None'
echo -e "请选择备份方式:\n 1.mysqldump\n 2.xtrabackup\n 3.退出"
while :
do
read -p "请输入(1,2,3):" backtype
case $backtype in
1)
mysqldump -F -x -A --master-data=2 > $dir.sql && echo "备份成功!备份文件:$dir.sql"
break
;;
2)
xtrabackup --backup --target-dir=$dir && echo "备份成功!备份目录:$dir"
break
;;
3)
echo Byebye!
break
;;
*)
echo "输入错误,请重新输入!"
continue
;;
esac
done
[root@localhost ~]# bash auto_backup_mariadb.sh
/backup/hellodb_2020-08-05_17:14:35
请选择备份方式:
1.mysqldump
2.xtrabackup
3.退出
请输入(1,2,3):1
备份成功!备份文件:/backup/hellodb_2020-08-05_17:14:35.sql
[root@localhost ~]# bash auto_backup_mariadb.sh
/backup/hellodb_2020-08-05_17:14:37
请选择备份方式:
1.mysqldump
2.xtrabackup
3.退出
请输入(1,2,3):2
备份成功!备份目录:/backup/hellodb_2020-08-05_17:14:37
[root@localhost ~]# ll /backup/
total 512
-rw-r--r-- 1 root root 521630 Aug 5 17:14 hellodb_2020-08-05_17:14:35.sql
drwxr-x--- 6 root root 217 Aug 5 17:14 hellodb_2020-08-05_17:14:37
[root@localhost ~]# ll /backup/hellodb_2020-08-05_17\:14\:37
total 18460
-rw-r----- 1 root root 431 Aug 5 17:14 backup-my.cnf
drwxr-x--- 2 root root 146 Aug 5 17:14 hellodb
-rw-r----- 1 root root 18874368 Aug 5 17:14 ibdata1
drwxr-x--- 2 root root 4096 Aug 5 17:14 mysql
drwxr-x--- 2 root root 4096 Aug 5 17:14 performance_schema
drwxr-x--- 2 root root 20 Aug 5 17:14 test
-rw-r----- 1 root root 23 Aug 5 17:14 xtrabackup_binlog_info
-rw-r----- 1 root root 135 Aug 5 17:14 xtrabackup_checkpoints
-rw-r----- 1 root root 492 Aug 5 17:14 xtrabackup_info
-rw-r----- 1 root root 2560 Aug 5 17:14 xtrabackup_logfile
# IP:10.10.10.71
# 安装
yum install -y mariadb-server
# 备份配置文件
cp /etc/my.cnf{,.bak}
# 修改配置,添加 server-id ,开启 log-bin
sed -ri '/\[mysqld\]/aserver-id=1' /etc/my.cnf
sed -ri '/\[mysqld\]/alog-bin' /etc/my.cnf
# 启动服务(同时设置开机启动)
systemctl enable --now mariadb
# 创建授权用户
grant replication slave on *.* to repluser@'10.10.10.72' identified by 'mysql';
# IP:10.10.10.72
# 安装
yum install -y mariadb-server
# 备份配置文件
cp /etc/my.cnf{,.bak}
# 修改配置,添加 server-id ,开启 log-bin
sed -ri '/\[mysqld\]/aserver-id=2' /etc/my.cnf
# 启动服务(同时设置开机启动)
systemctl enable --now mariadb
# 修改主服务器
mysql
MariaDB [(none)]> CHANGE MASTER TO
MASTER_HOST='10.10.10.71',
MASTER_USER='repluser',
MASTER_PASSWORD='mysql',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000002',
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: 10.10.10.71
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: 1
1 row in set (0.00 sec)
# 在主服务器创建数据库
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
# 在从服务器查看数据库,同步成功
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
MASTER_LOG_FILE='mariadb-bin.000002',
MASTER_LOG_POS=245;
环境(4)台服务器(登录帐号均为root)
mha 安装包
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
master 安装配置
# 设置主机名
hostnamectl set-hostname master
# 安装 mariadb
yum install -y mariadb-server
# 修改配置文件
vim /etc/my.cnf
## 在 [mysqld]后面添加以下内容
server-id=72
log-bin
skip-name-resolve
# 启动服务
systemctl enable --now mariadb-service
# 创建帐号
mysql
## 执行以下命令
grant replication slave on *.* to repluser@'10.10.10.%' identified by 'password-str';
grant all on *.* to mhauser@'10.10.10.%' identified by 'password-str';
# 安装 mha node 软件包
yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
# 设置主机名
hostnamectl set-hostname slave1
# 安装 mariadb
yum install -y mariadb-server
# 修改配置文件
vim /etc/my.cnf
## 在 [mysqld]后面添加以下内容
server-id=73
log-bin
read-only
skip-name-resolve
relay-log-purge=0
# 启动服务
systemctl enable --now mariadb
# 配置主从复制
mysql
## 执行以下命令
CHANGE MASTER TO
MASTER_HOST='10.10.10.72',
MASTER_USER='repluser',
MASTER_PASSWORD='password-str',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000001',
MASTER_LOG_POS=245;
start slave;
# 安装 mha node 软件包
yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
# 设置主机名
hostnamectl set-hostname slave2
# 安装 mariadb
yum install -y mariadb-server
# 修改配置文件
vim /etc/my.cnf
## 在 [mysqld]后面添加以下内容
server-id=74
log-bin
read-only
skip-name-resolve
relay-log-purge=0
# 启动服务
systemctl enable --now mariadb
# 配置主从复制
mysql
## 执行以下命令
CHANGE MASTER TO
MASTER_HOST='10.10.10.72',
MASTER_USER='repluser',
MASTER_PASSWORD='password-str',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000001',
MASTER_LOG_POS=245;
start slave;
# 安装 mha node 软件包
yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
hostnamectl set-hostname mha-manager
# 基于key验证(可以在任意一台操作,在 mha 服务启动前配置好)
## 生成公钥和密钥(一直按回车)
ssh-keygen
## 上传到本机(输入yes和当前帐号密码)
ssh-copy-id 10.10.10.71
## 复制.ssh目录到其他服务器
scp -r .ssh 10.10.10.72:/root/
scp -r .ssh 10.10.10.73:/root/
scp -r .ssh 10.10.10.74:/root/
# 安装 mha 软件(需启用 epel 源)
yum install -y mha*
# 创建 mha 配置文件
mkdir /etc/mha
vim /etc/mha/app1.cnf
## 添加以下内容
[server default]
user=mhauser
password=password-str
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=password-str
ping_interval=1
[server1]
hostname=10.10.10.72
candidate_master=1
[server2]
hostname=10.10.10.73
candidate_master=1
[server3]
hostname=10.10.10.74
# 测试
masterha_check_ssh --conf=/etc/mha/app1.cnf
masterha_check_repl --conf=/etc/mha/app1.cnf
# 启动
masterha_manager --conf=/etc/mha/app1.cnf
# 断开 master (10.10.10.72) 的网络
# mha 自动切换
# 在 slave2 查看 slave 状态可以看到服务器是 10.10.10.73,说明切换成功
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.10.73
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 245
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: 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: 73
1 row in set (0.00 sec)