文章目录
- MySQL备份工具之xtrabackup
- 一、xtrabackup的介绍
- 1、xtrabackup 版本兼容性
- 2、Xtrabackup优点
- 3、Xtrabackup备份原理
- 二、安装mysql5.7.x
- 1、yum方式安装mysql5.7.x的方式
- 2、下载 xtrabackup2.4 版本
- 3、xtrabackup2.4备份mysql5.7.x数据
- 3.1、innobackupex全备
- 3.2、模拟数据丢失,然后使用全备恢复
- 3.3、innobackupex 增量备份
- 3.4、模拟误操作,删除数据库:test和student,然后再使用增量进行恢复
- 4、二进制安装mysql8.0.x的方式
- 5、下载xtrabackup8.0版本
- 5.1、CentOS7安装xtrabackup8.0
- 5.2、CentOS8安装xtrabackup8.0(这里用CentOS8系统来演示)
- 5.3、xtrabackup8.0全量备份mysql8.0.28, 以及恢复。
- 5.4、xtrabackup8.0增量备份以及恢复
- 报错信息:
MySQL备份工具之xtrabackup
一、xtrabackup的介绍
xtrabackup官网
Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品。
简介说明:
Xtrabackup2.4有两个主要的工具:xtrabackup、innobackupex
(1)xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表
(2)innobackupex则封装了xtrabackup,是一个脚本封装,所以能同时备份处理innodb和myisam,但在处理myisam时需要加一个读锁(推荐)
xtrabackup 常用工具说明
xtrabackup 中主要包含两个工具
xtrabackup :是用于热备 innodb , xtradb 表中数据的工具,不能备份其他类型的表,也不能备份数据表结构 。
innobackupex :是将 xtrabackup 进行封装的 perl 脚本,提供了备份 myisam 表的能力。
常用选项:
--defaults-file=:指定my.cnf配置文件路径,如/etc/my.cnf。该选项必须是第一个选项,它会根据配置文件查找数据路径
--user=:执行备份操作的MySQL用户(非系统用户),该用户需要有MySQL的相关权限
--password=:MySQL用户的密码
--databases:指定要备份的数据库,多个数据库以空格隔开,如"db1 db2",在指定某数据库时也可以只指定其中的某张表(MySQL需要配置innodb_file_per_table = 1)
--incremental:增量备份
--incremental-basedir:指定增量备份所依赖的上一次备份
--use-memory:指定恢复数据时使用的内存大小
--port 指定端口
--apply-log 对备份进行预处理操作
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
--redo-only 不回滚未提交事务。在xtrabackup8.0里是--apply-log-only
--copy-back 恢复备份目录
1、xtrabackup 版本兼容性
Percona -xtraBackup 8.0 是 Percona XtraBackup 新推出了一个针对 MySQL8.0 的版本,主要是 MySQL8.0 在 Redo 和 数据库字典方面有了新的改进。
XtraBackup 2.4 继续支持 MySQL 5.6 和 5.7
(1) xtrabackup 8.0 移除了innobackupex 命令;
(2) 由于新的MySQL 重做日志和数据字典格式, 8.0 版本只支持 mysql8.0 和 percona8.0 ;
(3) 早于mysql8.0 的版本需要使用 xtrabackup2.4 备份和恢复 .
MySQLdump
和xtrabackup
备份对比
Xtrabackup是由percona开源的免费数据库热备份软件,它能对InnoDB数据库和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加表锁);
mysqldump备份方式是采用的逻辑备份,其最大的缺陷是备份和恢复速度较慢,如果数据大于50G,mysqldump备份就不太适合。 (说白了就是如果数据量很大就用xtrabackup来备份数据)
2、Xtrabackup优点
1)备份速度快,物理备份可靠
2)备份过程不会打断正在执行的事务(无需锁表)
3)能够基于压缩等功能节约磁盘空间和流量
4)自动备份校验
5)还原速度快
6)可以流传将备份传输到另外一台机器上
7)在不增加服务器负载的情况备份数据
3、Xtrabackup备份原理
备份开始时首先会开启一个后台检测进程,实时检测mysql redo
的变化,一旦发现有新的日志写入,立刻将日志记入后台日志文件xtrabackup_log
中,之后复制innodb的数据文件一系统表空间文件ibdatax
,复制结束后,将执行 flush tables with readlock
,然后复制 .frm MYI MYD
等文件,最后执行unlock tables
,最终停止xtrabackup_log
。
使用xtrabackup2.4备份mysql5.7.x,xtrabackup8.0备份mysql8.0.x
环境准备
操作系统 | IP | 应用 |
---|---|---|
CentOS7/8 | 192.168.229.10 | mysql5.7.x xtrabackup2.4 |
CentOS7/8 | 192.168.229.170 | mysql8.0.x xtrabackup8.0 |
两台都要关闭防火墙跟SElinux
关闭防火墙
关闭SElinux
二、安装mysql5.7.x
安装mysql有两种方式,二进制安装和yum安装。
1、yum方式安装mysql5.7.x的方式
下载mysql的rpm路径
防火墙和selinux都已关闭
// 在 安装MySQL 之前需要查看系统有没有安装mariadb 这个包
[root@node1 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64 // 卸载它,不然使用yum 安装MySQL的时候会报错
[root@node1 ~]#
// 卸载 mariadb-libs
[root@node1 ~]# yum -y remove mariadb*
...省略N
# 下载/mysql-community-client-5.7.9-1.el7.x86_64.rpm
[root@localhost ~]# cat /etc/redhat-release
CentOS Stream release 8
[root@localhost ~]#
[root@localhost ~]# cd /opt/
[root@localhost opt]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.9-1.el7.x86_64.rpm
………………省略部分输出信息………………
# 下载/mysql-community-client-5.7.9-1.el7.x86_64.rpm
[root@localhost opt]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.9-1.el7.x86_64.rpm
………………省略部分输出信息………………
# 下载/mysql-community-client-5.7.9-1.el7.x86_64.rpm
[root@localhost opt]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.9-1.el7.x86_64.rpm
………………省略部分输出信息………………
# 下载/mysql-community-client-5.7.9-1.el7.x86_64.rpm
[root@localhost opt]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.9-1.el7.x86_64.rpm
………………省略部分输出信息………………
# 下载/mysql-community-client-5.7.9-1.el7.x86_64.rpm
[root@localhost opt]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.9-1.el7.x86_64.rpm
………………省略部分输出信息………………
[root@localhost opt]# ll
total 175932
-rw-r--r--. 1 root root 25591100 Nov 12 2015 mysql-community-client-5.7.9-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 275276 Nov 12 2015 mysql-community-common-5.7.9-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 3889700 Nov 12 2015 mysql-community-devel-5.7.9-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 2301852 Nov 12 2015 mysql-community-libs-5.7.9-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 148087384 Nov 12 2015 mysql-community-server-5.7.9-1.el7.x86_64.rpm
[root@localhost opt]#
[root@localhost opt]# pwd
/opt
[root@localhost opt]# dnf -y install mysql-community-* # 安装mysql
···省略N
[root@localhost opt]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
[root@localhost opt]# systemctl enable --now mysqld
[root@localhost opt]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2023-05-08 10:10:42 CST; 2s ago
Process: 15795 ExecStart=/usr/sbin/mysqld --daemonize $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 15722 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 15797 (mysqld)
Tasks: 27 (limit: 17399)
Memory: 301.6M
CGroup: /system.slice/mysqld.service
└─15797 /usr/sbin/mysqld --daemonize
May 08 10:10:38 localhost.localdomain systemd[1]: Starting MySQL Server...
May 08 10:10:42 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost opt]# cat /var/log/mysqld.log | grep root
2023-05-08T02:10:39.233385Z 1 [Note] A temporary password is generated for root@localhost: ;dW,aj%&E9ha # 临时密码:;dW,aj%&E9ha
# 使用临时密码登录,然后修改临时密码
[root@localhost opt]# mysql -uroot -p';dW,aj%&E9ha'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.9
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 执行修改密码语句
mysql> alter user 'root'@'localhost' identified by 'Agan@3306';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; # 刷新授权表
Query OK, 0 rows affected (0.01 sec)
mysql> exit; # 退出
图片说明:
2、下载 xtrabackup2.4 版本
或者你可以复制 网址下载到本地
# 使用wget 下载xtrabackup2.4版本的
[root@localhost ~]# cd /opt/
[root@localhost opt]# wget $ wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/\
> binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
···下载进度省略N
[root@localhost opt]# ll
total 218472
-rw-r--r-- 1 root root 26670472 May 9 09:49 mysql-community-client-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 318884 May 9 09:49 mysql-community-common-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 4122424 May 9 09:49 mysql-community-devel-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 2474780 May 9 09:49 mysql-community-libs-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 182274360 May 9 09:49 mysql-community-server-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 7839980 Jul 21 2016 percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
[root@localhost opt]#
[root@localhost opt]# yum -y install percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
···安装过程省略N
[root@localhost opt]# rpm -qa | grep percona-xtrabackup
percona-xtrabackup-24-2.4.4-1.el7.x86_64
[root@localhost opt]#
[root@localhost ~]# mysql -u root -p'Agan@3306'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 查看数据库的存储引擎,必须为InnoDB类型,因为xtrabackup只能备份InnoDB和xtradb表
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql>
mysql> show databases; # 查看当前有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
mysql> select version(); # 查看当前数据库版本
+-----------+
| version() |
+-----------+
| 5.7.37 |
+-----------+
1 row in set (0.00 sec)
mysql> CREATE DATABASE agan; # 创建一个数据库名为:agan
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan | # 这是刚创建好的数据库:agan
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
mysql> USE agan; # 进入 agan 数据库
Database changed
mysql> SELECT DATABASE(); # 查看当前在哪个数据库里面
+------------+
| DATABASE() |
+------------+
| agan |
+------------+
1 row in set (0.00 sec)
# 创建表名为:t_test
mysql> CREATE TABLE t_test(
-> id int(11) not null primary key auto_increment,
-> name varchar(100) not null,
-> age char(8)
-> )charset utf8mb4;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW TABLES; # 查看当前有哪些表
+----------------+
| Tables_in_agan |
+----------------+
| t_test |
+----------------+
1 row in set (0.00 sec)
mysql>
mysql> DESC t_test; # 查看表结构
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | char(8) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
# 插入数据
mysql> insert t_test(name,age) values('阿伟',20),('周扒皮',25),('爱博',23),('小玉',19);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from t_test; # 查询 t_test 表的所有字符
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 阿伟 | 20 |
| 2 | 周扒皮 | 25 |
| 3 | 爱博 | 23 |
| 4 | 小玉 | 19 |
+----+-----------+------+
4 rows in set (0.00 sec)
mysql>
3、xtrabackup2.4备份mysql5.7.x数据
3.1、innobackupex全备
xtrabackup官方文档
[root@localhost ~]# tail -10 /etc/my.cnf // 查看my.cnf 后十行内容
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql // 数据存放的位置
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost ~]#
[root@localhost ~]# mkdir /data # 创建数据存放路径
[root@localhost ~]# which xtrabackup
/usr/bin/xtrabackup
[root@localhost ~]# which innobackupex
/usr/bin/innobackupex
[root@localhost ~]#
# 全备
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 /data
····备份过程省略N
[root@localhost ~]# ll /data/
total 0
drwxr-x--- 6 root root 205 May 10 11:09 2023-05-10_11-09-34 // 全备的数据
[root@localhost ~]# ll /data/2023-05-10_11-09-34/
total 12336
drwxr-x--- 2 root root 56 May 10 11:09 agan
-rw-r----- 1 root root 425 May 10 11:09 backup-my.cnf
-rw-r----- 1 root root 372 May 10 11:09 ib_buffer_pool
-rw-r----- 1 root root 12582912 May 10 11:09 ibdata1
drwxr-x--- 2 root root 4096 May 10 11:09 mysql
drwxr-x--- 2 root root 8192 May 10 11:09 performance_schema
drwxr-x--- 2 root root 8192 May 10 11:09 sys
-rw-r----- 1 root root 113 May 10 11:09 xtrabackup_checkpoints
-rw-r----- 1 root root 442 May 10 11:09 xtrabackup_info
-rw-r----- 1 root root 2560 May 10 11:09 xtrabackup_logfile
[root@localhost ~]#
3.2、模拟数据丢失,然后使用全备恢复
- 停止数据库服务 systemctl stop mysqld
- 要删除数据文件和日志文件(也可以mv移到别的地方,只要确保清空mysql数据存放目录就行)
- 注意:恢复前需先执行innobackupex --apply-log,然后再使 --copy-back 数据保持一致。
- 再启动数据库服务
[root@localhost ~]# ll /data/
total 0
drwxr-x--- 6 root root 205 May 9 12:25 all-2023-05-09:12-25 // 全备的数据
[root@localhost ~]#
# 恢复全备数据,模拟误操作,删除数据库 agan
[root@localhost ~]# mysql -uroot -pAgan@3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> DROP DATABASE agan; # 删除 agan数据库
Query OK, 1 row affected (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
# 第一步停止mysql 服务
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2023-05-09 13:15:08 CST; 22h ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 12566 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 12548 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 12570 (mysqld)
CGroup: /system.slice/mysqld.service
└─12570 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
May 09 13:15:08 localhost.localdomain systemd[1]: Starting MySQL Server...
May 09 13:15:08 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]# systemctl stop mysqld # 停止mysql服务
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2023-05-10 11:30:20 CST; 4s ago
Docs: man:mysqld(8)
···省略N
# 第二步删除或者移走mysql存放的数据目录下的数据。数据存放目录:/var/lib/mysql
[root@localhost ~]# cd /var/lib/mysql
[root@localhost mysql]#
[root@localhost mysql]# ls
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 performance_schema public_key.pem server-key.pem
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 mysql private_key.pem server-cert.pem sys
[root@localhost mysql]# mkdir -p /tmp/test
[root@localhost mysql]# mv * /tmp/test
[root@localhost mysql]# ll
total 0
[root@localhost mysql]#
# 第三步,恢复数据
## 1.1恢复准备,数据文件达到一致性状态
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --apply-log /data/2023-05-10_11-09-34/
···省略N
--apply-log 准备恢复的准备工作:从指定的选项中读取配置信息并应用日志等。
--redo-only 准备工作完成后:还有其他增量备份集待处理,那么就必须指定本参数。强制xtrabackup只应用redo而不回滚。如果没有增量备份,那么就不用指定此参数
innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --apply-log --redo-only /data/2023-05-10_11-09-34/
##1.2恢复数据文件
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --copy-back /data/2023-05-10_11-09-34/
···省略N
# 第四步,查看mysql数据存放目录权限问题
[root@localhost mysql]# pwd
/var/lib/mysql
[root@localhost mysql]# ll
total 122916
drwxr-x--- 2 root root 56 May 10 11:40 agan
-rw-r----- 1 root root 372 May 10 11:40 ib_buffer_pool
-rw-r----- 1 root root 12582912 May 10 11:40 ibdata1
-rw-r----- 1 root root 50331648 May 10 11:40 ib_logfile0
-rw-r----- 1 root root 50331648 May 10 11:40 ib_logfile1
-rw-r----- 1 root root 12582912 May 10 11:40 ibtmp1
drwxr-x--- 2 root root 4096 May 10 11:40 mysql
drwxr-x--- 2 root root 8192 May 10 11:40 performance_schema
drwxr-x--- 2 root root 8192 May 10 11:40 sys
-rw-r----- 1 root root 442 May 10 11:40 xtrabackup_info
[root@localhost mysql]# chown -R mysql.mysql /var/lib/mysql
[root@localhost mysql]# ll
total 122916
drwxr-x--- 2 mysql mysql 56 May 10 11:40 agan
-rw-r----- 1 mysql mysql 372 May 10 11:40 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 10 11:40 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 10 11:40 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 10 11:40 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 May 10 11:40 ibtmp1
drwxr-x--- 2 mysql mysql 4096 May 10 11:40 mysql
drwxr-x--- 2 mysql mysql 8192 May 10 11:40 performance_schema
drwxr-x--- 2 mysql mysql 8192 May 10 11:40 sys
-rw-r----- 1 mysql mysql 442 May 10 11:40 xtrabackup_info
[root@localhost mysql]#
# 第五步,启动mysql服务
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-05-10 11:49:02 CST; 4s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 13100 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 13082 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 13103 (mysqld)
CGroup: /system.slice/mysqld.service
└─13103 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
May 10 11:49:00 localhost.localdomain systemd[1]: Starting MySQL Server...
May 10 11:49:02 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]#
[root@localhost ~]# mysql -uroot -pAgan@3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan | # 数据库:agan 恢复了
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from agan.t_test;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 阿伟 | 20 |
| 2 | 周扒皮 | 25 |
| 3 | 爱博 | 23 |
| 4 | 小玉 | 19 |
+----+-----------+------+
4 rows in set (0.02 sec)
mysql>
mysql> exit
# 之前删除的数据已恢复
3.3、innobackupex 增量备份
特别注意:
innobackupex 增量备份仅针对InnoDB这类支持事务的引擎,对于MyISAM等引擎,则仍然是全备。
增量备份需要基于全量备份来操作。
**之前我已经全备了:(/data/2023-05-10_11-09-34/) 需要在该全量备份的基础上做第一次增量备份。 **
# 模拟有数据在增加
[root@localhost ~]# mysql -uroot -pAgan@3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database test; # 创建数据库名为:test
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql>
mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
# 创建一张表名为:t_data
mysql> create table t_data(
-> id int not null primary key auto_increment,
-> name varchar(50) not null,
-> age char(8)
-> );
Query OK, 0 rows affected (0.01 sec)
# 插入数据
mysql> insert t_data(name,age) values('tom',23),('jerry',22),('agan',23);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t_data;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 23 |
| 2 | jerry | 22 |
| 3 | agan | 23 |
+----+-------+------+
3 rows in set (0.00 sec)
mysql> exit;
Bye
[root@localhost ~]#
# 模拟有数据增加之后,现在第一次增量备份
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --incremental-basedir=/data/2023-05-10_11-09-34/ --incremental /data/days1
···备份过程省略N
[root@localhost ~]# ll /data/days1/
total 0
drwxr-x--- 7 root root 243 May 14 09:08 2023-05-14_09-08-20
[root@localhost ~]# ll /data/days1/2023-05-14_09-08-20/
total 644
drwxr-x--- 2 root root 85 May 14 09:08 agan
-rw-r----- 1 root root 425 May 14 09:08 backup-my.cnf
-rw-r----- 1 root root 372 May 14 09:08 ib_buffer_pool
-rw-r----- 1 root root 606208 May 14 09:08 ibdata1.delta
-rw-r----- 1 root root 44 May 14 09:08 ibdata1.meta
drwxr-x--- 2 root root 4096 May 14 09:08 mysql
drwxr-x--- 2 root root 8192 May 14 09:08 performance_schema
drwxr-x--- 2 root root 8192 May 14 09:08 sys
drwxr-x--- 2 root root 85 May 14 09:08 test
-rw-r----- 1 root root 117 May 14 09:08 xtrabackup_checkpoints
-rw-r----- 1 root root 517 May 14 09:08 xtrabackup_info
-rw-r----- 1 root root 2560 May 14 09:08 xtrabackup_logfile
[root@localhost ~]#
其中:
–incremental-basedir 指向全量备份目录 --incremental 指向增量备份的目录
上面语句执行成功之后,会在–incremental执行的目录下创建一个时间戳子目录(本例中为:/data/days1/2023-05-14_09-08-20),在该目录下存放着增量备份的所有文件。
# 在备份目录下,有一个文件xtrabackup_checkpoints记录着备份信息,其中可以查出
1、全量备份的信息如下:
[root@localhost ~]# cat /data/2023-05-10_11-09-34/xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 2768542
last_lsn = 2768551
compact = 0
recover_binlog_info = 0
2、基于以上全量备份的增量备份的信息如下:
[root@localhost ~]# cat /data/days1/2023-05-14_09-08-20/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2768542 #从这里可以看出,增量备份的from_lsn正好等于全备的to_lsn。
to_lsn = 2775970
last_lsn = 2776057
compact = 0
recover_binlog_info = 0
[root@localhost ~]#
从上面可以看出,增量备份的from_lsn正好等于全备的to_lsn。
那么,我们是否可以在增量备份的基础上再做增量备份呢?
答案是肯定的,只要把–incremental-basedir执行上一次增量备份的目录即可,如下所示:
# 模拟第一次增量备份完后,又有新的数据增加,然后进行第二次的备份
[root@localhost ~]# mysql -uroot -pAgan@3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql> create database student; # 创建数据库名为:student
Query OK, 1 row affected (0.01 sec)
mysql> use student; # 进入数据库student
Database changed
# 创建表t_student
mysql> create table t_student(
-> id int(11) not null primary key auto_increment,
-> name varchar(100) not null,
-> age tinyint(4)
-> );
Query OK, 0 rows affected (0.02 sec)
# 查看表结构
mysql> desc t_student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
# 插入数据
mysql> insert t_student(name,age) values('tom',20),('jerry',23),('xixi',25),('sean',28),('zhangshan',26),('zhangshan',20),('lisi',null),('meimei',10),('wangwuu',3),('zhaoliu',15),('haha',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0
# 查看表t_student 所有字段
mysql> select * from t_student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | xixi | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | meimei | 10 |
| 9 | wangwu | 3 |
| 10 | zhaoliu | 15 |
| 11 | haha | 20 |
+----+-----------+------+
11 rows in set (0.00 sec)
mysql> exit
# 接着在第一次增量备份的基础上进行第二次增量备份
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --incremental-basedir=/data/days1/2023-05-14_09-08-20/ --incremental /data/days2
···备份过程省略N
--incremental-basedir必须指向第一次的增量备份
[root@localhost ~]# ll /data/
total 0
drwxr-x--- 6 root root 257 May 14 08:58 2023-05-10_11-09-34 // 全备
drwxr-x--- 3 root root 33 May 14 09:08 days1 // 第一次增量备份
drwxr-x--- 3 root root 33 May 14 09:13 days2 // 第二次增量备份
[root@localhost ~]# cat /data/2023-05-10_11-09-34/xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 2768542 # 这里
last_lsn = 2768551
compact = 0
recover_binlog_info = 0
[root@localhost ~]# cat /data/days1/2023-05-14_09-08-20/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2768542 # 第一次增量备份的from_lsn对应全量备份的to_lsn
to_lsn = 2775970
last_lsn = 2776057
compact = 0
recover_binlog_info = 0
[root@localhost ~]# cat /data/days2/2023-05-14_09-13-45/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2775970 #第二次增量备份from_lsn对应第一次的to_lsn
to_lsn = 2785036
last_lsn = 2785045
compact = 0
recover_binlog_info = 0
[root@localhost ~]#
3.4、模拟误操作,删除数据库:test和student,然后再使用增量进行恢复
# 模拟误操作,删除数据库:test和student,然后再进行恢复
[root@localhost ~]# mysql -uroot -pAgan@3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; # 查看当前所有数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan |
| mysql |
| performance_schema |
| student |
| sys |
| test |
+--------------------+
7 rows in set (0.01 sec)
mysql> drop database test; # 删除数据库名为: test
Query OK, 1 row affected (0.02 sec)
mysql> drop database student; # 删除数据库名为:student
Query OK, 1 row affected (0.01 sec)
mysql> show databases; # 查看当前有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit;
Bye
[root@localhost ~]#
# 接下来进行数据恢复,此选项--redo-only 阻止回滚未完成的事务,(注意最后一个不需要加--redo-only参数)
## 预准备完成备份
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --apply-log --redo-only /data/2023-05-10_11-09-34/
···省略N
# 合并第1次增量备份到完全备份
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --apply-log --redo-only /data/2023-05-10_11-09-34/ --incremental-dir=/data/days1/2023-05-14_09-08-20/
···省略N
# 合并第2次增量备份到完全备份,最后一次还原不需要加选项--redo-only
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --apply-log /data/2023-05-10_11-09-34/ --incremental-dir=/data/days2/2023-05-14_09-13-45/
···省略N
# 停止mysql服务,并移除mysql存放目录下的文件,或者删除
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2023-05-14 08:59:46 CST; 31min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 15458 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 15440 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 15461 (mysqld)
CGroup: /system.slice/mysqld.service
└─15461 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
May 14 08:59:45 localhost.localdomain systemd[1]: Starting MySQL Server...
May 14 08:59:46 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# mkdir -p /tmp/test100
[root@localhost ~]# mv /var/lib/mysql/* /tmp/test100/
[root@localhost ~]# ll /var/lib/mysql
total 0
[root@localhost ~]#
# 复制到数据库目录,注意数据库目录必须为空,MySQL服务不能启动
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=Agan@3306 --copy-back /data/2023-05-10_11-09-34/
···省略N
# 恢复之后查看一下,所有者和所属组是mysql用户的
[root@localhost ~]# ll /var/lib/mysql
total 122916
drwxr-x--- 2 root root 56 May 14 09:31 agan
-rw-r----- 1 root root 372 May 14 09:31 ib_buffer_pool
-rw-r----- 1 root root 12582912 May 14 09:31 ibdata1
-rw-r----- 1 root root 50331648 May 14 09:31 ib_logfile0
-rw-r----- 1 root root 50331648 May 14 09:31 ib_logfile1
-rw-r----- 1 root root 12582912 May 14 09:31 ibtmp1
drwxr-x--- 2 root root 4096 May 14 09:31 mysql
drwxr-x--- 2 root root 8192 May 14 09:31 performance_schema
drwxr-x--- 2 root root 62 May 14 09:31 student
drwxr-x--- 2 root root 8192 May 14 09:31 sys
drwxr-x--- 2 root root 56 May 14 09:31 test
-rw-r----- 1 root root 523 May 14 09:31 xtrabackup_info
[root@localhost ~]# chown -R mysql.mysql /var/lib/mysql
[root@localhost ~]# ll /var/lib/mysql
total 122916
drwxr-x--- 2 mysql mysql 56 May 14 09:31 agan
-rw-r----- 1 mysql mysql 372 May 14 09:31 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 14 09:31 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 14 09:31 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 14 09:31 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 May 14 09:31 ibtmp1
drwxr-x--- 2 mysql mysql 4096 May 14 09:31 mysql
drwxr-x--- 2 mysql mysql 8192 May 14 09:31 performance_schema
drwxr-x--- 2 mysql mysql 62 May 14 09:31 student
drwxr-x--- 2 mysql mysql 8192 May 14 09:31 sys
drwxr-x--- 2 mysql mysql 56 May 14 09:31 test
-rw-r----- 1 mysql mysql 523 May 14 09:31 xtrabackup_info
[root@localhost ~]#
# 启动mysql服务
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2023-05-14 09:33:32 CST; 7s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 15667 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 15649 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 15671 (mysqld)
CGroup: /system.slice/mysqld.service
└─15671 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
May 14 09:33:31 localhost.localdomain systemd[1]: Starting MySQL Server...
May 14 09:33:32 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]#
[root@localhost ~]# mysql -uroot -pAgan@3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; # 查看数据是否已恢复
+--------------------+
| Database |
+--------------------+
| information_schema |
| agan |
| mysql |
| performance_schema |
| student |
| sys |
| test |
+--------------------+
7 rows in set (0.00 sec)
mysql> select * from test.t_data;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 23 |
| 2 | jerry | 22 |
| 3 | agan | 23 |
+----+-------+------+
3 rows in set (0.01 sec)
mysql> select * from student.t_student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | xixi | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | meimei | 10 |
| 9 | wangwuu | 3 |
| 10 | zhaoliu | 15 |
| 11 | haha | 20 |
+----+-----------+------+
11 rows in set (0.00 sec)
mysql> exit
[root@localhost ~]#
另外注意: 上面在做备份的时候,将备份目录和增量目录都放在了同一个目录路径下,其实推荐放在不同的路径下,方便管理!比如:
/backup/mysql/data/full 存放全量备份目录
/backup/mysql/data/daily1 存放第一次增量备份目录
/backup/mysql/data/daily2 存放第二次增量目录
以此类推
在恢复的时候,注意命令中的路径要跟对!
innobackupex 参数
innobackupex全量、增量备份脚本
可以根据自己线上数据库情况,编写全量和增量备份脚本,然后结合crontab设置计划执行。
比如:每周日的1:00进行全量备份,每周1-6的1:00进行增量备份。
还可以在脚本里编写邮件通知信息(可以用mail或sendemail)
4、二进制安装mysql8.0.x的方式
使用xftp 把mysql的二进制包传到系统里
# 创建mysql系统用户,没有家目录,不需要登录
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
# 解压到 /usr/local/ 目录下
[root@localhost opt]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@localhost opt]# ll -d /usr/local/mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 May 8 15:12 /usr/local/mysql-8.0.28-linux-glibc2.12-x86_64
[root@localhost opt]# cd /usr/local/
[root@localhost local]# mv mysql-8.0.28-linux-glibc2.12-x86_64 mysql # 重名名为mysql
[root@localhost local]# ll -d mysql/
drwxr-xr-x. 9 root root 129 May 8 15:12 mysql/
[root@localhost local]#
#修改目录/usr/local/mysql的属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll -d /usr/local/mysql
drwxr-xr-x. 9 mysql mysql 129 May 8 15:12 /usr/local/mysql
[root@localhost ~]#
[root@localhost ~]# ll /usr/local/mysql/
total 284
drwxr-xr-x. 2 mysql mysql 4096 Dec 18 2021 bin
drwxr-xr-x. 2 mysql mysql 55 Dec 18 2021 docs
drwxr-xr-x. 3 mysql mysql 282 Dec 18 2021 include
drwxr-xr-x. 6 mysql mysql 201 Dec 18 2021 lib
-rw-r--r--. 1 mysql mysql 276595 Dec 18 2021 LICENSE
drwxr-xr-x. 4 mysql mysql 30 Dec 18 2021 man
-rw-r--r--. 1 mysql mysql 666 Dec 18 2021 README
drwxr-xr-x. 28 mysql mysql 4096 Dec 18 2021 share
drwxr-xr-x. 2 mysql mysql 77 Dec 18 2021 support-files
[root@localhost ~]#
# 创建数据存放目录
[root@localhost ~]# mkdir -p /opt/mysqldata
[root@localhost ~]# ll -d /opt/mysqldata
drwxr-xr-x. 2 root root 6 May 8 15:16 /opt/mysqldata
[root@localhost ~]#
[root@localhost ~]# chown -R mysql.mysql /opt/mysqldata # 修改所有者和所属组为MySQL
[root@localhost ~]# ll -d /opt/mysqldata/
drwxr-xr-x. 2 mysql mysql 6 May 8 15:16 /opt/mysqldata/
[root@localhost ~]#
# 添加环境变量
[root@localhost ~]# which mysql # 没设置环境变量之前,是找不到,mysql命令的
/usr/bin/which: no mysql in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]#
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh # 刷新让其生效
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]#
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql
[root@localhost ~]#
# 创建软链接。叫mysql
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost ~]# ll -d /usr/include/mysql
lrwxrwxrwx. 1 root root 24 May 8 15:23 /usr/include/mysql -> /usr/local/mysql/include
[root@localhost ~]#
# /usr/include/mysql 这个文件源自于/usr/local/mysql/include
# 配置 man 文档
[root@localhost ~]# vim /etc/man_db.conf
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
# Lines beginning with `#' are comments and are ignored. Any combination of
# tabs or spaces may be used as `whitespace' separators.
#
# There are three mappings allowed in this file:
# --------------------------------------------------------
# MANDATORY_MANPATH manpath_element
# MANPATH_MAP path_element manpath_element
# MANDB_MAP global_manpath [relative_catpath]
#---------------------------------------------------------
# every automatically generated MANPATH includes these fields
#
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man # 添加man文档路径
# 映射库文件
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig # 让其生效
[root@localhost ~]#
# 初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/mysqldata
2023-05-08T07:31:51.755160Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 16438
2023-05-08T07:31:51.766308Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-05-08T07:31:52.388829Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-05-08T07:31:53.605686Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +.ke0r3rgd6F # 临时密码:+.ke0r3rgd6F
// 如果不想显示密码可以用这个参数:将--initialize替换为 --initialize-insecure
# 生成配置文件
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysqldata
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysqldata/mysql.pid
user = mysql
skip-name-resolve # 跳过名称解析,使用IP地址访问
# 配置启动mysql服务,用systemctl来控制mysql服务
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ll
total 20
-rwxr-xr-x. 1 mysql mysql 1061 Dec 18 2021 mysqld_multi.server
-rw-r--r--. 1 mysql mysql 2027 Dec 18 2021 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 Dec 18 2021 mysql.server
[root@localhost support-files]# cp -r mysql.server mysqld
[root@localhost support-files]# ll
total 32
-rwxr-xr-x. 1 root root 10576 May 8 15:45 mysqld
-rwxr-xr-x. 1 mysql mysql 1061 Dec 18 2021 mysqld_multi.server
-rw-r--r--. 1 mysql mysql 2027 Dec 18 2021 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 Dec 18 2021 mysql.server
[root@localhost support-files]# sed -n '/^datadir=/p' mysqld
datadir=/opt/mysqldata # vi 进去修改路径:数据存放路径
[root@localhost support-files]# sed -n '/^basedir=/p' mysqld
basedir=/usr/local/mysql # vi 进去修改路径,mysql目录路径
[root@localhost support-files]#
[root@localhost support-files]# chown -R mysql.mysql mysqld
[root@localhost support-files]# ll
total 32
-rwxr-xr-x. 1 mysql mysql 10576 May 8 15:45 mysqld
-rwxr-xr-x. 1 mysql mysql 1061 Dec 18 2021 mysqld_multi.server
-rw-r--r--. 1 mysql mysql 2027 Dec 18 2021 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 Dec 18 2021 mysql.server
[root@localhost support-files]#
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service
[root@localhost system]# vi mysqld.service
[Unit]
Description=mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]#
[root@localhost ~]# systemctl daemon-reload # 重新加载
[root@localhost ~]# systemctl enable --now mysqld # 启动服务,并设置开机自启
[root@localhost ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2023-05-08 16:43:08 CST; 8min ago
Process: 1987 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, status=0/SUCCESS)
Main PID: 2000 (mysqld_safe)
Tasks: 38 (limit: 17399)
····省略N
# 登录
[root@localhost ~]# mysql -uroot -p'+.ke0r3rgd6F'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 修改密码
mysql> alter user 'root'@'localhost' identified by 'Agan@3306';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges; # 刷新授权表
Query OK, 0 rows affected (0.00 sec)
mysql> exit; # 退出
Bye
[root@localhost ~]#
5、下载xtrabackup8.0版本
5.1、CentOS7安装xtrabackup8.0
[root@localhost ~]# cd /opt/
[root@localhost opt]# ll
total 1176056
-rw-r--r--. 1 root root 1204277208 May 8 15:08 mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
drwxr-xr-x. 6 mysql mysql 4096 May 8 16:43 mysqldata
[root@localhost opt]# which wget
/usr/bin/wget
# 下载xtrabackup8.0
[root@localhost opt]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-8.0.4/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.4-1.el7.x86_64.rpm
[root@localhost opt]# ls
mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz mysqldata percona-xtrabackup-80-8.0.4-1.el7.x86_64.rpm
[root@localhost opt]# dnf -y install percona-xtrabackup-80-8.0.4-1.el7.x86_64.rpm
····安装过程省略N
5.2、CentOS8安装xtrabackup8.0(这里用CentOS8系统来演示)
xtrabackup官网
[root@localhost ~]# cat /etc/redhat-release
CentOS Stream release 8
[root@localhost ~]# mysql -V
mysql Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
[root@localhost ~]#
[root@localhost ~]# cd /opt/
[root@localhost opt]# wget https://downloads.percona.com/downloads/Percona-XtraBackup-8.0/Percona-XtraBackup-8.0.28-21/binary/redhat/8/x86_64/percona-xtrabackup-80-8.0.28-21.1.el8.x86_64.rpm
···下载过程省略N
[root@localhost opt]# ls
mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz mysqldata percona-xtrabackup-80-8.0.28-21.1.el8.x86_64.rpm
[root@localhost opt]#
[root@localhost opt]# dnf -y install percona-xtrabackup-80-8.0.28-21.1.el8.x86_64.rpm // 安装xtrabackup8.0
···安装过程省略N
[root@localhost opt]# which xtrabackup
/usr/bin/xtrabackup
[root@localhost opt]# rpm -qa | grep percona-xtrabackup
percona-xtrabackup-80-8.0.28-21.1.el8.x86_64
[root@localhost opt]#
5.3、xtrabackup8.0全量备份mysql8.0.28, 以及恢复。
[root@localhost ~]# cat .my.cnf
[mysql]
user=root
password=Agan@3306
[root@localhost ~]#
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
# 没有进入agan数据库的情况下创建表:t_agan
mysql> create table agan.t_agan(
-> id int not null primary key auto_increment,
-> name varchar(50) not null,
-> age char(8)
-> )charset utf8mb4;
Query OK, 0 rows affected (0.02 sec)
mysql> desc agan.t_agan;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| age | char(8) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql>
# 插入数据
mysql> insert agan.t_agan(name,age) values('孟波',20),('周扒皮',25),('爱博',23),('小玉',19);
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from agan.t_agan;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 孟波 | 20 |
| 2 | 周扒皮 | 25 |
| 3 | 爱博 | 23 |
| 4 | 小玉 | 19 |
+----+-----------+------+
4 rows in set (0.00 sec)
mysql> exit
[root@localhost ~]#
# 创建备份存放目录
[root@localhost ~]# mkdir -p /data/
[root@localhost ~]# ll -d /data/
drwxr-xr-x 2 root root 6 May 14 14:14 /data/
[root@localhost ~]#
# 全量备份
[root@localhost ~]# xtrabackup --backup --target-dir=/data/full --datadir=/opt/mysqldata/ --user=root --password=Agan@3306 -S /tmp/mysql.sock
···备份过程省略N
[root@localhost ~]# ll /data/
total 4
drwxr-xr-x 6 root root 4096 May 14 14:23 full
[root@localhost ~]#
[root@localhost ~]# ll /data/full/
total 69684
drwxr-x--- 2 root root 24 May 14 14:23 agan
-rw-r----- 1 root root 475 May 14 14:23 backup-my.cnf
-rw-r----- 1 root root 157 May 14 14:23 binlog.000002
-rw-r----- 1 root root 16 May 14 14:23 binlog.index
-rw-r----- 1 root root 5914 May 14 14:23 ib_buffer_pool
-rw-r----- 1 root root 12582912 May 14 14:23 ibdata1
drwxr-x--- 2 root root 143 May 14 14:23 mysql
-rw-r----- 1 root root 25165824 May 14 14:23 mysql.ibd
drwxr-x--- 2 root root 8192 May 14 14:23 performance_schema
drwxr-x--- 2 root root 28 May 14 14:23 sys
-rw-r----- 1 root root 16777216 May 14 14:23 undo_001
-rw-r----- 1 root root 16777216 May 14 14:23 undo_002
-rw-r----- 1 root root 18 May 14 14:23 xtrabackup_binlog_info
-rw-r----- 1 root root 102 May 14 14:23 xtrabackup_checkpoints
-rw-r----- 1 root root 521 May 14 14:23 xtrabackup_info
-rw-r----- 1 root root 2560 May 14 14:23 xtrabackup_logfile
-rw-r----- 1 root root 39 May 14 14:23 xtrabackup_tablespaces
# 模拟误操作,删除数据库:agan
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database agan; # 删除数据库:agan
Query OK, 1 row affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> exit;
[root@localhost ~]# systemctl stop mysqld # 停止mysql
[root@localhost ~]# mkdir -p /tmp/test
[root@localhost ~]# mv /opt/mysqldata/* /tmp/test/
[root@localhost ~]# ll /opt/mysqldata/
total 0
[root@localhost ~]#
# 1.恢复全量备份的数据
## 1.1检查数据一致性
[root@localhost ~]# xtrabackup --prepare --target-dir=/data/full --user=root --password=Agan@3306
···省略N
## 1.2恢复数据
[root@localhost ~]# xtrabackup --copy-back --target-dir=/data/full --user=root --password=Agan@3306
···省略N
# 修改文件及文件夹属主
[root@localhost ~]# ll /opt/mysqldata/
total 180260
drwxr-x--- 2 root root 24 May 14 15:02 agan
-rw-r----- 1 root root 157 May 14 15:02 binlog.000003
-rw-r----- 1 root root 14 May 14 15:02 binlog.index
-rw-r----- 1 root root 5914 May 14 15:02 ib_buffer_pool
-rw-r----- 1 root root 12582912 May 14 15:02 ibdata1
-rw-r----- 1 root root 50331648 May 14 15:02 ib_logfile0
-rw-r----- 1 root root 50331648 May 14 15:02 ib_logfile1
-rw-r----- 1 root root 12582912 May 14 15:02 ibtmp1
drwxr-x--- 2 root root 143 May 14 15:02 mysql
-rw-r----- 1 root root 25165824 May 14 15:02 mysql.ibd
drwxr-x--- 2 root root 8192 May 14 15:02 performance_schema
drwxr-x--- 2 root root 28 May 14 15:02 sys
-rw-r----- 1 root root 16777216 May 14 15:02 undo_001
-rw-r----- 1 root root 16777216 May 14 15:02 undo_002
-rw-r----- 1 root root 521 May 14 15:02 xtrabackup_info
-rw-r----- 1 root root 1 May 14 15:02 xtrabackup_master_key_id
[root@localhost ~]# chown -R mysql.mysql /opt/mysqldata
[root@localhost ~]# ll /opt/mysqldata/
total 180260
drwxr-x--- 2 mysql mysql 24 May 14 15:02 agan
-rw-r----- 1 mysql mysql 157 May 14 15:02 binlog.000003
-rw-r----- 1 mysql mysql 14 May 14 15:02 binlog.index
-rw-r----- 1 mysql mysql 5914 May 14 15:02 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 14 15:02 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 14 15:02 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 14 15:02 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 May 14 15:02 ibtmp1
drwxr-x--- 2 mysql mysql 143 May 14 15:02 mysql
-rw-r----- 1 mysql mysql 25165824 May 14 15:02 mysql.ibd
drwxr-x--- 2 mysql mysql 8192 May 14 15:02 performance_schema
drwxr-x--- 2 mysql mysql 28 May 14 15:02 sys
-rw-r----- 1 mysql mysql 16777216 May 14 15:02 undo_001
-rw-r----- 1 mysql mysql 16777216 May 14 15:02 undo_002
-rw-r----- 1 mysql mysql 521 May 14 15:02 xtrabackup_info
-rw-r----- 1 mysql mysql 1 May 14 15:02 xtrabackup_master_key_id
[root@localhost ~]#
# 启动mysql
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2023-05-14 15:05:18 CST; 5s ago
Process: 13315 ExecStop=/usr/local/mysql/support-files/mysqld stop (code=exited, status=0/SUCCESS)
Process: 13432 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, status=0/SUCCESS)
Main PID: 13445 (mysqld_safe)
Tasks: 39 (limit: 17399)
Memory: 401.0M
CGroup: /system.slice/mysqld.service
├─13445 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/mysqldata --pid-file=/opt/mysqldata/mysql.pid
└─13633 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --l>
May 14 15:05:15 localhost.localdomain systemd[1]: Starting mysql server daemon...
May 14 15:05:15 localhost.localdomain mysqld[13432]: Starting MySQL.
May 14 15:05:15 localhost.localdomain mysqld[13445]: Starting MySQL.
May 14 15:05:16 localhost.localdomain mysqld[13445]: ogging to '/opt/mysqldata/localhost.loc
May 14 15:05:18 localhost.localdomain mysqld[13432]: o. SUCCESS!
May 14 15:05:18 localhost.localdomain systemd[1]: Started mysql server daemon.
[root@localhost ~]#
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; # 查看数据库:agan是否已恢复
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from agan.t_agan;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 孟波 | 20 |
| 2 | 周扒皮 | 25 |
| 3 | 爱博 | 23 |
| 4 | 小玉 | 19 |
+----+-----------+------+
4 rows in set (0.04 sec)
mysql> exit;
[root@localhost ~]#
5.4、xtrabackup8.0增量备份以及恢复
在全量备份的基础上备份增量备份。(全量备份:/data/full )
[root@localhost ~]# ll /data/
total 4
drwxr-x--- 7 root root 4096 May 14 14:55 full # 全量备份
[root@localhost ~]#
# 模拟有数据在增加
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database test; # 创建数据库名为:test
# 创建表名为:t_test
mysql> create table test.t_test(
-> id int not null primary key auto_increment,
-> name varchar(50) not null,
-> age char(8)
-> )charset utf8mb4;
Query OK, 0 rows affected (0.01 sec)
# 插入数据
mysql> insert test.t_test(name,age) values('阿干',20),('王五',25),('张学友',25),('爱德华',23);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from test.t_test;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 阿干 | 20 |
| 2 | 王五 | 25 |
| 3 | 张学友 | 25 |
| 4 | 爱德华 | 23 |
+----+-----------+------+
4 rows in set (0.00 sec)
mysql> exit;
# 第一次备份数据
[root@localhost ~]# xtrabackup --backup --target-dir=/data/days1 --incremental-basedir=/data/full --datadir=/opt/mysqldata/ --user=root --password=Agan@3306 -S /tmp/mysql.sock
···省略N
--backup 备份
--target-dir 备份好之后存放的目录
--incremental-basedir 指定全备的目录
--datadir 指定mysql数据库存放的路径
--user 指定用户(一般是root)
--password 指定密码
-S /tmp/mysql.sock 指定 /tmp/mysql.sock
[root@localhost ~]# ll /data/
total 8
drwxr-x--- 7 root root 4096 May 14 15:24 days1 #第一次增备
drwxr-x--- 7 root root 4096 May 14 14:55 full # 全备
[root@localhost ~]# ll /data/days1/
total 1444
drwxr-x--- 2 root root 100 May 14 15:24 agan
-rw-r----- 1 root root 475 May 14 15:24 backup-my.cnf
-rw-r----- 1 root root 157 May 14 15:24 binlog.000005
-rw-r----- 1 root root 16 May 14 15:24 binlog.index
-rw-r----- 1 root root 5914 May 14 15:24 ib_buffer_pool
-rw-r----- 1 root root 49152 May 14 15:24 ibdata1.delta
-rw-r----- 1 root root 64 May 14 15:24 ibdata1.meta
drwxr-x--- 2 root root 143 May 14 15:24 mysql
-rw-r----- 1 root root 671744 May 14 15:24 mysql.ibd.delta
-rw-r----- 1 root root 73 May 14 15:24 mysql.ibd.meta
drwxr-x--- 2 root root 8192 May 14 15:24 performance_schema
drwxr-x--- 2 root root 61 May 14 15:24 sys
drwxr-x--- 2 root root 53 May 14 15:24 test
-rw-r----- 1 root root 360448 May 14 15:24 undo_001.delta
-rw-r----- 1 root root 69 May 14 15:24 undo_001.meta
-rw-r----- 1 root root 327680 May 14 15:24 undo_002.delta
-rw-r----- 1 root root 69 May 14 15:24 undo_002.meta
-rw-r----- 1 root root 18 May 14 15:24 xtrabackup_binlog_info
-rw-r----- 1 root root 107 May 14 15:24 xtrabackup_checkpoints
-rw-r----- 1 root root 562 May 14 15:24 xtrabackup_info
-rw-r----- 1 root root 2560 May 14 15:24 xtrabackup_logfile
-rw-r----- 1 root root 39 May 14 15:24 xtrabackup_tablespaces
[root@localhost ~]#
# 模拟第二次有数据在增加
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql> create database tom; # 创建数据库名为:tom
Query OK, 1 row affected (0.01 sec)
mysql>
# 在数据库tom下创建表名为:t_test
mysql> create table tom.t_test(
-> id int not null primary key auto_increment,
-> name varchar(50) not null,
-> age char(8)
-> )charset utf8mb4;
Query OK, 0 rows affected (0.01 sec)
# 插入数据
mysql> insert tom.t_test(name,age) values('杰克',20),('路飞',25),('索隆',23),('娜美',19);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from tom.t_test;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | 杰克 | 20 |
| 2 | 路飞 | 25 |
| 3 | 索隆 | 23 |
| 4 | 娜美 | 19 |
+----+--------+------+
4 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| tom |
+--------------------+
7 rows in set (0.00 sec)
mysql> exit;
# 第二次增量备份
[root@localhost ~]# xtrabackup --backup --target-dir=/data/days2 --incremental-basedir=/data/days1 --datadir=/opt/mysqldata/ --user=root --password=Agan@3306 -S /tmp/mysql.sock
···省略N
[root@localhost ~]# ll /data/
total 12
drwxr-x--- 7 root root 4096 May 14 15:24 days1 # 第一次增量备份
drwxr-x--- 8 root root 4096 May 14 15:36 days2 # 第二次增量备份
drwxr-x--- 7 root root 4096 May 14 14:55 full # 全量备份
[root@localhost ~]# ll /data/days2/
total 1172
drwxr-x--- 2 root root 100 May 14 15:36 agan
-rw-r----- 1 root root 475 May 14 15:36 backup-my.cnf
-rw-r----- 1 root root 157 May 14 15:36 binlog.000006
-rw-r----- 1 root root 16 May 14 15:36 binlog.index
-rw-r----- 1 root root 5914 May 14 15:36 ib_buffer_pool
-rw-r----- 1 root root 49152 May 14 15:36 ibdata1.delta
-rw-r----- 1 root root 64 May 14 15:36 ibdata1.meta
drwxr-x--- 2 root root 143 May 14 15:36 mysql
-rw-r----- 1 root root 688128 May 14 15:36 mysql.ibd.delta
-rw-r----- 1 root root 73 May 14 15:36 mysql.ibd.meta
drwxr-x--- 2 root root 8192 May 14 15:36 performance_schema
drwxr-x--- 2 root root 61 May 14 15:36 sys
drwxr-x--- 2 root root 53 May 14 15:36 test
drwxr-x--- 2 root root 53 May 14 15:36 tom
-rw-r----- 1 root root 180224 May 14 15:36 undo_001.delta
-rw-r----- 1 root root 69 May 14 15:36 undo_001.meta
-rw-r----- 1 root root 212992 May 14 15:36 undo_002.delta
-rw-r----- 1 root root 69 May 14 15:36 undo_002.meta
-rw-r----- 1 root root 18 May 14 15:36 xtrabackup_binlog_info
-rw-r----- 1 root root 107 May 14 15:36 xtrabackup_checkpoints
-rw-r----- 1 root root 563 May 14 15:36 xtrabackup_info
-rw-r----- 1 root root 2560 May 14 15:36 xtrabackup_logfile
-rw-r----- 1 root root 39 May 14 15:36 xtrabackup_tablespaces
[root@localhost ~]#
# 模拟误删除,然后使用增量备份恢复数据
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| tom |
+--------------------+
7 rows in set (0.00 sec)
mysql> drop database test; # 删除数据库:test
Query OK, 1 row affected (0.01 sec)
mysql> drop database tom; # 删除数据库:tom
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit;
# 1.恢复数据:
## 1.1恢复数据为一致性
[root@localhost ~]# xtrabackup --prepare --apply-log-only --target-dir=/data/full
····省略N
[root@localhost ~]# xtrabackup --prepare --apply-log-only --target-dir=/data/full --incremental-dir=/data/days1
···省略N
## 最后恢复一致性的时候不需要加 --apply-log-only
[root@localhost ~]# xtrabackup --prepare --target-dir=/data/full --incremental-dir=/data/days2
···省略N
# 关闭mysql 服务,并清空数据mysql数据存放的目录
[root@localhost ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2023-05-14 15:05:18 CST; 46min ago
Process: 13315 ExecStop=/usr/local/mysql/support-files/mysqld stop (code=exited, status=0/SUCCESS)
Process: 13432 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, status=0/SUCCESS)
Main PID: 13445 (mysqld_safe)
Tasks: 40 (limit: 17399)
Memory: 403.3M
CGroup: /system.slice/mysqld.service
├─13445 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/mysqldata --pid-file=/opt/mysqldata/mysql.pid
└─13633 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --l>
May 14 15:05:15 localhost.localdomain systemd[1]: Starting mysql server daemon...
May 14 15:05:15 localhost.localdomain mysqld[13432]: Starting MySQL.
May 14 15:05:15 localhost.localdomain mysqld[13445]: Starting MySQL.
May 14 15:05:16 localhost.localdomain mysqld[13445]: ogging to '/opt/mysqldata/localhost.loc
May 14 15:05:18 localhost.localdomain mysqld[13432]: o. SUCCESS!
May 14 15:05:18 localhost.localdomain systemd[1]: Started mysql server daemon.
[root@localhost ~]# systemctl stop mysqld // 关闭
[root@localhost ~]# mkdir -p /tmp/backup
[root@localhost ~]# mv /opt/mysqldata/* /tmp/backup/
[root@localhost ~]# ll /opt/mysqldata/
total 0
[root@localhost ~]#
[root@localhost ~]# ll /data/full/
total 188468
drwxr-x--- 2 root root 42 May 14 15:46 agan
-rw-r----- 1 root root 475 May 14 14:34 backup-my.cnf
-rw-r----- 1 root root 157 May 14 15:48 binlog.000006
-rw-r----- 1 root root 16 May 14 15:48 binlog.index
-rw-r----- 1 root root 5914 May 14 14:34 ib_buffer_pool
-rw-r----- 1 root root 12582912 May 14 15:48 ibdata1
-rw-r----- 1 root root 50331648 May 14 15:48 ib_logfile0
-rw-r----- 1 root root 50331648 May 14 14:55 ib_logfile1
-rw-r----- 1 root root 12582912 May 14 15:48 ibtmp1
drwxr-x--- 2 root root 6 May 14 15:48 '#innodb_temp'
drwxr-x--- 2 root root 143 May 14 15:48 mysql
-rw-r----- 1 root root 25165824 May 14 15:48 mysql.ibd
drwxr-x--- 2 root root 8192 May 14 15:48 performance_schema
drwxr-x--- 2 root root 28 May 14 14:34 sys
drwxr-x--- 2 root root 24 May 14 15:46 test # 看这里
drwxr-x--- 2 root root 24 May 14 15:48 tom # 看这里
-rw-r----- 1 root root 16777216 May 14 15:48 undo_001
-rw-r----- 1 root root 16777216 May 14 15:48 undo_002
-rw-r----- 1 root root 18 May 14 15:48 xtrabackup_binlog_info
-rw-r----- 1 root root 102 May 14 15:48 xtrabackup_checkpoints
-rw-r----- 1 root root 563 May 14 15:48 xtrabackup_info
-rw-r----- 1 root root 8388608 May 14 14:55 xtrabackup_logfile
-rw-r--r-- 1 root root 1 May 14 15:48 xtrabackup_master_key_id
-rw-r----- 1 root root 39 May 14 15:48 xtrabackup_tablespaces
[root@localhost ~]#
# 2.恢复数据 --copy-back
[root@localhost ~]# xtrabackup --copy-back --target-dir=/data/full --user=root --password=Agan@3306
···省略N
# 修改所有者和所属组
[root@localhost ~]# ll /opt/mysqldata/
total 180260
drwxr-x--- 2 root root 42 May 14 15:58 agan
-rw-r----- 1 root root 157 May 14 15:58 binlog.000006
-rw-r----- 1 root root 14 May 14 15:58 binlog.index
-rw-r----- 1 root root 5914 May 14 15:58 ib_buffer_pool
-rw-r----- 1 root root 12582912 May 14 15:58 ibdata1
-rw-r----- 1 root root 50331648 May 14 15:58 ib_logfile0
-rw-r----- 1 root root 50331648 May 14 15:58 ib_logfile1
-rw-r----- 1 root root 12582912 May 14 15:58 ibtmp1
drwxr-x--- 2 root root 143 May 14 15:58 mysql
-rw-r----- 1 root root 25165824 May 14 15:58 mysql.ibd
drwxr-x--- 2 root root 8192 May 14 15:58 performance_schema
drwxr-x--- 2 root root 28 May 14 15:58 sys
drwxr-x--- 2 root root 24 May 14 15:58 test
drwxr-x--- 2 root root 24 May 14 15:58 tom
-rw-r----- 1 root root 16777216 May 14 15:58 undo_001
-rw-r----- 1 root root 16777216 May 14 15:58 undo_002
-rw-r----- 1 root root 563 May 14 15:58 xtrabackup_info
-rw-r----- 1 root root 1 May 14 15:58 xtrabackup_master_key_id
[root@localhost ~]# chown -R mysql.mysql /opt/mysqldata
[root@localhost ~]# ll /opt/mysqldata/
total 180260
drwxr-x--- 2 mysql mysql 42 May 14 15:58 agan
-rw-r----- 1 mysql mysql 157 May 14 15:58 binlog.000006
-rw-r----- 1 mysql mysql 14 May 14 15:58 binlog.index
-rw-r----- 1 mysql mysql 5914 May 14 15:58 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 14 15:58 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 14 15:58 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 14 15:58 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 May 14 15:58 ibtmp1
drwxr-x--- 2 mysql mysql 143 May 14 15:58 mysql
-rw-r----- 1 mysql mysql 25165824 May 14 15:58 mysql.ibd
drwxr-x--- 2 mysql mysql 8192 May 14 15:58 performance_schema
drwxr-x--- 2 mysql mysql 28 May 14 15:58 sys
drwxr-x--- 2 mysql mysql 24 May 14 15:58 test
drwxr-x--- 2 mysql mysql 24 May 14 15:58 tom
-rw-r----- 1 mysql mysql 16777216 May 14 15:58 undo_001
-rw-r----- 1 mysql mysql 16777216 May 14 15:58 undo_002
-rw-r----- 1 mysql mysql 563 May 14 15:58 xtrabackup_info
-rw-r----- 1 mysql mysql 1 May 14 15:58 xtrabackup_master_key_id
[root@localhost ~]#
# 启动mysql
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2023-05-14 15:59:48 CST; 3s ago
Process: 13891 ExecStop=/usr/local/mysql/support-files/mysqld stop (code=exited, status=0/SUCCESS)
Process: 13934 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, status=0/SUCCESS)
Main PID: 13947 (mysqld_safe)
Tasks: 39 (limit: 17399)
Memory: 401.5M
CGroup: /system.slice/mysqld.service
├─13947 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/mysqldata --pid-file=/opt/mysqldata/mysql.pid
└─14135 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --l>
May 14 15:59:46 localhost.localdomain systemd[1]: Starting mysql server daemon...
May 14 15:59:46 localhost.localdomain mysqld[13934]: Starting MySQL.
May 14 15:59:46 localhost.localdomain mysqld[13947]: Starting MySQL.
May 14 15:59:47 localhost.localdomain mysqld[13947]: ogging to '/opt/mysqldata/localhost.loc
May 14 15:59:48 localhost.localdomain mysqld[13934]: o SUCCESS!
May 14 15:59:48 localhost.localdomain systemd[1]: Started mysql server daemon.
lines 1-18/18 (END)
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; # 查看数据是否已恢复
+--------------------+
| Database |
+--------------------+
| agan |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| tom |
+--------------------+
7 rows in set (0.01 sec)
mysql> select * from tom.t_test;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | 杰克 | 20 |
| 2 | 路飞 | 25 |
| 3 | 索隆 | 23 |
| 4 | 娜美 | 19 |
+----+--------+------+
4 rows in set (0.01 sec)
mysql> select * from test.t_test;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 阿干 | 20 |
| 2 | 王五 | 25 |
| 3 | 张学友 | 25 |
| 4 | 爱德华 | 23 |
+----+-----------+------+
4 rows in set (0.01 sec)
mysql> exit;
[root@localhost ~]#
报错信息:
登录mysql报错:
[root@localhost ~]# mysql -uroot -p'+.ke0r3rgd6F'
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
解决:
[root@localhost ~]# find / -name libtinfo.so* 2> /dev/null
/usr/lib64/libtinfo.so.6
/usr/lib64/libtinfo.so.6.1
[root@localhost ~]# ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5
安装 xtrabackup2.4报错问题
Error: Package: perl-DBD-MySQL-4.023-6.el7.x86_64 (base)
Requires: libmysqlclient.so.18()(64bit)
Error: Package: perl-DBD-MySQL-4.023-6.el7.x86_64 (base)
Requires: libmysqlclient.so.18(libmysqlclient_18)(64bit)
解决
// 安装这个包
[root@node1 ~]# yum -y install https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm
备份数据报错:
[root@localhost ~]# xtrabackup --backup --target-dir=/data/backups/base --datadir=/var/lib/mysql/
Can't locate Digest/MD5.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at - line 693.
解决:
[root@localhost ~]# yum list all | grep perl-Digest-MD5
perl-Digest-MD5.x86_64 2.52-3.el7 base
[root@localhost ~]# yum -y install perl-Digest-MD5
安装xtrabackup8.0报错:
Last metadata expiration check: 0:45:15 ago on Sun 14 May 2023 09:03:49 AM CST.
Error:
Problem: conflicting requests
- nothing provides libgcrypt.so.11()(64bit) needed by percona-xtrabackup-80-8.0.4-1.el7.x86_64
- nothing provides libgcrypt.so.11(GCRYPT_1.2)(64bit) needed by percona-xtrabackup-80-8.0.4-1.el7.x86_64
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
[root@localhost opt]#
解决安装xtrabackup8.0的报错:
# 因为上面安装的是在Centos8里安装CentOS7的xtrabackup8.0
需要下载CentOS8的xtrabackup8.0
安装对应的就可以了
wget https://downloads.percona.com/downloads/Percona-XtraBackup-8.0/Percona-XtraBackup-8.0.28-21/binary/redhat/8/x86_64/percona-xtrabackup-80-8.0.28-21.1.el8.x86_64.rpm
dnf -y install percona-xtrabackup-80-8.0.28-21.1.el8.x86_64.rpm
[root@localhost ~]# xtrabackup --backup --target-dir=/data/full --datadir=/opt/mysqldata/ --user=root --password=Agan@3306
2023-05-14T14:20:12.931274+08:00 0 [Note] [MY-011825] [Xtrabackup] recognized server arguments: --datadir=/opt/mysqldata --datadir=/opt/mysqldata/
2023-05-14T14:20:12.931417+08:00 0 [Note] [MY-011825] [Xtrabackup] recognized client arguments: --backup=1 --target-dir=/data/full --user=root --password=*
xtrabackup version 8.0.28-21 based on MySQL server 8.0.28 Linux (x86_64) (revision id: 78878e9b608)
230514 14:20:13 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root' (using password: YES).
Failed to connect to MySQL server: DBI connect(';mysql_read_default_group=xtrabackup','root',...) failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) at - line 1536.
2023-05-14T14:20:13.022636+08:00 0 [Note] [MY-011825] [Xtrabackup] Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: not set
2023-05-14T14:20:13.022798+08:00 0 [ERROR] [MY-011825] [Xtrabackup] Failed to connect to MySQL server: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
解决:
# 备份不了的原因是找不到 mysql.sock
指定一下就可以了
[root@localhost ~]# xtrabackup --backup --target-dir=/data/full --datadir=/opt/mysqldata/ --user=root --password=Agan@3306 -S /tmp/mysql.sock