文章目录
MySQL备份
准备工作
创建数据库
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(50),
address VARCHAR(50)
);
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)
);
插入数据
[student]
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 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);
要求:
备份数据库school到/backup目录
[root@serverb ~]$ mysql -uroot -p'SYCredhat123' --default-character-set=utf8 -B school > /tmp/mysql_backups/school.$(date +%F).sql
[root@serverb ~]# ll /tmp/mysql_backups/
total 0
-rw-rw-r--. 1 redhat redhat 0 Sep 9 16:59 school.2022-09-09.sql
备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
root@serverb mysql_backups]# mysqldump --add-drop-table -uroot -p'SYCredhat123' school > /tmp/mysql_backups/school.sql
[root@serverb mysql_backups]# ll
total 20
-rw-r--r--. 1 root root 0 Sep 9 17:32 all_backupfile.sql
-rw-rw-r--. 1 redhat redhat 0 Sep 9 16:59 school.2022-09-09.sql
-rw-r--r--. 1 root root 4047 Sep 9 17:20 school+school_2.2022-09-09.sql
-rw-r--r--. 1 root root 3625 Sep 9 17:34 school.sql
-rw-r--r--. 1 root root 130 Sep 9 17:11 school.sql.gz
-rw-r--r--. 1 root root 2630 Sep 9 17:23 structure_school.2022-09-09.sql
-rw-r--r--. 1 root root 2684 Sep 9 17:15 tb_student.2022-09-09.sql
[root@serverb mysql_backups]#
直接将MySQL数据库压缩备份
[root@serverb ~]# mysqldump | gzip > /tmp/mysql_backups/school.sql.gz
[root@serverb ~]# ll /tmp/mysql_backups/
total 4
-rw-rw-r--. 1 redhat redhat 0 Sep 9 16:59 school.2022-09-09.sql
-rw-r--r--. 1 root root 130 Sep 9 17:11 school.sql.gz
备份MySQL数据库某个(些)表。此例备份student表
[root@serverb ~]# mysqldump -uroot -p'SYCredhat123' school student > /tmp/mysql_backups/tb_student.$(date +%F).sql
[root@serverb ~]# ll /tmp/mysql_backups/
total 8
-rw-rw-r--. 1 redhat redhat 0 Sep 9 16:59 school.2022-09-09.sql
-rw-r--r--. 1 root root 130 Sep 9 17:11 school.sql.gz
-rw-r--r--. 1 root root 2684 Sep 9 17:15 tb_student.2022-09-09.sql
同时备份多个MySQL数据库(其他数据库素材自行准备)
CREATE DATABASE school_2;
[root@serverb ~]# mysqldump -uroot -p'SYCredhat123' -B school school_2 > /tmp/mysql_backups/school+school_2.$(date +%F).sql
# -B 也可以用 --databases
[root@serverb ~]# ll /tmp/mysql_backups/
total 12
-rw-rw-r--. 1 redhat redhat 0 Sep 9 16:59 school.2022-09-09.sql
-rw-r--r--. 1 root root 4047 Sep 9 17:20 school+school_2.2022-09-09.sql
-rw-r--r--. 1 root root 130 Sep 9 17:11 school.sql.gz
-rw-r--r--. 1 root root 2684 Sep 9 17:15 tb_student.2022-09-09.sql
仅仅备份数据库结构
[root@serverb ~]# mysqldump -uroot -p'SYCredhat123' -d school > /tmp/mysql_backups/structure_school.$(date +%F).sql
# -d 只备份库结构,不包含数据内容
[root@serverb ~]# ll /tmp/mysql_backups/
total 16
-rw-rw-r--. 1 redhat redhat 0 Sep 9 16:59 school.2022-09-09.sql
-rw-r--r--. 1 root root 4047 Sep 9 17:20 school+school_2.2022-09-09.sql
-rw-r--r--. 1 root root 130 Sep 9 17:11 school.sql.gz
-rw-r--r--. 1 root root 2630 Sep 9 17:23 structure_school.2022-09-09.sql
-rw-r--r--. 1 root root 2684 Sep 9 17:15 tb_student.2022-09-09.sql
[root@serverb ~]#
备份服务器上所有数据库
[root@serverb mysql_backups]# mysqldump --all-databases > /tmp/mysql_backups/all_backupfile.sql
[root@serverb mysql_backups]# ll
total 16
-rw-r--r--. 1 root root 0 Sep 9 17:32 all_backupfile.sql
-rw-rw-r--. 1 redhat redhat 0 Sep 9 16:59 school.2022-09-09.sql
-rw-r--r--. 1 root root 4047 Sep 9 17:20 school+school_2.2022-09-09.sql
-rw-r--r--. 1 root root 130 Sep 9 17:11 school.sql.gz
-rw-r--r--. 1 root root 2630 Sep 9 17:23 structure_school.2022-09-09.sql
-rw-r--r--. 1 root root 2684 Sep 9 17:15 tb_student.2022-09-09.sql
[root@serverb mysql_backups]#