mysqldump详解之--master-data

在前一篇文章中,有提到mysqldump的--single-transaction参数。
另外还有个很重要,也是运维中经常用到的参数:--master-data,网上很多关于MySQL不停机备份的实现都有它的身影。
翻译下man mysqldump中--master-data相关的章节,以便加强理解

? --master-data[=value]

Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. 

It causes the dump output to include a CHANGE MASTER TO statement that indicates the binary log coordinates (file name and position) of the dumped server. 

These are the master server coordinates from which the slave should start replicating after you load the dump file into the slave.

使用此选项dump a master replication server到dump文件,可用于设立另一台服务器作为master的slave。

它会使dump输出包含CHANGE MASTER TO语句,标记dump源的二进制日志坐标(文件名和位置)。

在把load dump文件加载到slave之后,slave应当从该master坐标开始复制。


If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. 

If the option value is 1, the statement is not written as a comment and takes effect when the dump file is reloaded. 

If no option value is specified, the default value is 1.

如果选项赋值为2,那么CHANGE MASTER TO 语句会被写成一个SQL comment(注释),从而只提供信息; 

如果选项赋值为1,那么语句不会被写成注释并且在dump被载入时生效。

如果没有指定,默认值为1。


This option requires the RELOAD privilege and the binary log must be enabled.

这个选项要求 RELOAD 权限,并且二进制文件必须打开。


The --master-data option automatically turns off --lock-tables. 

It also turns on --lock-all-tables, unless --single-transaction also is specified, in which case, a global read lock is acquired only for a short time at the beginning of the dump (see the description for --single-transaction). 

In all cases, any action on logs happens at the exact moment of the dump.

--master-data 选项会自动关闭 --lock-tables。

也会打开 --lock-all-tables,除非指定--single-transaction,如果那样的话,在开始dump时会获取一个全局read lock 一小段时间(详见--single-transaction的描述)。

不论什么情况下,日志相关动作发生在dump的确切时刻。——没懂


It is also possible to set up a slave by dumping an existing slave of the master. 

To do this, use the following procedure on the existing slave:

它也可以dump一个存在的slave来设立一个新的slave。

要做到这点,在已存在的slave使用如下的步骤:


1. Stop the slave?s SQL thread and get its current status:

停止slave的SQL 线程并获取它的当前状态


mysql> STOP SLAVE SQL_THREAD;
mysql> SHOW SLAVE STATUS;

2. From the output of the SHOW SLAVE STATUS statement, the binary log coordinates of the master server
from which the new slave should start replicating are the values of the Relay_Master_Log_File and
Exec_Master_Log_Pos fields. Denote those values as file_name and file_pos.

从SHOW SLAVE STATUS语句的输出,新slave应当开始复制master二进制的坐标就是 Relay_Master_Log_File 和 Exec_Master_Log_Pos 的值。 

用 file_name 和 file_pos 表示这些值。


3. Dump the slave server:

shell> mysqldump --master-data=2 --all-databases > dumpfile

Using --master-data=2 works only if binary logging has been enabled on the slave. 

Otherwise, mysqldump fails with the error Binlogging on server not active. 

In this case you must handle any locking issues in another manner, using one or more of --add-locks, --lock-tables, --lock-all-tables, or --single-transaction, as required by your application and environment.

仅在已打开二进制日志的slave上使用--master-data=2。

否则mysqldump会报错二进制日志未启动。

这种情况下你必须以另一种方式来处理任何锁定问题,根据你应用和环境来决定使用--add-locks, --lock-tables, --lock-all-tables,or --single-transaction中的一个或多个。


4. Restart the slave:

mysql> START SLAVE;

5. On the new slave, load the dump file:

shell> mysql < dumpfile

6. On the new slave, set the replication coordinates to those of the master server obtained earlier:

在新slave上,设置之前从master获取的复制坐标


mysql> CHANGE MASTER TO
-> MASTER_LOG_FILE = ?file_name?, MASTER_LOG_POS = file_pos;

The CHANGE MASTER TO statement might also need other parameters, such as MASTER_HOST to point the slave to the correct master server host. Add any such parameters as necessary.
CHANGE MASTER TO语句也需要其他参数,比如 MASTER_HOST 来指定slave连接正确的master。按需增加其他参数
------------------------

? --delete-master-logs

On a master replication server, delete the binary logs by sending a PURGE BINARY LOGS statement to the server after performing the dump operation. This option automatically enables --master-data.

在master replication server 执行dump操作后,通过发送一个 PURGE BINARY LOGS 语句到服务器来删除二进制日志。

该选项自动启用--master-data。

------------------------


? --flush-logs, -F

Flush the MySQL server log files before starting the dump. This option requires the RELOAD privilege. 

If you use this option in combination with the --all-databases option, the logs are flushed for each database dumped. 

The exception is when using --lock-all-tables or --master-data: In this case, the logs are flushed only once, corresponding to the moment that all tables are locked. 

If you want your dump and the log flush to happen at exactly the same moment, you should use --flush-logs together with either --lock-all-tables or --master-data.

在开始dump之前flush mysql 日志文件。该选项需要 RELOAD 权限。

如果你将该选项与 --all-databases 结合使用,每个数据库的日志都会被flush for dump。

例外是当使用 --lock-all-tables 或 --master-data,这种情况下,日志会被flush一次,对应于所有表锁定的那一刻。

如果你希望dump 和 log flush 发生在完全相同的时刻,你应该把 --flush-logs 连同 --lock-all-tables 或 --master-data使用。

------------------------

This backup acquires a global read lock on all tables (using FLUSH TABLES WITH READ LOCK) at the beginning of the dump. 

As soon as this lock has been acquired, the binary log coordinates are read and the lock is released.

If long updating statements are running when the FLUSH statement is issued, the MySQL server may get stalled until those statements finish. 

After that, the dump becomes lock free and does not disturb reads and writes on the tables. 

If the update statements that the MySQL server receives are short (in terms of execution time), the initial lock period should not be noticeable, even with many updates.

此备份在dump开始的时候就获得所有表的全局读锁定(使用FLUSH TABLES WITH READ LOCK)。
当锁一旦被获取,就读取二进制日志坐标并释放锁。
如果执行FLUSH语句时有很长的update语句正在执行,MySQL会直到等那些语句完成才会get stalled(失速/抛锚,停止?)。
在此之后,dump 变得 lock free 并且不再妨碍读写表。

如果MySQL服务器接收update语句很短(在执行时间),初步锁定期并不明显,即使有很多的update。


For point-in-time recovery (also known as "roll-forward," when you need to restore an old backup and replay the changes that happened since that backup), it is often useful to rotate the binary log (see Section 5.2.4, "The Binary Log") or at least know the binary log coordinates to which the dump corresponds:

对于即时恢复(也称为“roll-forward”,当你需要恢复旧的备份并重放从备份之后发生的变化),这是经常用于循环二进制日志(参见第5.2.4,“二进制日志”)或至少知道二进制日志坐标到dump对应关系:


shell> mysqldump --all-databases --master-data=2 > all_databases.sql

Or:

shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql

The --master-data and --single-transaction options can be used simultaneously, which provides a convenient way to make an online backup suitable for use prior to point-in-time recovery if tables are stored using the InnoDB storage engine.

--master-data 和 --single-transaction 选项可同时使用,如果表使用InnoDB存储引擎,它提供了简便的途径来制作适用于定点恢复的在线备份。


<think>好的,用户现在遇到了一个关于mysqldump命令的问题,命令是“mysqldump -u root -h -p dongcheng2”。我需要先分析这个命令的问题在哪里。首先,用户可能是在尝试备份名为dongcheng2的数据库,但命令执行时出现了错误。 首先,检查命令结构。mysqldump的基本用法是:mysqldump -u [用户名] -h [主机名] -p [数据库名]。这里用户输入的是“-h ”,后面没有跟主机地址,这可能是一个错误。因为-h参数后面需要指定主机名或IP地址,比如-h localhost或者-h 127.0.0.1。如果用户省略了主机名,可能应该直接不写-h参数,或者如果是要连接本地服务器,可能需要明确指定localhost。 接下来是-p参数,用户直接在-p后面跟了数据库名dongcheng2,这会导致-p后面的内容被当作密码的一部分,而实际上-p是提示输入密码,后面不应该直接跟其他参数。正确的做法是-p后面不加空格,或者单独使用-p然后回车输入密码,或者如果密码需要直接写在命令中(不安全),应该用--password=密码的形式。 所以用户的命令应该是缺少了主机地址,并且-p的使用方式不正确。可能的正确命令应该是:mysqldump -u root -h localhost -p dongcheng2,然后系统会提示输入密码。或者如果用户的主机是远程的,需要替换localhost为正确的IP或域名。 另外,用户可能不太清楚各个参数的具体用法,需要详细解释每个参数的作用,以及正确的语法格式。例如,-u指定用户名,-h指定主机,-p表示需要输入密码,数据库名应该放在命令的最后。还要注意参数的顺序,避免混淆。 还需要考虑用户是否可能输错了参数顺序,比如把数据库名放在了错误的位置,导致mysqldump无法识别。另外,如果用户的主机参数确实需要留空,可能在某些情况下可以省略-h参数,默认使用本地连接,但需要确认MySQL服务器的配置是否允许这样。 总结来说,用户的问题在于命令中-h参数后面没有主机地址,以及-p参数的使用方式错误。需要指导用户修正这两个部分,并提供正确的命令示例,同时解释每个参数的作用,避免未来出现类似错误。</think>--- ### **错误命令解析与修复** #### **1. 问题定位** 你执行的命令存在**参数缺失**和**语法顺序错误**: ```bash mysqldump -u root -h -p dongcheng2 ``` - **核心问题**: - `-h` 参数未指定主机地址(例如`localhost`或IP) - `-p` 参数与密码或数据库名位置冲突 --- #### **2. 正确命令格式** **标准语法**: ```bash mysqldump -u <用户名> -h <主机地址> -p <数据库名> > <输出文件.sql> ``` **修复后命令**(以本地服务器为例): ```bash mysqldump -u root -h localhost -p dongcheng2 > dongcheng2_backup.sql ``` - 执行后会提示输入密码 - 备份文件将保存为`dongcheng2_backup.sql` --- #### **3. 参数详解** | **参数** | **作用** | **注意事项** | |-----------|-----------------------------------|-----------------------------------------------| | `-u root` | 指定MySQL用户名 | 若使用非root用户,替换为实际用户名 | | `-h` | 指定MySQL服务器地址 | 本地可省略或写`-h localhost`,远程需填IP/域名 | | `-p` | 启用密码输入 | 不要直接跟密码(明文不安全),应回车后输入 | | 末尾参数 | 指定要备份的数据库名(此处dongcheng2) | 必须放在命令最后 | --- #### **4. 典型错误场景** ##### **场景1:省略`-h`参数** ```bash # 若MySQL服务器在本地,可简化为: mysqldump -u root -p dongcheng2 > backup.sql ``` ##### **场景2:连接远程服务器** ```bash # 指定远程IP和端口(默认端口3306可省略) mysqldump -u root -h 192.168.1.100 -P 3306 -p dongcheng2 > remote_backup.sql ``` ##### **场景3:备份所有数据库** ```bash # 添加`--all-databases`参数(不需要指定数据库名) mysqldump -u root -h localhost -p --all-databases > full_backup.sql ``` --- #### **5. 安全增强建议** | **操作** | **命令示例** | **说明** | |-------------------------|----------------------------------------------|-----------------------------------| | **使用配置文件** | `mysqldump --defaults-extra-file=~/.my.cnf` | 避免在命令行暴露密码 | | **压缩备份** | `mysqldump -u root -p dbname \| gzip > backup.sql.gz` | 减少备份文件大小 | | **排除指定表** | `mysqldump -u root -p dbname --ignore-table=dbname.logs` | 跳过不需要备份的大表 | --- #### **6. 验证备份完整性** ```bash # 检查SQL文件头信息 head -n 10 dongcheng2_backup.sql # 预期输出应包含: -- MySQL dump 10.13 Distrib 8.0.33 -- Host: localhost Database: dongcheng2 ``` --- #### **7. 高级用法示例** ##### **仅备份表结构** ```bash mysqldump -u root -h localhost -p --no-data dongcheng2 > schema.sql ``` ##### **仅备份特定表** ```bash mysqldump -u root -h localhost -p dongcheng2 users orders > tables.sql ``` ##### **增量备份(需配合binlog)** ```bash # 先全量备份 mysqldump -u root -p --master-data=2 --single-transaction dongcheng2 > full_backup.sql # 后续通过binlog实现增量恢复 mysqlbinlog mysql-bin.000001 > incremental.sql ``` --- ### **总结** - **核心修复**:补全`-h`参数的主机地址,确保`-p`与数据库名之间有空格 - **扩展建议**:根据实际需求选择备份范围和安全策略 - **验证步骤**:始终检查备份文件头部信息和文件大小 遇到具体报错时,可提供完整终端输出进一步分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值