常用的mysqldump语句

1)mysqldump -u dba -p123456 -h172.240.54.1 -P60018 mydb tbl_test -t>tbl_test.sql

作用:只导出数据,不导出表结构。

mydb是数据库名

tbl_test是表名

-t表示只导出数据,导出结构。

 注意导出的脚本中有LOCK TABLES `tbl_test` WRITE

 

2)mysqldump -u dba -p123456 -h172.240.54.1 -P60018 mydb tbl_test>tbl_test.sql

没有-t选项,则导出的test.sql备份了结构和内容。

直接执行,会先删除表,再恢复数据。--危险

mysqldump -u dba -p123456 -h172.240.54.1 -P60018 -d mydb --tables tbl_test >tbl_test.sql

 

1 导出整个数据库结构和数据
mysqldump -h localhost -uroot -p123456 database > dump.sql

2 导出单个数据表结构和数据
mysqldump -h localhost -uroot -p123456  database table > dump.sql

注意,如果写脚本或shell,如果一旦忘写了table名,或shell脚本获得文件名为空,则备份整库。

因为语句变成了#mysqldump -A -u用户名 -p密码 数据库名>数据库备份名  

 

3 导出表格内容,但是不导出结构

mysqldump -h localhost -uroot -p123456  database table -t> dump.sql

 

4 导出整个数据库结构(不包含数据)
mysqldump -h localhost -uroot -p123456  -d database > dump.sql

注意这个d选项可不是数据库的含义,下面写法,可以减少歧义

mysqldump -h localhost -uroot -p123456  database -d > dump.sql

 

5 导出单个数据表结构(不包含数据)
mysqldump -h localhost -uroot -p123456  -d database table > dump.sql

mysqldump -h localhost -uroot -p123456  database table -d> dump.sql

 

6 导出表格,但是导出脚本不要有lock

mysqldump -h localhost -uroot -p123456  database table -t   --skip-add-locks> dump.sql

7 导出表格,纯文本格式

mysqldump -h localhost -uroot -p123456  database table -T‘/path/paht1’

文件分为.sql和.txt,放到-T指定的目录下 .txt是纯内容。就是使用select into导出的文件格式。

 

 

 

使用mysqldump --help

 常用选项:

-t, --no-create-info
Don't write table creation info.
-d, --no-data No row information.

-x, --lock-all-tables
Locks all tables across all databases. This is achieved
by taking a global read lock for the duration of the
whole dump. Automatically turns --single-transaction and
--lock-tables off.
-l, --lock-tables Lock all tables for read.
(Defaults to on; use --skip-lock-tables to disable.)默认会锁表的。

--add-drop-table Add a DROP TABLE before each create.
(Defaults to on; use --skip-add-drop-table to disable.)建表结构,默认会有drop语句

-A, --all-databases Dump all the databases. This will be same as --databases
with all databases selected.

-Y, --all-tablespaces
Dump all the tablespaces.
-y, --no-tablespaces
Do not dump any tablespace information.

-B, --databases Dump several databases. Note the difference in usage; in
this case no tables are given. All name arguments are
regarded as database names. 'USE db_name;' will be
included in the output.

  --delayed-insert    Insert rows with INSERT DELAYED.

--fields-terminated-by=name
Fields in the output file are terminated by the given
string.
--fields-enclosed-by=name
Fields in the output file are enclosed by the given
character.
--fields-optionally-enclosed-by=name
Fields in the output file are optionally enclosed by the
given character.
--fields-escaped-by=name
Fields in the output file are escaped by the given
character.

--ignore-table=name Do not dump the specified table. To specify more than one
table to ignore, use the directive multiple times, once
for each table. Each table must be specified with both
database and table names, e.g.,
--ignore-table=database.table.

  --replace           Use REPLACE INTO instead of INSERT INTO.

-T, --tab=name Create tab-separated textfile for each table to given
path. (Create .sql and .txt files.) NOTE: This only works
if mysqldump is run on the same machine as the mysqld
server.

  --tables            Overrides option --databases (-B).

  -w, --where=name    Dump only selected records. Quotes are mandatory.

 

--add-locks Add locks around INSERT statements.
(Defaults to on; use --skip-add-locks to disable.)---因为这个选项,所以在生成的脚本里的insert之前,有lock write语句

 

 

 

Posted on 2017-06-27 15:11  旅途  阅读(195)  评论(0编辑  收藏  举报