设为首页 加入收藏

TOP

MySQL 命令行工具之 mysqldump 深入研究(一)
2015-11-12 21:29:16 来源: 作者: 【 】 浏览:12
Tags:MySQL 命令 工具 mysqldump 深入 研究

mysqldump 是MySQL的一个命令行工具,用于逻辑备份。可以将数据库和表的结构,以及表中的数据分别导出成:create database, create table, insert into的sql语句。当然也可以导出 存储过程,触发器,函数,调度事件(events)。不管是程序员,还是DBA都会经常使用的一个工具。


1. mysqldump --help


mysqldump 的选项很多,我们看一下他的帮助文档:


总体看来,帮助文档分成了两个部分。前一个部分是对各种选项的说明,后一个部分是mysqldump的各种选项的默认值。


mysqldump拥有超多的选项,这样说明它很灵活,功能强大。为了好理解,可以将他的众多的选项和参数,根据他们的功能分为几个类别来介绍。


在此之前,我们还要搞清楚mysqldump的所有的默认选项的值,它们的含义。


2. mysqldump 默认选项:


这些默认值,有的是 false, 有的是 true,有的没有默认值。


我们要关注的是 默认为 true,也就是 mysqldump 默认给我打开的选择,它们的具体含义如下


如果要关闭这些打开的默认选项时,需要明确指定:mysqldump -uxxx -p aazj --extended-insert=0 Users > Users.sql; =0 表示关闭选项。


上面这些默认选项,最好结合一个例子来理解:


2. mysqldump 登录服务器的相关选项


Default options are read from the following files in the given order:


/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf



The following groups are read: mysqldump client
The following options may be given as the first argument:
--no-defaults? ? ? ? ? Don't read default options from any option file, except for login file.
--defaults-file=#? ? ? Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=# Also read groups with concat(group, suffix)
--login-path=#? ? ? ? ? Read this path from the login file.


1)mysqldump 作为一个客户端工具,它会去上诉目录中寻找 my.cnf 文件,然后读取该文件中 [mysqldump] 和 [client] 下面的选项


2)那些 defaults 相关的选项都是为了另外指定 配置文件和登录文件,极少使用;


? -u, --user=name? ? User for login if not current user.


? -p, --password[=name] Password to use when connecting to server. If password is not given it's solicited on the tty.


? -h, --host=name? ? Connect to host.


? -P, --port=#? ? ? ? Port number to use for connection.
? --protocol=name? ? The protocol to use for connection (tcp, socket, pipe, memory).


? --max-allowed-packet=#? The maximum packet length to send to or receive from server.
? --net-buffer-length=#? ? ? The buffer size for TCP/IP and socket communication.


3)这几个选项指定 登录的用户名,密码,mysqld IP地址,端口,连接使用的协议等等。


? ? 一般常用的是 -h192.168.2.xx -uxxx -p ,如果mysqld默认端口不是3306,则需要使用 -Pxxx 指定端口.


? ? --max-allowed-packet 我们一般配置在my.cnf中。--net-buffer-length 是为了优化网络连接的socket buffer.


使用示例: mysqldump -h192.168.1.20 -uxxx -p -P3057


3. mysqldump 选择备份内容的相关选项


我们可以选择备份所有数据库,某几个数据库,某一个数据库,某一个数据库中的某几个表,某一个数据库中的一个表;


可以选择是否备份 存储过程和函数,触发器,调度事件.


1)选择导出的数据库 和 表:


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


-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.


database [tables]? 导出数据库?database 中的表结构 和 表中数据;


2)选择是否导出 建库,建表语句,是否导出 表中的数据:


-n, --no-create-db? Suppress the CREATE DATABASE ... IF NOT EXISTS statement that normally is output for each dumped database if?


? ? ? ? ? ? ? ? ? ? ? ? ? ? --all-databases or --databases is given. (不导出建库语句: CREATE DATABASE,也就是不导库结构)


-t, --no-create-info?Don't write table creation info. (不导出建表语句)


-d, --no-data? ? ? ? No row information. (不导出数据,有时我们仅仅需要导出表结构,也就是建表语句就行了)


3)选择是否导出 存储过程和函数,触发器,调度事件:


-R, --routines? ? ? Dump stored routines (functions and procedures). (导出存储过程和函数)


--triggers? ? ? ? ? ? Dump triggers for each dumped table. (Defaults to on; use --skip-triggers to disable.) (导出触发器)


--skip-triggers? ? 不导出触发器


-E, --events? ? ? ? Dump events. 导出调度事件(根据备份的目的进行选择,如果是搭建slave,那么就不要导出events.)


4)指定不导出 某个库的某个表:


--ignore-table=name? Do not dump the specified t

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据处理之PostgreSQL过程语言学习 下一篇mysqldump 逻辑备份的正确方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: