On this page本页内容
dropDatabase
The dropDatabase
command drops the current database, deleting the associated data files.dropDatabase
命令删除当前数据库,删除关联的数据文件。
The command has the following form:命令的格式如下:
{ dropDatabase: 1, writeConcern: <document>, comment: <any> }
The command takes the following optional field:该命令接受以下可选字段:
writeConcern |
{ w: <value>, j: <boolean>, wtimeout: <number> }
|
comment |
|
mongosh
also provides the helper method 还提供了助手方法db.dropDatabase()
.db.dropDatabase()
。
Starting in version 4.2.2, the operation takes an exclusive (X) database lock only.从4.2.2版开始,该操作只接受独占(X)数据库锁。
In versions 3.6-4.2.1, the operation takes an exclusive (X) database lock while dropping the collections in the database but a global lock when dropping the now-empty database.在3.6-4.2.1版本中,当删除数据库中的集合时,该操作将使用独占(X)数据库锁,但当删除现在为空的数据库时,该将使用全局锁。
This command does not delete the users associated with the current database. 此命令不会删除与当前数据库关联的用户。To drop the associated users, run the 要删除关联的用户,请在要删除的数据库中运行dropAllUsersFromDatabase
command in the database you are deleting.dropAllUsersFromDatabase
命令。
Starting in MongoDB 4.4, the 从MongoDB 4.4开始,db.dropDatabase()
method and dropDatabase
command abort any in-progress index builds on collections in the target database before dropping the database. db.dropDatabase()
方法和dropDatabase
命令会在删除数据库之前中止目标数据库中的集合上的任何正在进行的索引构建。Aborting an index build has the same effect as dropping the built index. 中止索引生成与删除生成的索引具有相同的效果。Prior to MongoDB 4.4, attempting to drop a database that contains a collection with an in-progress index build results in an error, and the database is not dropped.在MongoDB 4.4之前,尝试删除包含正在进行索引构建的集合的数据库会导致错误,并且不会删除该数据库。
For replica sets or shard replica sets, aborting an index on the primary does not simultaneously abort secondary index builds. MongoDB attempts to abort the in-progress builds for the specified indexes on the primary and if successful creates an associated 对于副本集或分片副本集,中止主索引不会同时中止辅助索引生成。MongoDB尝试中止primary上指定索引的正在进行的构建,如果成功,将创建一个相关的abort
oplog entry. abort
oplog条目。Secondary members with replicated in-progress builds wait for a commit or abort oplog entry from the primary before either committing or aborting the index build.具有复制的正在进行的生成的Secondary成员在提交或中止索引生成之前,等待来自主成员的提交或中止oplog条目。
Changed in version 4.4.在版本4.4中更改。
At minimum, dropDatabase
waits until all collections drops in the database have propagated to a majority of the replica set members (i.e. uses the write concern "majority"
).dropDatabase
至少会等待,直到数据库中的所有集合都已传播到大多数副本集成员(即使用写关注点"majority"
)。
If you specify a write concern that requires acknowledgement from fewer than the majority, the command uses write concern 如果指定的写关注点需要少数人的确认,则命令使用写关注点"majority"
."majority"
。
If you specify a write concern that requires acknowledgement from more than the majority, the command uses the specified write concern.如果指定的写入关注点需要多数人的确认,则命令将使用指定的写入关心点。
When issued on a sharded cluster, MongoDB converts the specified write concern to 当在分片集群上发出时,MongoDB将指定的写入关注转换为"majority"
."majority"
。
If you intend to create a new database with the same name as the dropped database, you must follow these additional steps for using the 如果您打算创建一个与删除的数据库同名的新数据库,则必须按照以下附加步骤使用dropDatabase
command, specific to your version of MongoDB:dropDatabase
命令,具体取决于您的MongoDB版本:
For MongoDB 4.4 and later, you must:对于MongoDB 4.4及更高版本,您必须:
dropDatabase
command on a mongos
.mongos
上运行dropDatabase
命令。dropDatabase
command once more on a mongos
.mongos
上再次运行dropDatabase
命令。For MongoDB 4.2, you must:对于MongoDB 4.2,您必须:
dropDatabase
command on a mongos
.mongos
上运行dropDatabase
命令。dropDatabase
command once more on a mongos
.mongos
上再次运行dropDatabase
命令。flushRouterConfig
command on allmongos
instances before reading or writing to that database.mongos
实例上使用flushRouterConfig
命令。For MongoDB 4.0 and earlier, you must:对于MongoDB 4.0及更早版本,您必须:
dropDatabase
command on a mongos
.mongos
上运行dropDatabase
命令。dropDatabase
command again directly from the primary.dropDatabase
命令。Connect to a 连接到mongos
, switch to the config database, and remove any reference to the removed namespace from the databases
, collections
, chunks
, tags
, and locks
collections:mongos
,切换到config
数据库,并从databases
、collections
、chunks
、tags
和locks
集合中删除对已删除命名空间的任何引用:
use config db.collections.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) db.databases.remove( { _id: "DATABASE" }, {writeConcern: {w: 'majority' }} ) db.chunks.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) db.tags.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) db.locks.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
Where 其中DATABASE
represents the namespace of the database you just dropped.DATABASE
表示刚刚删除的数据库的名称空间。
Connect to the primary of each shard, and remove any reference to the removed namespace from the 连接到每个分片的primary,并从cache.databases
, cache.collections
, and cache.chunks.DATABASE.COLLECTION
collections:cache.databases
、cache.collections
和cache.chunks.DATABASE.COLLECTION
集合中删除对已删除命名空间的任何引用:
db.getSiblingDB("config").cache.databases.remove({_id:"DATABASE"}, {writeConcern: {w: 'majority' }}); db.getSiblingDB("config").cache.collections.remove({_id:/^DATABASE.*/}, {writeConcern: {w: 'majority' }}); db.getSiblingDB("config").getCollectionNames().forEach(function(y) { if(y.indexOf("cache.chunks.DATABASE.") == 0) db.getSiblingDB("config").getCollection(y).drop() })
Where 其中DATABASE
represents the namespace of the database you just dropped.DATABASE
表示刚刚删除的数据库的名称空间。
flushRouterConfig
command on allmongos
instances before reading or writing to that database.mongos
实例上使用flushRouterConfig
命令。These steps ensure that all cluster nodes refresh their metadata cache, which includes the location of the primary shard for the new database. 这些步骤确保所有集群节点刷新其元数据缓存,其中包括新数据库的primary分片的位置。Otherwise, you may miss data on reads, and may not write data to the correct shard. 否则,您可能会在读取时丢失数据,并且可能无法将数据写入正确的分片。To recover, you must manually intervene.要恢复,必须手动干预。
Starting in MongoDB 5.0, the 从MongoDB 5.0开始,如果您试图从dropDatabase
command and the db.dropDatabase()
method return an error if you try to drop the admin database or the config database from a mongos
.mongos
中删除admin
数据库或config
数据库,dropDatabase
命令和db.dropDatabase()
方法将返回一个错误。
Dropping the admin database or the config database can leave your cluster in an unusable state.删除admin
数据库或config
数据库可能会使集群处于不可用状态。
The db.dropDatabase()
method and dropDatabase
create an invalidate
Event for any Change Streams opened on the dropped database or opened on the collections in the dropped database.db.dropDatabase()
方法和dropDatabase
为在已删除的数据库上打开的或在已删除数据库中的集合上打开的任何更改流创建一个invalidate
事件。
The following example in mongosh
uses the use <database>
operation to switch the current database to the temp
database and then uses the dropDatabase
command to drop the temp
database:mongosh
中的以下示例使用use <database>
操作将当前数据库切换到temp
数据库,然后使用dropDatabase
命令删除temp
数据库:
use temp db.runCommand( { dropDatabase: 1 } )