如何重命名 MongoDB 数据库? [英] How do you rename a MongoDB database?

查看:37
本文介绍了如何重命名 MongoDB 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 MongoDB 数据库名称有误,我想重命名数据库.

There's a typo in my MongoDB database name and I'm looking to rename the database.

我可以复制并像这样删除...

I can copy and delete like so...

db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();

是否有重命名数据库的命令?

Is there a command to rename a database?

推荐答案

不,没有.请参阅 https://jira.mongodb.org/browse/SERVER-701

不幸的是,由于数据库元数据存储在原始(默认)存储引擎中的方式,这不是我们要实现的简单功能.在 MMAPv1 文件中,描述每个集合和索引的命名空间(例如:dbName.collection)包括数据库名称,因此要重命名一组数据库文件,必须重写每个命名空间字符串.这会影响:

Unfortunately, this is not an simple feature for us to implement due to the way that database metadata is stored in the original (default) storage engine. In MMAPv1 files, the namespace (e.g.: dbName.collection) that describes every single collection and index includes the database name, so to rename a set of database files, every single namespace string would have to be rewritten. This impacts:

  • .ns 文件
  • 集合的每个编号文件
  • 每个索引的命名空间
  • 每个集合和索引的内部唯一名称
  • system.namespaces 和 system.indexes 的内容(或将来的等价物)
  • 我可能遗漏的其他地点

这只是为了在独立 mongod 实例中完成单个数据库的重命名.对于副本集,需要在每个副本节点上完成上述操作,并且在每个节点上,每个引用该数据库的 oplog 条目都必须以某种方式失效或重写,然后如果它是一个分片集群,还需要添加这些如果数据库被分片,则更改到每个分片,而且配置服务器拥有所有分片元数据,即命名空间及其全名.

This is just to accomplish a rename of a single database in a standalone mongod instance. For replica sets the above would need to be done on every replica node, plus on each node every single oplog entry that refers this database would have to be somehow invalidated or rewritten, and then if it's a sharded cluster, one also needs to add these changes to every shard if the DB is sharded, plus the config servers have all the shard metadata in terms of namespaces with their full names.

在实时系统上绝对没有办法做到这一点.

There would be absolutely no way to do this on a live system.

要离线执行,需要重新编写每个数据库文件以适应新名称,此时它会像当前的copydb"命令一样慢...

To do it offline, it would require re-writing every single database file to accommodate the new name, and at that point it would be as slow as the current "copydb" command...

这篇关于如何重命名 MongoDB 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆