MySQL全量备份的实现
一、实验素材
1.创建student和score表
(1) student表
create database school; use school CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY , name VARCHAR(20) NOT NULL , sex VARCHAR(4) , birth YEAR, department VARCHAR(20) , address VARCHAR(50) );
(2) score表
CREATE TABLE score ( id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT , stu_id INT(10) NOT NULL , c_name VARCHAR(20) , grade INT(10) );
2.为student表和score表增加记录
(1) 向student表插入记录的INSERT语句如下
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区'); INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区'); INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市'); INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市'); INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市'); INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
(2) 向score表插入记录的INSERT语句如下
INSERT INTO score VALUES(NULL,901, '计算机',98); INSERT INTO score VALUES(NULL,901, '英语', 80); INSERT INTO score VALUES(NULL,902, '计算机',65); INSERT INTO score VALUES(NULL,902, '中文',88); INSERT INTO score VALUES(NULL,903, '中文',95); INSERT INTO score VALUES(NULL,904, '计算机',70); INSERT INTO score VALUES(NULL,904, '英语',92); INSERT INTO score VALUES(NULL,905, '英语',94); INSERT INTO score VALUES(NULL,906, '计算机',90); INSERT INTO score VALUES(NULL,906, '英语',85);
二、备份习题
注意:如果感觉每次输密码麻烦
[root@node7 ~]# echo 'export MYSQL_PWD=MySQL@123' >> ~/.bashrc #将密码写入环境变量 [root@node7 ~]# source ~/.bashrc
1、备份数据库school到/backup目录
[root@node7 ~]# mkdir /backup [root@node7 ~]# mysqldump -B school > /backup/school1_date +%F.sql [root@node7 ~]# ll /backup/ total 4 -rw-r–r-- 1 root root 3558 Feb 19 16:51 school_2024-02-19.sql [root@node7 ~]# vim /backup/school1_2024-02-19.sql #可以看到内容 [root@node7 ~]# grep -i drop /backup/school1_2024-02-19.sql DROP TABLE IF EXISTS score; DROP TABLE IF EXISTS student;
注意:在MySQL8.0版本中默认有–add-drop-table这个选项
2、备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
--add-drop-table
如果表存在,它将被删除,然后新的表会被创建
[root@node7 ~]# mysqldump --add-drop-table school > /backup/school_drop_date +%F.sql #恢复时,如果表存在,它将被删除,然后新的表会被创建 [root@node7 ~]# vim /backup/school_drop_2024-02-19.sql [root@node7 ~]# grep -i drop /backup/school_drop_2024-02-19.sql DROP TABLE IF EXISTS score; DROP TABLE IF EXISTS student;
注意:在MySQL8.0版本中默认有–add-drop-table这个选项,所以加不加都一样。
3、直接将MySQL数据库压缩备份
[root@node7 ~]# mysqldump -B school | gzip > /backup/school2_date +%F.sql [root@node7 ~]# vim /backup/school2_2024-02-19.sql [root@node7 ~]# zcat /backup/school2_2024-02-19.sql
4、备份MySQL数据库某个(些)表。此例备份student表
[root@node7 ~]# mysqldump school student > /backup/school3_date +%F.sql [root@node7 ~]# cat /backup/school3_2024-02-19.sql #只有student表的数据
MySQL dump 10.13 Distrib 8.0.36, for Linux (x86_64) -- -- Host: localhost Database: school -- ------------------------------------------------------ -- Server version 8.0.36 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!50503 SET NAMES utf8mb4 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `student` -- DROP TABLE IF EXISTS `student`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `student` ( `id` int NOT NULL, `name` varchar(20) NOT NULL, `sex` varchar(4) DEFAULT NULL, `birth` year DEFAULT NULL, `department` varchar(20) DEFAULT NULL, `address` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `student` -- LOCK TABLES `student` WRITE; /*!40000 ALTER TABLE `student` DISABLE KEYS */; INSERT INTO `student` VALUES (901,'张老大','男',1985,'计算机系','北京市海淀区'),(902,'张老二','男',1986,'中文系','北京市昌平区'),(903,'张三','女',1990,'中文系','湖南省永州市'),(904,'李四','男',1990,'英语系','辽宁省阜新市'),(905,'王五','女',1991,'英语系','福建省厦门市'),(906,'王六','男',1988,'计算机系','湖南省衡阳市'); /*!40000 ALTER TABLE `student` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2024-02-19 17:21:31
5、同时备份多个MySQL数据库(其他数据库素材自行准备)
(1) 建表
create database db1; use db1 CREATE TABLE `stu` ( `Sno` int(10) NOT NULL COMMENT '学号', `Sname` varchar(16) NOT NULL COMMENT '姓名', `Ssex` char(2) NOT NULL COMMENT '性别', `Sage` tinyint(2) NOT NULL DEFAULT '0' COMMENT '学生年龄', `Sdept` varchar(16) DEFAULT 'NULL' COMMENT '学生所在系别', PRIMARY KEY (`Sno`) );
(2) 插入数据
INSERT INTO `stu` VALUES (1, '陆亚', '男', 24, '计算机网络'),(2, 'tom', '男', 26, '英语'),(3, '张阳', '男', 21, '物流管理'), (4, 'alex', '女', 22, '电子商务');
(3)备份-B 或 --databases
用于备份多个数据库
[root@node7 ~]# mysqldump --databases school db1 > /backup/school4_date +%F.sql [root@node7 ~]# grep school /backup/school4_2024-02-19.sql – Host: localhost Database: school – Current Database: school CREATE DATABASE /!32312 IF NOT EXISTS/ school /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci / /!80016 DEFAULT ENCRYPTION=‘N' */; USE school; [root@node7 ~]# grep db1 /backup/school4_2024-02-19.sql – Current Database: db1 CREATE DATABASE /!32312 IF NOT EXISTS/ db1 /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci / /!80016 DEFAULT ENCRYPTION=‘N' */; USE db1;
6、仅仅备份数据库结构
-d
:指定只导出数据库结构而不包含数据
[root@node7 ~]# mysqldump -d school > /backup/school5_date +%F.sql [root@node7 ~]# grep -i insert /backup/school5_2024-02-19.sql #没有insert语句 [root@node7 ~]# echo $?
7、备份服务器上所有数据库
–all-databases 指所有数据库
[root@node7 ~]# mysqldump --all-databases > /backup/school6_date +%F.sql [root@node7 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | db1 | | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+
[root@node7 ~]# grep -i ‘use db1' /backup/school6_2024-02-19.sql USE db1; [root@node7 ~]# grep -i ‘use mysql' /backup/school6_2024-02-19.sql USE mysql; [root@node7 ~]# grep -i ‘use school' /backup/school6_2024-02-19.sql USE school;
注意:information_schema MySQL的内部状态和配置信息、performance_schema服务器资源的使用情况、sys视图,这三个数据库不会备份
8、还原MySQL数据库
[root@node7 ~]# mysql -e 'drop database school' [root@node7 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | db1 | | information_schema | | mysql | | performance_schema | | sys | +--------------------+
#mysql命令恢复 [root@node7 ~]# mysql < /backup/school1_2024-02-19.sql [root@node7 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | db1 | | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+
#source恢复 [root@node7 ~]# mysql -e 'drop database school' [root@node7 ~]# mysql -e 'source /backup/school1_2024-02-19.sql' [root@node7 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | db1 | | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+
9、还原压缩的MySQL数据库
[root@node7 ~]# mysqldump -B school | gzip > /backup/school2_date +%F.sql [root@node7 ~]# mysql -e 'drop database school' [root@node7 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | db1 | | information_schema | | mysql | | performance_schema | | sys | +--------------------+
[root@node7 ~]# zcat /backup/school2_2024-02-19.sql | mysql [root@node7 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | db1 | | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+
10、使用mydumper备份数据库
mydumper安装部署
[root@node7 ~]# wget -c https://github.com/mydumper/mydumper/releases/download/v0.15.1-3/mydumper-0.15.1-3.el8.x86_64.rpm [root@node7 ~]# yum install mydumper-0.15.1-3.el8.x86_64.rpm [root@node6 ~]# mydumper -B school -o /backup/ [root@node6 ~]# ll /backup/
total 32 drwx------ 2 root root 4096 Feb 19 19:01 2024-02-19_19-01-26 drwx------ 2 root root 4096 Feb 19 19:01 2024-02-19_19-01-44 -rw-r--r-- 1 root root 323 Feb 19 19:13 metadata -rw-r--r-- 1 root root 155 Feb 19 19:13 school-schema-create.sql -rw-r--r-- 1 root root 0 Feb 19 19:13 school-schema-triggers.sql -rw-r--r-- 1 root root 352 Feb 19 19:13 school.score.00000.sql -rw-r--r-- 1 root root 384 Feb 19 19:13 school.score-schema.sql -rw-r--r-- 1 root root 503 Feb 19 19:13 school.student.00000.sql -rw-r--r-- 1 root root 435 Feb 19 19:13 school.student-schema.sql
11、使用mydumper恢复数据库
[root@node6 ~]# mysql -e 'drop database school' [root@node6 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+
[root@node6 ~]# myloader -d /backup/ -o -B school [root@node6 ~]# mysql -e 'show databases'
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+
到此这篇关于MySQL全量备份的实现的文章就介绍到这了,更多相关MySQL全量备份内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
最新评论