数据库备份,数据库为school
1.建表
创建student和score表
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)
);
创建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表增加记录
向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,'计算机系', '湖南省衡阳市');
向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);
mysql8.0 [school]>mysql8.0 [school]>select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 王大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 王二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.01 sec)
mysql8.0 [school]>select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name | grade |
+----+--------+-----------+-------+
| 1 | 901 | 计算机 | 98 |
| 2 | 901 | 英语 | 80 |
| 3 | 902 | 计算机 | 65 |
| 4 | 902 | 中文 | 88 |
| 5 | 903 | 中文 | 95 |
| 6 | 904 | 计算机 | 70 |
| 7 | 904 | 英语 | 92 |
| 8 | 905 | 英语 | 94 |
| 9 | 906 | 计算机 | 90 |
| 10 | 906 | 英语 | 85 |
+----+--------+-----------+-------+
10 rows in set (0.00 sec)
3.备份数据库school到/backup目录
[root@localhost backup]# mysqldump -uroot -p school > /backup/school.sql
Enter password:
[root@localhost backup]# ll
total 4
-rw-r--r-- 1 root root 3233 Jul 20 17:11 school.sql
[root@localhost backup]# cat school.sql
-- MySQL dump 10.13 Distrib 8.0.26, for Linux (x86_64)
--
-- Host: localhost Database: school
-- ------------------------------------------------------
-- Server version 8.0.26
/*!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 `score`
--
DROP TABLE IF EXISTS `score`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `score` (
`id` int NOT NULL DEFAULT '0',
`stu_id` int NOT NULL,
`c_name` varchar(20) DEFAULT NULL,
`grade` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `score`
--
LOCK TABLES `score` WRITE;
/*!40000 ALTER TABLE `score` DISABLE KEYS */;
INSERT INTO `score` VALUES (1,901,'计算机',98),(2,901,'英语',80),(3,902,'计算机',65),(4,902,'中文',88),(5,903,'中文',95),(6,904,'计算机',70),(7,904,'英语',92),(8,905,'英语',94),(9,906,'计算机',90),(10,906,'英语',85);
/*!40000 ALTER TABLE `score` ENABLE KEYS */;
UNLOCK TABLES;
--
-- 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
) 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 2023-07-20 17:11:39
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@localhost backup]# mysqldump --add-drop-table -uroot -p school > /backup/schooldrop.sql
Enter password:
[root@localhost backup]# ll
total 8
-rw-r--r-- 1 root root 3233 Jul 20 17:18 schooldrop.sql
-rw-r--r-- 1 root root 3233 Jul 20 17:11 school.sql
5.直接将MySQL数据库压缩备份
[root@localhost backup]# mysqldump -uroot -p school | gzip > /backup/schoolgzip.sql.gz
Enter password:
[root@localhost backup]# ll
total 12
-rw-r--r-- 1 root root 3233 Jul 20 17:18 schooldrop.sql
-rw-r--r-- 1 root root 1135 Jul 20 17:21 schoolgzip.sql.gz
-rw-r--r-- 1 root root 3233 Jul 20 17:11 school.sql
6.备份MySQL数据库某个(些)表。此例备份student表
[root@localhost backup]# mysqldump -uroot -p school student > /backup/school.student.sql
Enter password:
[root@localhost backup]# ll
total 16
-rw-r--r-- 1 root root 3233 Jul 20 17:18 schooldrop.sql
-rw-r--r-- 1 root root 1135 Jul 20 17:21 schoolgzip.sql.gz
-rw-r--r-- 1 root root 3233 Jul 20 17:11 school.sql
-rw-r--r-- 1 root root 2361 Jul 20 17:22 school.student.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@localhost backup]# mysqldump -uroot -p -B school SLB > /backup/muldatabase.sql
Enter password:
[root@localhost backup]# ll
total 24
-rw-r--r-- 1 root root 4353 Jul 20 17:28 muldatabase.sql
-rw-r--r-- 1 root root 3233 Jul 20 17:18 schooldrop.sql
-rw-r--r-- 1 root root 1135 Jul 20 17:21 schoolgzip.sql.gz
-rw-r--r-- 1 root root 3233 Jul 20 17:11 school.sql
-rw-r--r-- 1 root root 2361 Jul 20 17:22 school.student.sql
8.仅仅备份数据库结构
[root@localhost backup]# mysqldump -uroot -p school > /backup/descschool.sql
Enter password:
9.备份服务器上所有数据库
[root@localhost backup]# mysqldump -uroot -p -A > /backup/alldatabase.sql
Enter password:
10.还原MySQL数据库
[root@localhost backup]# mysql -uroot -p school < /backup/school.sql
Enter password:
11.还原压缩的MySQL数据库
[root@localhost backup]# gunzip < /backup/schoolgzip.sql.gz | mysql -uroot -p school
Enter password:
12.使用xtrabackup 备份数据库
[root@localhost ~]# yum localinstall -y percona-xtrabackup-80-8.0.27-19.1.el8.x86_64.rpm
[root@localhost ~]# mysql --version
mysql Ver 8.0.26 for Linux on x86_64 (Source distribution)
[root@localhost backupSLB]# xtrabackup -uroot -pSLBmysql@ --backup --target-dir=/backup/backupSLB
13.在另外的数据库服务器上还原xtrabackup 备份
[root@localhost backupdata]# scp -r [email protected]:/backup/backupSLB1 /backupdata
[email protected]'s password:
[root@localhost ~]# xtrabackup --copy-back --target-di
14.使用mydumper备份数据库
[root@localhost backupSLB]# pwd
/backup/backupSLB
[root@localhost backupSLB]# mydumper -u root -p SLBmysql@ -B SLB -o /backup/backupSLB/
[root@localhost backupSLB]# ll
total 16
-rw-r--r-- 1 root root 135 Jul 20 23:46 metadata
-rw-r--r-- 1 root root 198 Jul 20 23:46 SLB.dom-schema.sql
-rw-r--r-- 1 root root 133 Jul 20 23:46 SLB.dom.sql
-rw-r--r-- 1 root root 127 Jul 20 23:46 SLB-schema-create.sql
15.使用mydumper恢复数据库
[root@localhost backupSLB]# pwd
/backup/backupSLB
[root@localhost backupSLB]# mydumper -u root -p SLBmysql@ -B SLB -o /backup/backupSLB/
[root@localhost backupSLB]# ll
total 16
-rw-r--r-- 1 root root 135 Jul 20 23:46 metadata
-rw-r--r-- 1 root root 198 Jul 20 23:46 SLB.dom-schema.sql
-rw-r--r-- 1 root root 133 Jul 20 23:46 SLB.dom.sql
-rw-r--r-- 1 root root 127 Jul 20 23:46 SLB-schema-create.sql