文章末尾有mysqldump的用法详解
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)
);
//向表中插入数据
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,'计算机系', '湖南省衡阳市');
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)
);
//向表中插入数据
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);
先创建backup目录,mkdir /backup
1.备份数据库school到/backup目录
[root@MySQL ~]# mysqldump -uroot -p123 school > /backup/school.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
2.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@MySQL ~]# mysqldump -uroot -p123 --add-drop-table school > /backup/droptable.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
3.直接将MySQL数据库压缩备份
[root@MySQL ~]# mysqldump -uroot -p123 school | gzip > /backup/school.sql.gz
mysqldump: [Warning] Using a password on the command line interface can be insecure.
4.备份MySQL数据库某个(些)表。此例备份student表
[root@MySQL ~]# mysqldump -uroot -p123 school student > /backup/school_student.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
5.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@MySQL ~]# mysqldump -uroot -p123 -B school db1 > /backup/moredatabase.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
6.仅仅备份数据库结构
[root@MySQL ~]# mysqldump -uroot -p123 -d school > /backup/schoolStructure.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
7.备份服务器上所有数据库
[root@MySQL ~]# mysqldump -uroot -p123 -A > /backup/alldatabase.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
8.还原MySQL数据库
//删除数据库school
[root@MySQL ~]# mysql -uroot -p123 -e 'drop database school'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@MySQL ~]# mysql -uroot -p123 -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| mysql |
| performance_schema |
| sys |
+--------------------+
//先创建数据库school,再进行恢复数据
[root@MySQL ~]# mysql -uroot -p123 -e 'create database school default character set utf8'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@MySQL ~]# mysql -uroot -p123 school < /backup/school.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
//查看数据库和表
[root@MySQL ~]# mysql -uroot -p123
mysql> use school
Database changed
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| score |
| student |
+------------------+
2 rows in set (0.00 sec)
9.还原压缩的MySQL数据库
[root@MySQL ~]# zcat school.sql.gz | mysql -uroot -p123 school
mysql: [Warning] Using a password on the command line interface can be insecure.
1.备份的时候,如果不写备份的目录,会备份到当前文件夹下;
2.备份数据库
mysqldump -uroot -pPassword [database name] [table name] > [dump patch/file name]
eg.
备份数据库school下的表student:
mysqldump -uroot -p123 school student > /backup/school.sql
3.导出结构不导出数据
mysqldump -uroot -pPassword -d [database name] > [dump patch/file name]
4.导出数据不导出结构
mysqldump -uroot -pPassword -t [database name] > [dump patch/file name]
5.导出特定的数据库
mysqldump -uroot -pPassword -B [database name] > [dump patch/file name]
6.导出数据库并删除表
mysqldump -uroot -pPassword --add-drop-table [database name] > [dump patch/file name]
7.备份全部数据库
mysqldump -uroot -pPassword -A [database name] > [dump patch/file name]
8.导入数据
mysql -uroot -p123 [database name] < [dump file]
zcat **.sql.gz | mysql -uroot -p123 [database name]
使用zcat导入数据时,如果使用参数-B备份数据库,后面可不用加上数据库名,反之,需先创建该数据库,再加上数据库名字。
9.使用source导入数据
mysql -uroot -p123 -e 'source /backup/school.sql'
10.参数详解
–all-databases , -A
导出全部数据库
–databases, -B
导出几个数据库。参数后面所有名字参量都被看作数据库名。
–all-tablespaces , -Y
导出全部表空间
–no-tablespaces , -y
不导出任何表空间信息
–add-drop-database
每个数据库创建之前添加drop数据库语句
–add-drop-table
每个数据表创建之前添加drop数据表语句。(默认为打开状态,使用–skip-add-drop-table取消选项)
–add-locks
在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(默认为打开状态,使用–skip-add-locks取消选项)