mysqldump导数据库数据不加锁(InnoDB)

本文深入解析了MySQL mysqldump的单交易模式选项,详细阐述了其工作原理、适用场景及使用注意事项,特别是针对InnoDB表的一致性状态备份。同时,文章还介绍了如何结合快速模式来高效备份大型表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction

 --single-transaction

This option sends a START TRANSACTION SQL statement to the server before dumping data. It is useful only with transactional tables such as InnoDB, because then it dumps the consistent state of the database at the time whenBEGIN was issued without blocking any applications.

When using this option, you should keep in mind that only InnoDB tables are dumped in a consistent state. For example, any MyISAM or MEMORY tables dumped while using this option may still change state.

While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log coordinates), no other connection should use the following statements: ALTER TABLECREATE TABLE,DROP TABLERENAME TABLETRUNCATE TABLE. A consistent read is not isolated from those statements, so use of them on a table to be dumped can cause the SELECT that is performed by mysqldump to retrieve the table contents to obtain incorrect contents or fail.

The --single-transaction option and the --lock-tables option are mutually exclusive because LOCK TABLES causes any pending transactions to be committed implicitly.

This option is not supported for MySQL Cluster tables; the results cannot be guaranteed to be consistent due to the fact that the NDBCLUSTER storage engine supports only the READ_COMMITTED transaction isolation level. You should always use NDB backup and restore instead.

To dump large tables, you should combine the --single-transaction option with --quick.


### mysqldump备份数据库时跳过某些表或选项的方法 mysqldump 是一种强大的工具,用于MySQL 数据库的内容到 SQL 文件中。通过适当配置参数,可以实现跳过某些表或者忽略特定的选项。 #### 跳过特定表 如果希望在使用 `mysqldump` 进行备份时跳过某些表,则可以通过 `--ignore-table` 参数来指定要排除的表名称。该参数允许多次使用以定义多个被忽略的表[^3]。以下是具体命令结构: ```bash mysqldump -h hostname -u username -p password --ignore-table=database_name.table1 --ignore-table=database_name.table2 database_name > output_file.sql ``` 此命令会将名为 `database_name` 的数据库出至 `output_file.sql` 中,但在过程中会包含 `table1` 和 `table2` 表的数据以及其创建语句[^3]。 #### 数据仅保留建表语句 有时可能只需要保存表结构而需要实际记录内容,在这种情况下可利用 `--no-data (-t)` 选项: ```bash mysqldump -h hostname -u username -p password --no-data database_name > schema_only_output.sql ``` 这只会生成目标数据库里各张表格对应的 CREATE TABLE 命令而涉及任何 INSERT INTO 操作[^5]。 #### 配置其他高级设置 除了基本功能外还存在一些额外有用的标志位可以帮助定制化我们的 dump 流程比如防止定整个数据库期间阻止写入操作发生(`--skip-lock-tables`)等特性[^4]: - **--single-transaction**: 此标记确保所有读取都在事务开始瞬间的一致视图下完成;这对于 InnoDB 存储引擎特别有用因为它能提供一致性的快照副本。 - **--quick**: 强制每次从服务器获取一行而是缓冲整条结果集再写出从而减少内存消耗量大查询的风险。 综合以上提到的技术要点下面给出一个完整的例子展示如何高效安全地执行带过滤条件的大规模迁移任务: ```bash mysqldump \ --host=localhost \ --user=root \ --password=my_password \ --single-transaction \ --quick \ --skip-lock-tables \ --ignore-table=test_db.log_table \ test_db > /backup/test_db_backup.sql ``` 上述脚本实现了针对本地主机上的 root 用户访问权限下的测试用 DB (`test_db`) 实施了一次快速且加锁的整体转存动作同时还避开了日志类别的特殊处理需求[^4]. ### 注意事项 当运用这些技巧的时候需要注意版本兼容性和授权许可等问题因为同发行版之间可能存在细微差异另外也要记得验证最终产物是否满足预期标准以免遗漏重要信息造成可逆损失.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值