Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >深入解析 MongoDB 中的 renameCollection 操作:不同版本中的风险与影响

深入解析 MongoDB 中的 renameCollection 操作:不同版本中的风险与影响

作者头像
DBA实战
发布于 2024-09-06 11:05:11
发布于 2024-09-06 11:05:11
1870
举报
文章被收录于专栏:DBA实战DBA实战

MongoDB 中,renameCollection 操作用于将一个集合重命名为另一个名称。虽然这看起来是一个简单的操作,但在不同版本的 MongoDB 中,renameCollection 有一些重大的变化和注意事项。本文将深入探讨在不同版本的 MongoDB 中执行 renameCollection 操作时需要考虑的方面。

MongoDB 3.4 版本

renameCollection is suitable for production environments; however:

  • renameCollection blocks all database activity for the duration of the operation.
  • renameCollection is not compatible with sharded collections.
  • renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true
  • You cannot rename a collection from a replicated database to the database, which is not replicated.local
  • You cannot rename a collection from the database, which is not replicated, to a replicated database.local

renameCollection 适合生产 环境:

  • renameCollection操作会阻塞整个数据库的活动,直到操作完成为止。
  • renameCollection不兼容分片集合(sharded collections)
  • 如果要重命名的集合名字已经存在,并且没有指定.targetdropTarget: true,renameCollection操作会失败。
  • 不能将一个来自有副本的数据库(replicated database)的集合重命名到没有副本的数据库(non-replicated database)。
  • 不能将一个来自没有副本的数据库的集合重命名到有副本的数据库。

注意事项:

  1. 相当于是一个copy的操作,如果集合数据量过大,非常耗时,线上慎用;
  2. 重名过程会阻塞整个数据库的活动,直到操作完成为止;
  3. 分片集合不支持重命名操作;

MongoDB 3.6 版本

Sharded Collections

renameCollection is not compatible with sharded collections.

Existing Target Collection

renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true

Performance

Changed in version 3.6.

renameCollection has different performance implications depending on the target namespace.

If the target database is the same as the source database, renameCollection simply changes the namespace. This is a quick operation.

If the target database differs from the source database, renameCollection copies all documents from the source collection to the target collection. Depending on the size of the collection, this may take longer to complete. Other operations which require exclusive access to the affected databases will be blocked until the rename completes. See What locks are taken by some common client operations? for operations which require exclusive access to all databases.

  1. 分片集合不兼容:renameCollection 操作不兼容于分片集合。这意味着,如果你的集合是分片的,你不能使用 renameCollection 来重命名这个集合。在对分片集合进行操作时,需要使用其他方式来管理集合的名称或结构。
  2. 现有目标集合:如果目标集合已经存在,并且你没有明确指定 .targetdrop 选项为 true,那么 renameCollection 操作将失败。在执行 renameCollection 操作时,如果目标集合已经存在,你必须明确指定是否要删除目标集合并覆盖它。
  3. 性能影响:在 MongoDB 3.6 版本中,renameCollection 操作的性能取决于目标命名空间的情况:
  • 如果目标数据库与源数据库相同,那么 renameCollection 操作只是简单地更改命名空间。这是一个快速的操作,因为它只涉及更改命名空间的元数据信息。
  • 如果目标数据库与源数据库不同,那么 renameCollection 操作将会复制源集合中的所有文档到目标集合中。这可能会导致操作需要花费更多的时间来完成,特别是对于大型集合来说。在执行复制操作期间,其他需要对受影响的数据库进行排他性访问的操作将被阻塞,直到重命名操作完成。

注意事项:

db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )

  1. renameCollection 操作不兼容于分片集合;
  2. 只更改命名空间,速度极快;

db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )

  1. 跨库重命名相当于是一个copy操作,如果集合数据量较大,执行特别耗时,阻塞操作,线上慎用。

MongoDB 4.2 版本

Sharded Collections

renameCollection is not compatible with sharded collections.

Existing Target Collection

renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true

Performance

Changed in version 3.6.

renameCollection has different performance implications depending on the target namespace.

If the target database is the same as the source database, renameCollection simply changes the namespace. This is a quick operation.

If the target database differs from the source database, renameCollection copies all documents from the source collection to the target collection. Depending on the size of the collection, this may take longer to complete.

Resource Locking

Changed in version 4.2.

If renaming a collection within the same database, renameCollection obtains an exclusive lock on the source and target collections for the duration of the operation. All subsequent operations on the collections must wait until renameCollection completes.

Prior to MongoDB 4.2, renaming a collection within the same database with renameCollection required an exclusive database lock.

If renaming a collection between different databases, renameCollection locking behavior depends on the MongoDB version:

For MongoDB 4.2.2 and later, renameCollection obtains an exclusive (W) lock on the target database, an intent shared (r) lock on the source database, and a shared (S) lock on the source collection. Subsequent operations on the target database must wait until renameCollection releases the exclusive database lock.

For MongoDB 4.2.1 and earlier, renameCollection obtains an exclusive (W) global lock. Subsequent operations on the mongod must wait until renameCollection releases the global lock.

For more information on locking in MongoDB, see FAQ: Concurrency.

1、分片集合不兼容:

renameCollection 操作不兼容于分片集合。这意味着无法直接对分片集合使用 renameCollection 来重命名。

2、现有目标集合:

如果要重命名的目标集合名称已经存在,并且没有指定 .targetdrop: true 参数,renameCollection 操作将失败。这是为了避免意外覆盖已存在的集合。在执行 renameCollection 操作时,需要明确指定是否要删除已存在的目标集合以便重命名成功。

3、性能:

在 MongoDB 3.6 及更高版本中,renameCollection 操作的性能取决于目标命名空间的不同情况:

  • 如果目标数据库与源数据库相同,renameCollection 操作只是简单地更改命名空间,这是一个快速的操作,因为它只涉及元数据的更改。
  • 如果目标数据库与源数据库不同,renameCollection 操作将会复制源集合中的所有文档到目标集合中。这可能会导致操作耗时更长,特别是对于大型集合而言。

4、资源锁定:

在 MongoDB 4.2 版本中,renameCollection 操作的资源锁定行为发生了变化:

  • 如果在同一数据库中重命名集合,renameCollection 会在操作期间对源集合和目标集合获取独占锁。所有对这些集合的后续操作必须等待 renameCollection 完成。
  • 在不同数据库之间重命名集合时,renameCollection 的锁定行为取决于 MongoDB 版本:
  • 对于 MongoDB 4.2.2 及更高版本,renameCollection 会在目标数据库上获取独占锁(W),在源数据库上获取意图共享锁(r),在源集合上获取共享锁(S)。所有对目标数据库的后续操作必须等待 renameCollection 释放独占数据库锁。
  • 对于 MongoDB 4.2.1 及更早版本,renameCollection 会获取全局独占锁(W)。所有对 MongoDB 实例的后续操作必须等待 renameCollection 释放全局锁。

注意事项:

db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )

  1. renameCollection 操作不兼容于分片集合;
  2. 只更改命名空间,速度极快;
  3. 对源集合和目标集合获取独占锁,必须等待 renameCollection

db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )

  1. MongoDB 4.2.2 及更高版本,跨库重命名相当于是一个copy操作,如果集合数据量较大,执行特别耗时,目标数据库上获取独占锁(W),目标数据库的后续操作必须等待 renameCollection 释放独占数据库锁,线上慎用。
  2. MongoDB 4.2.1 及更早版本,跨库重命名相当于是一个copy操作,如果集合数据量较大,执行特别耗时,renameCollection 会获取全局独占锁(W)。所有对 MongoDB 实例的后续操作必须等待 renameCollection 释放全局锁,线上慎用。

MongoDB 5.0 版本

Sharded Collections

Starting in MongoDB 5.0, you can use the renameCollection command to change the name of a sharded collection. The target database must be the same as the source database.

Unsharded Collections

You can use renameCollection to rename an unsharded collection in a sharded cluster as long as the source and target databases are on the same primary shard.

Existing Target Collection

renameCollection fails if target is the name of an existing collection and you do not specify dropTarget: true.

Performance

Changed in version 3.6.

renameCollection has different performance implications depending on the target namespace.

If the target database is the same as the source database, renameCollection simply changes the namespace. This is a quick operation.

If the target database differs from the source database, renameCollection copies all documents from the source collection to the target collection. Depending on the size of the collection, this may take longer to complete.

Resource Locking in Sharded Clusters

Changed in version 5.0.

When renaming a sharded or unsharded collection in a sharded cluster, the source and target collections are exclusively locked on every shard. Subsequent operations on the source and target collections must wait until the rename operation completes.

For more information on locking in MongoDB, see FAQ: Concurrency.

Resource Locking in Replica Sets

If renaming a collection within the same database, renameCollection obtains an exclusive lock on the source and target collections for the duration of the operation. All subsequent operations on the collections must wait until renameCollection completes.

If renaming a collection between different databases, renameCollection obtains an exclusive (W) lock on the target database, an intent shared (r) lock on the source database, and a shared (S) lock on the source collection. Subsequent operations on the target database must wait until renameCollection releases the exclusive database lock.

For more information on locking in MongoDB, see FAQ: Concurrency.

1、行为

  • 对于分片集合(Sharded Collections):从MongoDB 5.0开始,可以使用renameCollection命令来更改分片集合的名称。目标数据库必须与源数据库相同。
  • 对于非分片集合(Unsharded Collections):在分片集群中,只要源数据库和目标数据库在同一个主分片上,就可以使用renameCollection来重命名非分片集合。
  • 对于已存在的目标集合:如果目标集合已经存在,而且没有设置dropTarget: true选项,则renameCollection操作将失败。

2、性能:

  • 自MongoDB 3.6版本开始,renameCollection的性能取决于目标命名空间。
  • 如果目标数据库与源数据库相同,则renameCollection仅仅修改命名空间,这是一个快速的操作。
  • 如果目标数据库与源数据库不同,则renameCollection将所有文档从源集合复制到目标集合。根据集合的大小,这可能需要更长的时间来完成。

3、资源锁定:

  • 在分片集群中,当重命名分片集合或非分片集合时,每个分片上的源和目标集合都会被独占锁定。在重命名操作完成之前,对源和目标集合的后续操作必须等待。
  • 在副本集中,如果在同一个数据库中重命名集合,renameCollection会为操作的整个过程获取源和目标集合的独占锁。所有对集合的后续操作必须等待renameCollection完成。
  • 如果在不同的数据库之间重命名集合,renameCollection会在目标数据库上获取独占锁(W),在源数据库上获取意向共享锁(r),在源集合上获取共享锁(S)。后续对目标数据库的操作必须等待renameCollection释放独占数据库锁。

注意事项:

  1. 不能使用 renameCollection 重命名时间序列集合
  2. 支持分片集合renameCollection操作,不能跨库操作;
  3. 非分片集合renameCollection操作,在分片集群中,源数据库和目标数据库在同一个主分片上;

db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )

  1. 同库重命名,只更改命名空间,速度极快;
  2. 在分片集群中,当重命名分片集合或非分片集合时,在重命名操作完成之前,源和目标集合都会被独占锁定,必须等待;
  3. 在副本集中,如果在同一个数据库中重命名集合,在重命名操作完成之前,源和目标集合都会被独占锁定,必须等待;

db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )

跨库重命名相当于是一个copy操作,如果集合数据量较大,执行特别耗时。目标数据库上获取独占锁(W),在源数据库上获取意向共享锁(r),在源集合上获取共享锁(S)。后续对目标数据库的操作必须等待renameCollection释放独占数据库锁阻塞操作,线上慎用。

MongoDB 6.0 、7.0版本版本renameCollection未做变更

结论:

在进行 renameCollection 操作时,请务必谨慎操作。这个操作可能会对线上系统产生重大影响,因此建议在非高峰时段进行,并且提前备份数据以防万一。谨慎的操作是确保系统稳定性和数据完整性的重要步骤,我们始终致力于提供最佳的服务和解决方案,以确保您的业务顺利运行。再次感谢您对我们的信任和支持。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DBA实战 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Ansible自动化运维学习笔记2
前言:在ansible中使用变量,能让我们的工作变得更加灵活,在ansible中变量的使用方式有很多种
全栈工程师修炼指南
2020/10/26
3.1K0
Ansible自动化运维学习笔记2
[Ansible专栏]Ansible条件判断的介绍和使用
很多时候,某些场景下playbook的结果依赖于变量、fact或者是前一个任务的执行结果,或者有的时候,我们会基于上一个task执行返回的结果而决定如何执行后续的task。这个时候就需要用到条件判断。
没有故事的陈师傅
2022/12/06
1.6K0
Ansible自动化运维学习笔记3
在使用ansible的过程中,我们经常需要处理一些返回信息而这些返回信息中,通常可能不是单独的一条返回信息而是一个信息列表;
全栈工程师修炼指南
2020/10/26
2.6K0
Ansible自动化运维学习笔记3
运维必备 | ansible 自动化运维工具之变量的定义与调用
前言:在编写ansible脚本中往往会使用变量,它能让我们的工作变得更加灵活,但是在ansible中变量的使用方式有很多种,下面跟着【WeiyiGeek】作者通过一个个简单示例来进行一一讲解,赶快来一起来学习吧。
全栈工程师修炼指南
2023/10/31
1.8K0
运维必备 | ansible 自动化运维工具之变量的定义与调用
Ansible自动化运维学习笔记1
基础概念 什么是ansible? 答:它是一个”配置管理工具”,它是一个Linux系统上的”自动化运维工具”;
全栈工程师修炼指南
2020/10/26
4.2K0
Ansible自动化运维学习笔记1
Ansible自动化运维学习笔记5
描述:”过滤器(filters)”可以帮助我们对数据进行处理,ansible中的过滤器功能来自于jinja2模板引擎,我们可以借助jinja2的过滤器功能在ansible中对数据进行各种处理;很多其他的过滤器有些是jinja2内置的有些是ansible特有,变量和过滤器之间采用类似于管道符进行拼接;
全栈工程师修炼指南
2020/10/23
3.4K0
Ansible自动化运维学习笔记1
基础概念 什么是ansible? 答:它是一个Linux系统上的”自动化运维工具”,类似一个”配置管理工具”;
全栈工程师修炼指南
2022/09/28
2K0
Ansible自动化运维学习笔记1
Ansible自动化运维工具
Ansible 是一个极其简单的 IT 自动化平台,可让您的应用程序和系统更易于部署和维护。从代码部署到网络配置再到云管理,使用一种接近简单英语的语言,使用 SSH 实现一切自动化,无需在远程系统上安装代理。
BoyChai
2022/06/22
2.7K0
Ansible自动化运维工具
自动化运维实践 | Ansible变量
2)用户无须自定义。Ansible会在执行playbook之前去远程主机上搜集关于远程节点系统信息的变量。
小土豆Yuki
2020/08/17
2.1K0
自动化运维实践 | Ansible变量
Ansible自动化运维配置与应用(结合实例)
注: command模块和shell模块的区别是:shell模块支持“管道符”及脚本。
阿dai学长
2019/04/03
2.9K0
自动化运维利器Ansible要点汇总
  由于大部分互联网公司服务器环境复杂,线上线下环境、测试正式环境、分区环境、客户项目环境等造成每个应用都要重新部署,而且服务器数量少则几十台,多则千台,若手工一台台部署效率低下,且容易出错,不利后期运维,因此需要Ansible或者Saltstack来解决此问题。
欢醉
2021/10/26
2.2K0
Ansible概要
检验清单是否正确。-i用于指定清单文件,all和master是被管理节点的模式(pattern)。
宅蓝三木
2024/10/09
4270
Ansible概要
Ansible playbook 编程
3、 该用户也被 ansible 使用,因为几乎所有的生产环境都是禁止 root 远程登录的(因此该 yun 用户也进行了 sudo 提权)。
踏歌行
2020/10/15
1.9K0
Ansible playbook 编程
Ansible PlayBook语法
playbook是由一个或多个play组成的列表,play的主要功能在于将事先归并为一组的主机装扮成事先通过Ansible中的tasks定义好的角色(play的内容被称为tasks,即任务),从根本上来讲所谓tasks无非是调用Ansible的一个module,将多个play组织在一个playbook中即可以让它们联同起来按事先编排的机制一同工作.
王 瑞
2022/12/28
2.5K0
ansible 变量
和大多数编程语言一样,ansible变量名应该由字符、数字、下划线组成,变量名需要以字母开头,ansible内置的关键字不能作为变量名。
张琳兮
2019/03/14
2.3K0
ansible-语法
上述使用vars定义一个变量testvar1 并使用{{ testvar1 }}来引用
丁D
2022/08/12
5940
ansible-语法
自动化运维工具Ansible之Tests测验详解
3、 该用户也被 ansible 使用,因为几乎所有的生产环境都是禁止 root 远程登录的(因此该 yun 用户也进行了 sudo 提权)。
踏歌行
2020/10/15
7130
自动化运维工具Ansible之Tests测验详解
自动化运维利器 ansible-入门
ansible 是一款强大的配置管理工具,诣在帮助系统管理员高效率地管理成百上千台主机。设想一个主机是一个士兵,那么有了 ansible ,作为系统管理员的你就是一个将领,你可以通过口头命令,即一次下发一条命令(ansible ad-hoc 模式)方式让一个或一组或全部的士兵按你的指令行事,也可以将多条命令写在纸上(ansible playbook 模式), 需要执行命令时只需要提供这张纸即可。你可以让多个士兵同时做相同或不同的事情,可以方便的让新加入的士兵快速加入已有的兵种队伍,也以快速改变兵种(配置管理),一句话,士兵都严格听你的,你做好命令的设计,ansible 自动帮你发布和执行。
somenzz
2020/12/10
1.7K0
《Ansible自动化运维:技术与最佳实践》第三章读书笔记
本章主要通过对 Ansible 经常使用的组件进行讲解,使对 Ansible 有一个更全面的了解,主要包含以下内容:
武培轩
2019/09/24
1.2K0
第五章·自动化运维工具-Ansible流程控制
-多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。 -擅长Web集群架构与自动化运维,曾负责国内某大型金融公司运维工作。 -devops项目经理兼DBA。 -开发过一套自动化运维平台(功能如下): 1)整合了各个公有云API,自主创建云主机。 2)ELK自动化收集日志功能。 3)Saltstack自动化运维统一配置管理工具。 4)Git、Jenkins自动化代码上线及自动化测试平台。 5)堡垒机,连接Linux、Windows平台及日志审计。 6)SQL执行及审批流程。 7)慢查询日志分析web界面。
DriverZeng
2022/09/26
7010
第五章·自动化运维工具-Ansible流程控制
相关推荐
Ansible自动化运维学习笔记2
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档