Table of Contents
1. 语法选项说明
2. 示例
参考
Home Database Mysql Tutorial MySQL备份命令mysqldump参数说明与示例

MySQL备份命令mysqldump参数说明与示例

Jun 07, 2016 pm 04:42 PM
mysql parameter Order backup Example illustrate

原文 ? http://segmentfault.com/blog/seanlook/1190000002428533 1. 语法选项说明 -h, --host=name 主机名 -P[ port_num], --port=port_num 用于连接MySQL服务器的的TCP/IP端口号 --master-data 这个选项可以把binlog的位置和文件名添加到输出中,如果等于1

1. 语法选项说明

  • -h, --host=name
    主机名
  • -P[ port_num], --port=port_num用于连接MySQL服务器的的TCP/IP端口号

  • --master-data这个选项可以把binlog的位置和文件名添加到输出中,如果等于1,将会打印成一个CHANGE MASTER 命令;如果等于2,会加上注释前缀。并且这个选项会自动打开--lock-all-tables ,除非同时设置了 --single-transaction (这种情况下,全局读锁只会在开始dump的时候加上一小段时间,不要忘了阅读--single-transaction 的部分)。在任何情况下,所有日志中的操作都会发生在导出的准确时刻。这个选项会自动关闭 --lock-tables

  • -x, --lock-all-tables锁定所有库中所有的表。这是通过在整个dump的过程中持有全局读锁来实现的。会自动关闭 --single-transaction--lock-tables

  • --single-transaction通过将导出操作封装在一个事务内来使得导出的数据是一个一致性快照。只有当表使用支持MVCC的存储引擎(目前只有InnoDB)时才可以工作;其他引擎不能保证导出是一致的。当导出开启了 --single-transaction 选项时,要确保导出文件有效(正确的表数据和二进制日志位置),就要保证没有其他连接会执行如下语句: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE ,这会导致一致性快照失效。这个选项开启后会自动关闭 --lock-tables

  • -l, --lock-tables对所有表加读锁。(默认是打开的,用 --skip-lock-tables 来关闭,上面的选项会把关闭 -l 选项)

  • -F, --flush-logs在开始导出前刷新服务器的日志文件。注意,如果你一次性导出很多数据库(使用-databases=--all-databases 选项),导出每个库时都会触发日志刷新。例外是当使用了 --lock-all-tables--master-data 时:日志只会被刷新一次,那个时候所有表都会被锁住。所以如果你希望你的导出和日志刷新发生在同一个确定的时刻,你需要使用 --lock-all-tables ,或者--master-data 配合 --flush-logs

  • --delete-master-logs备份完成后删除主库上的日志。这个选项会自动打开“–master-data`。

  • --opt-add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, --disable-keys。(默认已开启, --skip-opt 关闭表示这些选项保持它的默认值)应该给你为读入一个MySQL服务器的尽可能最快的导出, --compact 差不多是禁用上面的选项。

  • -q, --quick
    不缓冲查询,直接导出至stdout。(默认打开,用 --skip-quick 来关闭)该选项用于转储大的表。
  • --set-charset
    SET NAMES default_character_set 加到输出中。该选项默认启用。要想禁用 SET NAMES 语句,使用 --skip-set-charset
  • --add-drop-tables
    在每个 CREATE TABLE 语句前添加 DROP TABLE 语句。默认开启。
  • --add-locks
    在每个表导出之前增加 LOCK TABLES 并且之后 UNLOCK TABLE 。(为了使得更快地插入到MySQL)。默认开启。
  • --create-option
    在CREATE TABLE语句中包括所有MySQL表选项。默认开启,使用--skip-create-options 来关闭。
  • -e, --extended-insert使用全新多行INSERT语法,默认开启(给出更紧缩并且更快的插入语句)

  • -d, --no-data不写入表的任何行信息。如果你只想得到一个表的结构的导出,这是很有用的。

  • --add-drop-database在create数据库之前先 DROP DATABASE ,默认关闭,所以一般在导入时需要保证数据库已存在。

  • --default-character-set=使用的默认字符集。如果没有指定,mysqldump使用utf8。

  • -B, --databases转储几个数据库。通常情况,mysqldump将命令行中的第1个名字参量看作数据库名,后面的名看作表名。使用该选项,它将所有名字参量看作数据库名。CREATE DATABASE IF NOT EXISTS db_nameUSE db_name 语句包含在每个新数据库前的输出中。

  • --tables覆盖 --database 选项。选项后面的所有参量被看作表名。

  • -u[ name], --user=连接服务器时使用的MySQL用户名。

  • -p[password], --password[=password]
    连接服务器时使用的密码。如果你使用短选项形式(-p),不能在选项和密码之间有一个空格。如果在命令行中,忽略了 --password-p 选项后面的 密码值,将提示你输入一个。

2. 示例

导出一个数据库:

$ mysqldump -h localhost -uroot -ppassword \
--master-data=2 --single-transaction --add-drop-table --create-options --quick \
--extended-insert --default-character-set=utf8 \
--databases discuz > backup-file.sql
Copy after login

导出一个表:

$ mysqldump -u pak -p --opt --flush-logs pak t_user > pak-t_user.sql
Copy after login

将备份文件压缩:

$ mysqldump -hhostname -uusername -ppassword --databases dbname | gzip > backup-file.sql.gz
对应的还原动作为
gunzip 
<p>导入数据库:</p>
<pre class="brush:php;toolbar:false">mysql> use target_dbname
mysql> source /mysql/backup/path/backup-file.sql
或
$ mysql target_dbname <backup-file.sql>
<p>导入还有一个 <code class="prettyprint">mysqlimport</code> 命令,暂未研究。</p>
<p>直接从一个数据库向另一个数据库转储:</p>
<pre class="brush:php;toolbar:false">mysqldump -u用户名 -p --opt dbname | mysql --host remote_host -C dbname2
Copy after login

关于增量备份与恢复请参考: MySQL增量备份与恢复实例 。

参考

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles