在我的MongoDB数据库名称中有一个错别字,我正在寻找重命名数据库。
我可以像这样复制和删除…
db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();
是否有重命名数据库的命令?
在我的MongoDB数据库名称中有一个错别字,我正在寻找重命名数据库。
我可以像这样复制和删除…
db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();
是否有重命名数据库的命令?
当前回答
替代解决方案:您可以转储您的数据库,并恢复到不同的名称。根据我的经验,它比db.copyDatabase()快得多。
$ mongodump -d old_db_name -o mongodump/
$ mongorestore -d new_db_name mongodump/old_db_name
http://docs.mongodb.org/manual/tutorial/backup-with-mongodump/
这是目前官方推荐的数据库重命名方法,因为copyDatabase在MongoDB 4.2中被删除了:
"copydb"命令已弃用,请使用以下两个命令: mongodb(用于备份数据) Mongorestore(将数据从mongodb恢复到一个新的命名空间)
其他回答
你可以这样做,如果你使用MongoDB < 4.2 (ref):
db.copyDatabase("db_to_rename","db_renamed","localhost")
use db_to_rename
db.dropDatabase();
编者按:这与问题本身使用的方法相同,但已被证明对其他人有用。
从4.2版开始,copyDatabase已弃用。从现在开始,我们应该使用:mongump和mongorestore。
假设我们有一个名为:old_name的数据库,我们希望将其重命名为new_name。
首先,我们必须转储数据库:
mongodump --archive="old_name_dump.db" --db=old_name
如果您必须作为用户进行身份验证,则使用:
mongodump -u username --authenticationDatabase admin \
--archive="old_name_dump.db" --db=old_name
现在我们将db转储为一个名为:old_name_dump.db的文件。
用新名称恢复:
mongorestore --archive="old_name_dump.db" --nsFrom="old_name.*" --nsTo="new_name.*"
同样,如果您需要进行身份验证,请将以下参数添加到命令中:
-u username --authenticationDatabase admin
参考:https://docs.mongodb.com/manual/release-notes/4.2-compatibility/ # remove-support-for-the-copydb-and-clone-commands
上面的过程很慢,你可以使用下面的方法,但你需要一个集合一个集合地移动到另一个db。
use admin
db.runCommand({renameCollection: "[db_old_name].[collection_name]", to: "[db_new_name].[collection_name]"})
替代解决方案:您可以转储您的数据库,并恢复到不同的名称。根据我的经验,它比db.copyDatabase()快得多。
$ mongodump -d old_db_name -o mongodump/
$ mongorestore -d new_db_name mongodump/old_db_name
http://docs.mongodb.org/manual/tutorial/backup-with-mongodump/
这是目前官方推荐的数据库重命名方法,因为copyDatabase在MongoDB 4.2中被删除了:
"copydb"命令已弃用,请使用以下两个命令: mongodb(用于备份数据) Mongorestore(将数据从mongodb恢复到一个新的命名空间)
不,没有。见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...