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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles