Article directory
xtrabackup of MySQL backup tool
1. Introduction to xtrabackup
Xtrabackup is a tool for data backup of InnoDB, supports online hot backup (does not affect data reading and writing during backup), and is a good substitute for the commercial backup tool InnoDB Hotbackup.
Brief introduction:
Xtrabackup2.4 has two main tools: xtrabackup , innobackupex
(1) xtrabackup can only back up InnoDB and XtraDB data tables, but not MyISAM data tables
(2) innobackupex encapsulates xtrabackup, which is a script package, so innodb and myisam can be backed up and processed at the same time, but a read lock needs to be added when processing myisam (recommended)
xtrabackup common tool description
There are two main tools in xtrabackup
xtrabackup: It is a tool for hot backup of data in innodb and xtradb tables. It cannot back up other types of tables, nor can it back up the data table structure.
innobackupex: It is a perl script that encapsulates xtrabackup, providing the ability to back up myisam tables.
常用选项:
--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 version compatibility
Percona -xtraBackup 8.0 is a new version of Percona XtraBackup for MySQL8.0, mainly because MySQL8.0 has new improvements in Redo and database dictionary.
XtraBackup 2.4 continues to support MySQL 5.6 and 5.7
(1) xtrabackup 8.0 removes the innobackupex command;
(2) Due to the new MySQL redo log and data dictionary format, version 8.0 only supports mysql8.0 and percona8.0;
(3) Versions earlier than mysql8.0 need to use xtrabackup2.4 to backup and restore .
MySQLdump
Compare with xtrabackup
backup
Xtrabackup is a free database hot backup software open sourced by percona. It can back up InnoDB databases and XtraDB storage engine databases non-blockingly (table locks are also required for MyISAM backups);
The mysqldump backup method is a logical backup. Its biggest defect is that the backup and recovery speed is slow. If the data is larger than 50G, mysqldump backup is not suitable . (To put it bluntly, if the amount of data is large, use xtrabackup to back up the data)
2. Advantages of Xtrabackup
1) Fast backup speed and reliable physical backup
2) The backup process will not interrupt the ongoing transaction (no need to lock the table)
3) Can save disk space and traffic based on functions such as compression
4) Automatic backup verification
5) Fast recovery speed
6) You can stream the backup to another machine
7) Backup data without increasing server load
3. Xtrabackup backup principle
When the backup starts, a background detection process will first be started to detect mysql redo
changes in real time. Once a new log is found to be written, the log will be recorded in the background log file immediately xtrabackup_log
, and then the data file of InnoDB is copied to the system table space file ibdatax
. After the copy is completed , will execute flush tables with readlock
, then copy .frm MYI MYD
etc. files, finally execute unlock tables
, and finally stop xtrabackup_log
.
Use xtrabackup2.4 to back up mysql5.7.x, xtrabackup8.0 to back up mysql8.0.x
Environmental preparation
operating system | IP | application |
---|---|---|
CentOS7/8 | 192.168.229.10 | mysql5.7.x xtrabackup2.4 |
CentOS7/8 | 192.168.229.170 | mysql8.0.x xtrabackup8.0 |
Both must close the firewall and SElinux
turn off firewall
close SElinux
Two, install mysql5.7.x
There are two ways to install mysql, binary installation and yum installation.
1. The way to install mysql5.7.x by yum
Download the rpm path of mysql
防火墙和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; # 退出
illustrate:
2. Download xtrabackup2.4 version
Or you can copy the URL to download locally
# 使用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 backs up mysql5.7.x data
3.1, innobackupex full backup
xtrabackupofficial documentation
[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. Simulate data loss, and then use full backup recovery
- Stop the database service systemctl stop mysqld
- To delete data files and log files (you can also move mv to other places, just make sure to clear the mysql data storage directory)
- Note: You need to execute innobackupex --apply-log before restoring, and then make the --copy-back data consistent.
- Restart the database service
[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 incremental backup
pay attention:
innobackupex incremental backup is only for transaction-supporting engines such as InnoDB, and it is still full backup for engines such as MyISAM.
Incremental backups need to be based on full backups.
**I have made a full backup before: (/data/2023-05-10_11-09-34/) The first incremental backup needs to be done on the basis of the full backup. **
# 模拟有数据在增加
[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. Simulate misoperation, delete the database: test and student, and then use incremental recovery
# 模拟误操作,删除数据库: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. How to install mysql8.0.x binary
Use xftp to transfer the mysql binary package to the system
# 创建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. Download xtrabackup8.0 version
5.1, CentOS7 installation 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, install xtrabackup8.0 on CentOS8 (here use CentOS8 system to demonstrate)
[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 full backup mysql8.0.28, and recovery.
[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 incremental backup and recovery
Incremental backups are backed up on the basis of full backups. (full backup: /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 ~]#
Error message:
Log in to mysql and report an error:
[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
solve:
[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
Install xtrabackup2.4 error problem
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)
solve
// 安装这个包
[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
Backup data error:
[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.
solve:
[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
Install xtrabackup8.0 error:
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]#
Solve the error of installing 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)
solve:
# 备份不了的原因是找不到 mysql.sock
指定一下就可以了
[root@localhost ~]# xtrabackup --backup --target-dir=/data/full --datadir=/opt/mysqldata/ --user=root --password=Agan@3306 -S /tmp/mysql.sock