1.创建student和score表
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)
) charset=utf8;
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)
) charset=utf8;
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);
3.备份数据库school到/backup目录
[root@localhost ~]# mysqldump -uroot -p'RedHat@123' -B school > /backup/dbschool.sql
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
(向school库中的score表随意添加一个数据用来测试)
[root@localhost ~]# mysqldump --add-drop-table -uroot -p'RedHat@123' school > /backup/adt_sch.sql
[root@localhost ~]# mysql -uroot -p'RedHat@123' school < /backup/adt_sch.sql
[root@localhost ~]# mysqldump -uroot -p'RedHat@123' school | gzip > /backup/zip.sql.gz
6.备份MySQL数据库某个(些)表。此例备份student表
[root@localhost ~]# mysqldump -hlocalhost -uroot -p'RedHat@123' school student > /backup/stu.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@localhost ~]# mysqldump -uroot -p'RedHat@123' --databases school test > /backup/dbs.sql
[root@localhost ~]# mysqldump -uroot -p'RedHat@123' -d school > /backup/sch.sql
[root@localhost ~]# mysqldump -uroot -p'RedHat@123' --all-databases > /backup/all.sql
[root@localhost ~]# mysqldump -uroot -p'RedHat@123' school > /backup/dbschool.sql
11.还原压缩的MySQL数据库
[root@localhost ~]# gunzip < /backup/zip.sql.gz | mysql -uroot -p'RedHat@123' school
12.使用xtrabackup 备份数据库
[root@localhost ~]# xtrabackup --backup --user=root --password='RedHat@123' --target-dir=/backup/xtr
[root@localhost ~]# xtrabackup --prepare --target-dir=/backup/xtr
14.使用mydumper备份数据库
[root@localhost ~]# mydumper -u root -p 'RedHat@123' -G -R -E -B school -o /mysqlbackup/
[root@localhost ~]# myloader -u root -p 'RedHat@123' -d /mysqlbackup/ -o -B school