使用mysqldump导出数据库

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:     mysqldump是mysql用于转存储数据库的客户端程序。它主要产生一系列的SQL语句,可以封装到文件,该文件包含有所有重建您的数据库所需要的SQL命令如CREATE DATABASE,CREATE TABLE,INSERT等等。

    mysqldump是mysql用于转存储数据库的客户端程序。它主要产生一系列的SQL语句,可以封装到文件,该文件包含有所有重建您的数据库所需要的SQL命令如CREATE DATABASE,CREATE TABLE,INSERT等等。可以用来实现轻量级的快速迁移或恢复数据库。是mysql数据库实现逻辑备份的一种方式。本文描述了mysqldump的一些重要参数以及给出了相关示例供大家参考。

 

1、获取mysqldump的帮助信息
  [root@SZDB ~]# mysqldump --help|more
  mysqldump  Ver 10.13 Distrib 5.6.12, for Linux (x86_64)
  
  #以下为MySQL dump调用的几种常用方式
  Dumping structure and contents of MySQL databases and tables.
  Usage: mysqldump [OPTIONS] database [tables]
  OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
  OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
  
  #如果没有指定任何表或使用了---database或--all--database选项,则转储整个数据库
                 
  --opt           Same as --add-drop-table, --add-locks, --create-options,
                   --quick, --extended-insert, --lock-tables, --set-charset,
                   and --disable-keys. Enabled by default, disable with
                   --skip-opt.
                
  -q, --quick           Don't buffer query, dump directly to stdout.  
                            (Defaults to on; use --skip-quick to disable.)
  #以上2个参数未使用的情况下,在转储结果之前会把全部内容载入到内存中,对于较大的数据库转储将严重影响性能。
  #缺省情况下这2个参数为开启状态。有些类似于Oracle的绕过PGA而直接写direct write。
              
  --skip-opt        Disable --opt. Disables --add-drop-table, --add-locks,
                      --create-options, --quick, --extended-insert,
                      --lock-tables, --set-charset, and --disable-keys.             
  #skip-opt与前2个参数相反,在转储之前先load到内存中。
  
  --compatible=name   Change the dump to be compatible with a given mode. By
                        default tables are dumped in a format optimized for
                        MySQL. Legal modes are: ansi, mysql323, mysql40,
                        postgresql, oracle, mssql, db2, maxdb, no_key_options,
                        no_table_options, no_field_options. One can use several
                        modes separated by commas. Note: Requires MySQL server
                        version 4.1.0 or higher. This option is ignored with
                        earlier server versions.
  
  #产生与其它数据库系统或旧版本MySQL服务器相兼容的输出。用于跨数据库,跨版本之间的迁移。
  #其值可以为ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options或者no_field_options。
  #如果要使用多个值,用逗号将它们隔开。该选项不能保证同其它数据库服务器之间的完全兼容。如Oracle的数据类型等。
                       
  --compact           Give less verbose output (useful for debugging). Disables
                        structure comments and header/footer constructs.  Enables
                        options --skip-add-drop-table --skip-add-locks
                        --skip-comments --skip-disable-keys --skip-set-charset.
  #该选项使得输出的文件更小,启用后等用于使用一些skip项等。
  
  -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.
  #该选项一次导出多个数据库所有名字参量看作数据库名,更重要的是会生成CREATE DATABASE IF NOT EXISTS dbname 
  
  --default-character-set=name
                        Set the default character set.
  #设置导出脚本的字符集,未指定的情况下为UTF8。
  
  --flush-privileges  Emit a FLUSH PRIVILEGES statement after dumping the mysql
                        database.  This option should be used any time the dump
                        contains the mysql database and any other database that
                        depends on the data in the mysql database for proper
                        restore.
  #在dump mysql数据库以及依赖于mysql数据库恢复时建议使用该选项生成FLUSH PRIVILEGES语句
  
  -F, --flush-logs    Flush logs file in server before starting dump. Note that
                        if you dump many databases at once (using the option
                        --databases= or --all-databases), the logs will be
                        flushed for each database dumped. The exception is when
                        using --lock-all-tables or --master-data: in this case
                        the logs will be flushed only once, corresponding to the
                        moment all tables are locked. So if you want your dump
                        and the log flush to happen at the same exact moment you
                        should use --lock-all-tables or --master-data with
                        --flush-logs.
  #在启动dump前会flush日志,此方式可以用于实现增量备份
                                                                                        
  -d, --no-data       No row information.
  #不输出数据行,仅导出结构
                     
  -f, --force         Continue even if we get an SQL error.                                           
  #在碰到错误时,依旧强制dump
  
  --master-data[=#]   This causes the binary log position and filename to be
                        appended to the output. If equal to 1, will print it as a
                        CHANGE MASTER command; if equal to 2, that command will
                        be prefixed with a comment symbol. This option will turn
                        --lock-all-tables on, unless --single-transaction is
                        specified too (in which case a global read lock is only
                        taken a short time at the beginning of the dump; don't
                        forget to read about --single-transaction below). In all
                        cases, any action on logs will happen at the exact moment
                        of the dump. Option automatically turns --lock-tables
                        off.
  #添加二进制日志位置到输出。1表示输出change master命令,2则注释输出change master命令。
                       
  -R, --routines      Dump stored routines (functions and procedures).
  #导出函数和过程以及触发器,缺省情况下,这些不会被导出
  
  -t, --no-create-info
                        Don't write table creation info.
  #不生成建表语句
                       
  --single-transaction
                        Creates a consistent snapshot by dumping all tables in a
                        single transaction. Works ONLY for tables stored in
                        storage engines which support multiversioning (currently
                        only InnoDB does); the dump is NOT guaranteed to be
                        consistent for other storage engines. While a
                        --single-transaction dump is in process, to ensure a
                        valid dump file (correct table contents and binary log
                        position), no other connection should use the following
                        statements: ALTER TABLE, DROP TABLE, RENAME TABLE,
                        TRUNCATE TABLE, as consistent snapshot is not isolated
                        from them. Option automatically turns off --lock-tables.
  #创建一致性快照,仅仅针对innodb引擎
  #不能存在其他操作:ALTER TABLE, DROP TABLE, RENAME TABLE,TRUNCATE TABLE,关闭--lock-tables。
                       
  -w, --where=name    Dump only selected records. Quotes are mandatory.
  #使用where子句只导出符合条件的记录
  # Author : Leshami
  # Blog   :
http://blog.csdn.net/leshami

 

2、dump的相关示例      
  备份单个数据库
  shell> mysqldump -uroot -pxxx sakila >sakila.sql
  
  带压缩方式备份数据库
  shell> mysqldump -uroot -pxxx sakila |gzip >sakila.sql.gz
  
  备份数据库上的特定表
  shell> mysqldump -uroot -pxxx sakila actor >sakila_actor.sql
  
  备份表上特定的记录
  shell> mysqldump -uroot -pxxx sakila actor -w "first_name='NICK'" >sakila_actor_row.sql
  
  备份数据库的结构
  shell> mysqldump -uroot -pxxx sakila --no-data >sakila_structure.sql
  
  同时备份多个数据库
  shell> mysqldump -uroot -pxxx --database sakila tempdb test >multidb.sql
  
  备份服务器上的所有数据库
  shell> mysqldump -uroot -pxxx --all-databases --opt --compact --flush-privileges >alldb.sql
  
  只导出数据库中的存储过程,函数,触发器
  shell> mysqldump -uroot -pxxx sakila --no-create-db --no-data --no-tablespaces --no-create-info --routines >sakila.sql
  
  全量备份与增量备份
  #下面首先对数据库做全量备份并在备份前flush日志
  shell> mysqldump -uroot -pxxx --single-transaction --flush-logs --master-data=2 sakila >sakila_full.sql
  #假定备份时flush的日志为mysql-bin.000004,则使用如下方式恢复
  shell> mysql -uroot -pxxx < sakila_full.sql
  shell> mysqlbinlog mysql-bin.000004 | mysql -uroot -pPwd

 

3、其它
a、对比--database与直接数据库备份,如下,也就是说使用--database会生成建库语句
  shell> mysqldump -uroot -pxxx --database tempdb >tempdb1.log
  shell> mysqldump -uroot -pxxx --opt tempdb >tempdb2.log
  shell> grep DATABASE tempdb1.log
   CREATE DATABASE /*!32312 IF NOT EXISTS*/ `tempdb` /*!40100 DEFAULT CHARACTER SET utf8 */;
  shell> grep DATABASE tempdb2.log

b、性能相关
  使用--quick或者--opt有助于加快dump的过程,减少dump所需的时间。
  mysqldump不适用于大型数据库备份与恢复,速度慢,不支持并行,其次SQL重放将耗用大量的I/O。对于这种情形,建议使用物理备份方式。
  如果mysql数据库中使用的存储引擎主要为innodb或myisam,或者2者的混合,可以考虑使用mysql企业版更高效的mysqlbackup工具。
  如果mysql数据库中主要的表为myisam,更好的性能建议使用mysqlhotcopy方式。
  详细参考:
http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html

鹏城DBA总群

 

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
11天前
|
数据库 数据安全/隐私保护
【YashanDB 知识库】exp 导出数据库时,报错 YAS-00402
**简介:** 在执行数据导出命令 `exp --csv -f csv -u sales -p sales -T area -O sales` 时,出现 YAS-00402 错误,提示“Connection refused”。原因是数据库安装时定义的 IP 地址或未正确配置导致连接失败。解决方法是添加 `--server-host ip:port` 参数,例如 `exp --csv -f csv -u sales -p sales -T area -O sales --server-host 192.168.33.167:1688`。
|
13天前
|
关系型数据库 数据库连接 数据库
循序渐进丨MogDB 中 gs_dump 数据库导出工具源码概览
通过这种循序渐进的方式,您可以深入理解 `gs_dump` 的实现,并根据需要进行定制和优化。这不仅有助于提升数据库管理的效率,还能为数据迁移和备份提供可靠的保障。
31 6
|
3月前
|
关系型数据库 MySQL Linux
Linux下mysql数据库的导入与导出以及查看端口
本文详细介绍了在Linux下如何导入和导出MySQL数据库,以及查看MySQL运行端口的方法。通过这些操作,用户可以轻松进行数据库的备份与恢复,以及确认MySQL服务的运行状态和端口。掌握这些技能,对于日常数据库管理和维护非常重要。
163 8
|
7月前
|
SQL 关系型数据库 MySQL
如何在 MySQL 或 MariaDB 中导入和导出数据库
如何在 MySQL 或 MariaDB 中导入和导出数据库
751 0
|
7月前
|
JSON NoSQL MongoDB
在Ubuntu 14.04上如何导入和导出MongoDB数据库
在Ubuntu 14.04上如何导入和导出MongoDB数据库
41 0
|
7月前
|
安全 关系型数据库 MySQL
如何在 MySQL 中导入和导出数据库以及重置 root 密码
如何在 MySQL 中导入和导出数据库以及重置 root 密码
98 0
|
8月前
|
Cloud Native 关系型数据库 MySQL
云原生数据仓库使用问题之如何将ADB中的数据导出到自建的MySQL数据库
阿里云AnalyticDB提供了全面的数据导入、查询分析、数据管理、运维监控等功能,并通过扩展功能支持与AI平台集成、跨地域复制与联邦查询等高级应用场景,为企业构建实时、高效、可扩展的数据仓库解决方案。以下是对AnalyticDB产品使用合集的概述,包括数据导入、查询分析、数据管理、运维监控、扩展功能等方面。
|
11天前
|
监控 关系型数据库 MySQL
云数据库:从零到一,构建高可用MySQL集群
在互联网时代,数据成为企业核心资产,传统单机数据库难以满足高并发、高可用需求。云数据库通过弹性扩展、分布式架构等优势解决了这些问题,但也面临数据安全和性能优化挑战。本文介绍了如何从零开始构建高可用MySQL集群,涵盖选择云服务提供商、创建实例、配置高可用架构、数据备份恢复及性能优化等内容,并通过电商平台案例展示了具体应用。
|
18天前
|
SQL 关系型数据库 MySQL
数据库数据恢复——MySQL简介和数据恢复案例
MySQL数据库数据恢复环境&故障: 本地服务器,安装的windows server操作系统。 操作系统上部署MySQL单实例,引擎类型为innodb,表空间类型为独立表空间。该MySQL数据库没有备份,未开启binlog。 人为误操作,在用Delete命令删除数据时未添加where子句进行筛选导致全表数据被删除,删除后未对该表进行任何操作。
|
24天前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
122 25

热门文章

最新文章