一.环境准备
数据库备份,数据库为school,素材如下
>create database school;
>use school1.创建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表。SQL代码如下: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);
二.备份数据库
2.1.备份数据库school到/backup目录
2.1.1.mysqldump+binlog
[root@server ~]# mkdir -pv /backup
mkdir: 已创建目录 '/backup'
[root@server ~]# mysqldump --opt -B school > /backup/school.sql
[root@server ~]# cd /backup
[root@server backup]# ll
总用量 4.0K
-rw-r--r-- 1 root root 3.5K 3月 8 10:38 school.sql
2.2.备份数据库为带删除表的格式,可以覆盖已有数据库,而不需要手动删除原有数据库
[root@server backup]# mysqldump --add-drop-table -B school > /backup/school1.sql
[root@server backup]# ll
总用量 8.0K
-rw-r--r-- 1 root root 3.5K 3月 8 12:08 school1.sql
-rw-r--r-- 1 root root 3.5K 3月 8 10:38 school.sql
2.3.直接将数据库压缩备份
[root@server backup]# mysqldump --opt -B school | gzip > /backup/school2.sql.gz
[root@server backup]# ll
总用量 12K
-rw-r--r-- 1 root root 3.5K 3月 8 12:08 school1.sql
-rw-r--r-- 1 root root 1.3K 3月 8 12:15 school2.sql.gz
-rw-r--r-- 1 root root 3.5K 3月 8 10:38 school.sql