1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。
[root@mariadb ~]# cat full-backup.sh
#!/bin/bash
PS3="please select your backup type:";
select type in mysqldump xtraback;do
case $REPLY in
1)
[ -e /backup ] || mkdir /backup
mysqldump -A --single-transaction -F |gzip > /backup/full-backup-$(date '+%Y%m%d-%H%M').sql.gz || (echo 'backup failed!' && exit 2 )
echo 'mysqldump full-backup successfully !'
break
;;
2)
xtrabackup --backup --target-dir=/backup/full-backup-$(date '+%Y%m%d-%H%M') &> /dev/null || (echo 'backup failed!' && exit 2 )
echo 'xtrabackup full-backup successfully !'
break
;;
*)
echo "please input number for your backup type "
esac
done
实验
[root@mariadb ~]# ./full-backup.sh
1) mysqldump
2) xtraback
please select your backup type:1
mysqldump full-backup successfully !
[root@mariadb ~]# ll /backup/
total 348
-rw-r--r--. 1 root root 355755 Jun 7 23:10 full-backup-20200607-2310.sql.gz
2、配置Mysql主从同步
实验环境
* 系统环境:CentOS7.2
* 两个节点:
* 主节点mariadb IP地址:192.168.0.102
* 从节点mariadb1 IP地址:192.168.0.112
----
主节点目前数据库情况
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
从节点为新安装的数据库服务
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
1、同步时间(ntp, chrony)
作为主从复制,时间同步是最基本的设置!最基本的满足条件!
Note: 笔记:网络服务 -ntp VS chrony-时间同步
查看时间是是否同步
[root@mariadb ~]# date "+%F %T"
2020-06-03 21:45:23
[root@mariadb1 mysql]# date "+%F %T"
2020-06-03 21:45:25
2、配置主节点
#启用二进制日志,需要满足两点:1、变量sql_log_bin的值 需要为on ;2、在配置文件中配置log_bin,指定二进制日志路径(注意路径权限问题)
MariaDB [mysql]> show variables like 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | ON |
+---------------+-------+
1 row in set (0.001 sec)
------------------------------------
修改配置文件,确保有如下几项
[root@mariadb ~]# vim /etc/my.cnf
[mysqld]
skip-name-resolve=on #可选-跳过名称解析,确保验证时不会基于主机名称来解析
log_bin=/data/mysql-log/mysql-bin.log #启用二进制日志
server_id=1 #节点的唯一标识,必须保持唯一性
binlog_format=ROW #其他格式可能会造成主从数据不一致的情况
...
[root@mariadb ~]# systemctl restart mysql
[root@mariadb ~]# systemctl status mysql
#创建一个拥有复制权限的用户帐号
MariaDB [mysql]> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.002 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.004 sec)
3、备份主节点数据库
# 主从复制,--master-data的值需要设置为1
[root@mariadb ~]# mysqldump -A --single-transaction --master-data=1 -F > /tmp/mysql-full.sql
4、复制该备份文件到从节点
[root@mariadb ~]# scp /tmp/mysql-full.sql 192.168.0.112:/tmp/
[email protected]'s password:
mysql-full.sql 100% 1429KB 1.4MB/s 00:00
5、配置从节点 /etc/my.cnf
#修改配置文件,新增如下几项
[root@mariadb1 ~]# vim /etc/my.cnf
[mysqld]
skip-name-resolve=on #可选-跳过名称解析,确保验证时不会基于主机名称来解析
server_id=2 #节点的唯一标识,必须保持唯一性
read_only=ON #可选-设置数据库只读(此限制对拥有SUPER权限的用户均无效)
relay_log=relay-log #启动中继日志
#重启服务
[root@mariadb1 ~]# systemctl restart mysql
[root@mariadb1 ~]# systemctl status mysql
● mysql.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-06-03 22:37:10 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
6、修改复制到从节点的 备份文件,配置主节点连接信息
[root@ansible-manager ~]# vim /tmp/mysql-full.sql
...
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=385;
#修改为-->
CHANGE MASTER TO
MASTER_HOST='192.168.0.102',
MASTER_USER='repluser',
MASTER_PASSWORD='replpass',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000008',
MASTER_LOG_POS=385;
...
7、在从节点上还原备份
MariaDB [mysql]> source /tmp/mysql-full.sql;
8、启动从节点 I/O 线程和SQL线程
MariaDB [mysql]> start slave;
Query OK, 0 rows affected (0.004 sec)
9、查看启动后从节点状态
#查看启动后 I/O 线程和SQL线程 是否已存在
MariaDB [hellodb]> show processlist;
+----+-------------+-----------+---------+-----------+------+-----------------------------------------------------------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+-------------+-----------+---------+-----------+------+-----------------------------------------------------------------------------+------------------+----------+
...
| 9 | system user | | NULL | Slave_IO | 1287 | Waiting for master to send event | NULL | 0.000 |
| 10 | system user | | NULL | Slave_SQL | 696 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0.000 |
+----+-------------+-----------+---------+-----------+------+-----------------------------------------------------------------------------+------------------+----------+
8 rows in set (0.001 sec)
MariaDB [mysql]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.102
Master_User: repluser
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 385
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes #两个线程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: 653
Relay_Log_Space: 858
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 #和主节点同步延迟时间(s)
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
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
10、测试
#删除主节点hellodb.coc表
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| toc |
+-------------------+
6 rows in set (0.000 sec)
MariaDB [hellodb]> drop table coc;
Query OK, 0 rows affected (0.007 sec)
#查看从节点。已同步更新
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| courses |
| scores |
| students |
| toc |
+-------------------+
5 rows in set (0.000 sec)
3、使用MHA实现Mysql高可用。
1、环境;MHA对MySQL复制环境有特殊要求, 例如各节点都要开启二进制日志及中继日志, 各从节点必须显示启用其read-only属性, 并关闭relay_log_purge功能等, 这里对配置做事先说明。
master:172.16.253.35
slave1:172.16.253.53
slave2:172.16.253.52
manager:172.16.253.51
2、master的配置文件;
[root@master ~]# vim /etc/my.cnf
[mysqld]
server-id = 1
log-bin = master-log
relay-log = relay-log
skip_name_resolve = ON
然后启动mariadb服务;
[root@master ~]# systemctl start mariadb
3、所有的从节点的配置文件;
[root@slave1 ~]# vim /etc/my.cnf
[mysqld]
server-id = 2 #复制集群中的各节点的id均必须唯一;
relay-log = relay-log
log-bin = master-log
read_only = 1
relay_log_purge = 0 #是否自动清空不再需要中继日志
skip_name_resolve = ON
然后启动所有从服务器上的mariadb服务;
[root@slave1 ~]# systemctl start mariadb
4、在master节点上授权;
MariaDB [(none)]> grant replication slave,replication client on *.* to 'slave'@'172.16.253.%' identified by 'centos';
MariaDB [(none)]> flush privileges; #刷新权限;
MariaDB [(none)]> show master status; #查看master现在的状态,显示的file名字及position位置编号;
5、启动从服务器上的I/O线程和SQL线程;
MariaDB [(none)]> change master to
-> master_host='172.16.253.35',
-> master_user='slave',
-> master_password='centos',
-> master_log_file='master-log.000001', #第四步中查看到的master的file文件名
-> master_log_pos=493; #第四步中查看到的master的position位置号
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G; #可查看到如下关键两行,只有全为Yes才表示同步成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
6、安装配置MHA;
1)、在master上执行授权,允许所有其他节点远程访问;
MariaDB [(none)]> grant all on *.* to 'mhaadmin'@'172.16.253.%' identified by 'mhapass';
2)、使所有节点可以进行无密码互相通信,所有节点都要做的操作,例如manager节点上生成秘钥对,分别发给其他节点;
[root@manager ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #此处实验没有设置密码;
[root@manager ~]# ssh-copy-id [email protected] #分别发给35、53、52三个节点;
3)、yum安装MHA;
准备两个安装包:
mha4mysql-node-0.56-0.el6.noarch.rpm
mha4mysql-manager-0.56-0.el6.noarch.rpm
[root@master ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y #所有节点上都执行;
[root@manager ~]# yum install mha4mysql-manager-0.56-0.el6.noarch.rpm -y #只在manager上安装;
注意:在manager上要先安装mha4mysql-node再安装manager,因为有些安装manager时所依赖的包;
4)、关于配置文件:
Manager节点需要为每个监控的master/slave集群提供一个专用的配置文件,而所有的master/slave集群也可共享全局配置。全局配置文件默认为/etc/masterha_default.cnf,其为可选配置。如果仅监控一组master/slave集群,也可直接通过application的配置来提供各服务器的默认配置信息。而每个application的配置文件路径为自定义。
5)、定义配置文件;
为MHA专门创建一个管理用户,方便以后使用,在mysql的主节点上配置,三个节点自动同步;
[root@manager~]# mkdir /etc/mha_master
[root@manager ~]# vim /etc/mha_master/sjj.cnf
[server default]
user=mhaadmin #MHA管理用户;
password=mhapass #MHA管理密码;
manager_workdir=/etc/mha_master/sjj #mha_master自己的工作路径,需要手动创建;
manager_log=/etc/mha_master/manager.log #mha_master自己的日志文件;
remote_workdir=/mydata/mha_master/sjj #每个远程主机的工作目录,自己手动创建;
ssh_user=root #基于ssh秘钥认证用户;
repl_user=slave #数据库用户名;
repl_password=centos #数据库密码;
ping_interval=1 #ping间隔时长;
[server1] #节点1;
hostname=172.16.253.35 #节点1主机ip;
ssh_port=22 #节点1 的ssh端口;
candidate_master=1 #将来可不可以作为master候选主节点;
[server2]
hostname=172.16.253.53
ssh_port=22
candidate_master=1
[server3]
hostname=172.16.253.52
ssh_port=22
candidate_master=1
6)、检测各节点ssh互相通信配置是否OK;
[root@manager ~]# masterha_check_ssh -conf=/etc/mha_master/sjj.cnf
测试结果显示[info] All SSH connection tests passed successfully.表示测试成功
7)、检查管理的MYSQL复制集群的连接配置参数是否OK;
[root@manager ~]# masterha_check_repl -conf=/etc/mha_master/sjj.cnf
最后显示结果:MySQL Replication Health is OK. 表示成功;
7、启动MHA;
在manager节点上执行:
[root@manager ~]# nohup masterha_manager -conf=/etc/mha_master/sjj.cnf &> /etc/mha_master/manager.log &
其中“&> &”是将输出结果全部重定向到/etc/mha_master/manager.log,也可以使用“2>&1”;
[root@manager ~]# masterha_check_status -conf=/etc/mha_master/sjj.cnf #查看master的状态;
若是需要停止MHA,需要使用master_stop命令;
[root@manager ~]# masterha_stop -conf=/etc/mha_master/sjj.cnf
[root@manager ~]# masterha_check_status -conf=/etc/mha_master/sjj.cnf #可查看到停止状态
测试:
1、测试MHA故障转移;
1)、在master节点关闭mariadb服务,模拟主节点数据崩溃;
[root@master ~]# killall -9 mysqld mysqld_safe
[root@master ~]# rm -rf /var/lib/mysql/*
2)、在manager节点查看日志;
[root@manager ~]# tail -200 /etc/mha_master/manager.log
结果显示如下:
sjj: MySQL Master failover 172.16.253.35(172.16.253.35:3306) to 172.16.253.53(172.16.253.53:3306) succeeded
Master 172.16.253.35(172.16.253.35:3306) is down!
Check MHA Manager logs at manager:/etc/mha_master/manager.log for details.
Started automated(non-interactive) failover.
The latest slave 172.16.253.53(172.16.253.53:3306) has all relay logs for recovery.
Selected 172.16.253.53(172.16.253.53:3306) as a new master.
172.16.253.53(172.16.253.53:3306): OK: Applying all logs succeeded.
172.16.253.52(172.16.253.52:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
172.16.253.52(172.16.253.52:3306): OK: Applying all logs succeeded. Slave started, replicating from 172.16.253.53(172.16.253.53:3306)
172.16.253.53(172.16.253.53:3306): Resetting slave info succeeded.
Master failover to 172.16.253.53(172.16.253.53:3306) completed successfully.
master节点172.16.253.35 down,提升具有最新数据的slave节点172.16.253.53做新的主节点;
借鉴文章:https://blog.csdn.net/OH_ON/article/details/78820183