MySQL备份恢复作业
数据库备份,数据库为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表。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);
3.备份数据库school到/backup目录 (此处备份数据库恢复需要先创建数据库)
[root@localhost ~]
[root@localhost ~]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]
total 4
-rw-r--r-- 1 root root 3300 May 4 06:17 s1.sql
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@localhost ~]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]
-rw-r--r-- 1 root root 3443 May 4 06:26 /backup/s2.sql
[root@localhost ~]
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `school` /*!40100 DEFAULT CHARACTER SET utf8 */;
CREATE TABLE `score` (
CREATE TABLE `student` (
5.直接将MySQL数据库压缩备份
[root@localhost ~]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
6.备份MySQL数据库某个(些)表。此例备份student表
[root@localhost ~]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]
-rw-r--r-- 1 root root 2380 May 5 07:07 /backup/s4.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@localhost ~]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
8.仅仅备份数据库结构
[root@localhost ~]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
9.备份服务器上所有数据库
[root@localhost ~]
mysqldump: [Warning] Using a password on the command line interface can be insecure.
10.还原MySQL数据库 :还原数据库有两种方法
mysql> source /backup/s2.sql /第一种方法
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> source /backup/s2.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> select * from school.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)
第二种方法:
[root@localhost ~]
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
6 rows in set (0.00 sec)
11.还原压缩的MySQL数据库
使用 zcat 加管道符连接
[root@localhost ~]
mysql: [Warning] Using a password on the command line interface can be insecure.
12.使用xtrabackup 备份数据库
[root@localhost ~]
13.在另外的数据库服务器上还原xtrabackup 备份
[root@localhost ~]
进行备份操作
[root@localhost ~]
14.使用mydumper备份数据库
[root@localhost ~]
15.使用mydumper恢复数据库
[root@localhost ~]
|