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

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

问题描述

我的MongoDB数据库名称有一个错字,我正在重命名数据库。



我可以复制并删除,如此...

 code> db.copyDatabase('old_name','new_name'); 
use old_name
db.dropDatabase();是否有重命名数据库的命令?


h2_lin>解决方案

没有没有。请参见 https://jira.mongodb.org/browse/SERVER-701


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




  • .ns文件

  • >
  • 每个索引的命名空间

  • 每个集合和索引的内部唯一名称

  • system.namespaces和系统的内容



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



将绝对没有办法在实时系统上执行此操作。



要离线操作,需要重新编写每个数据库文件以容纳新名称,此时,与当前的copydb命令一样慢...



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?

解决方案

No there isn't. See https://jira.mongodb.org/browse/SERVER-701

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:

  • the .ns file
  • every single numbered file for the collection
  • the namespace for every index
  • internal unique names of each collection and index
  • contents of system.namespaces and system.indexes (or their equivalents in the future)
  • other locations I may be missing

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.

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天全站免登陆