素材
数据库名为school ,student和score表
创建数据库school
create database school;
创建student表
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)
);
增加数据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表数据
NSERT 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);
练习
3.备份数据库school到/backup目录
mysqldump -uroot -p123456 -B school > /root/backup/school.sql
mysql -uroot -p123456 < school.sql
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
mysqldump -uroot -p123456 -B school > /root/backup/school.sql
mysql -uroot -p123456 < school.sql
5.直接将MySQL数据库压缩备份
mysqldump -uroot -p123456 -B school |gzip > /root/backup/school_1.sql.gz
-rw-r--r-- 1 root root 1214 Mar 23 06:01 school_1.sql.gz
6.备份MySQL数据库某个(些)表。此例备份student表
mysqldump -uroot -p123456 school student > /root/backup/school_student.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
mysqldump -uroot -p123456 -B school hzit > /root/backup/school_hzit.sql
8.仅仅备份数据库结构
mysqldump -uroot -p123456 -d school > /root/backup/school_d.sql
9.备份服务器上所有数据库
mysqldump -uroot -p123456 -A > /root/backup/all.sql
10.还原MySQL数据库
在这里插入代码片mysql -uroot -p123456 < school.sql
11.还原压缩的MySQL数据库
zcat school_1.sql.gz | mysql -uroot -p123456
12.使用xtrabackup 备份数据库
13.在另外的数据库服务器上还原xtrabackup 备份
14.使用mydumper备份数据库
mydumper -u root -p 123456 -B school -S /tmp/mysql.sock -o /root/db
[root@localhost ~]# tree db/
db/
├── metadata
├── school-schema-create.sql
├── school.score-schema.sql
├── school.score.sql
├── school.student-schema.sql
└── school.student.sql
0 directories, 6 files
15.使用mydumper恢复数据库
myloader -u root -p 123456 -S /tmp/mysql.sock -d /root/db/ -B school