The following are some of the commands that can be used for database administration for mongo.
1. getLastError
> db.getLastError()
E11000 duplicate key error collection: sample1.test1 index: _id_ dup key: { : ObjectId('4c90bf054b7f54c607f6b23c') }
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2019-03-20T14:44:51.434Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1
}
1. getLastError
> db.getLastError()
E11000 duplicate key error collection: sample1.test1 index: _id_ dup key: { : ObjectId('4c90bf054b7f54c607f6b23c') }
This command will help in getting details about the last failed command execution.For example in the above scenario an insert had failed to test1 collection since the primary key already existed for _id field.
2. isMaster
> db.isMaster(){
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2019-03-20T14:44:51.434Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1
}
It lets us know whether the current mongo instance we are accessing is the master instance in a replication setup of mongo.
3. Drop Collection
> db.items.drop()
true
It can be used to drop a specific collection from the database, as in this case collection items is dropped.
4. Server Status
> db.serverStatus()
{
"host" : "localhost.localdomain",
"version" : "4.0.6",
"process" : "mongod",
"pid" : NumberLong(3817),
"uptime" : 764,
"uptimeMillis" : NumberLong(763716),
"uptimeEstimate" : NumberLong(763),
"localTime" : ISODate("2019-03-20T14:49:49.322Z"),
"asserts" : {------------
------------
"storage" : {
"freelist" : {
"search" : {
"bucketExhausted" : NumberLong(0),
"requests" : NumberLong(0),
"scanned" : NumberLong(0)
}
}
},
},
"ok" : 1
}
Server status gives a statistics on what is happening on the database.
5.Run Command
>db.runCommand({<commandName>:value})
>> db.runCommand({isMaster:1})
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2019-03-21T07:30:15.724Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1
}
{
"inprog" : [
{
"host" : "localhost.localdomain:27017",
"desc" : "conn5",
"connectionId" : 5,
"client" : "127.0.0.1:33962",
"appName" : "MongoDB Shell",
"clientMetadata" : {
"application" : {
"name" : "MongoDB Shell"
},
"driver" : {
"name" : "MongoDB Internal Client",
"version" : "4.0.6"
},
"os" : {
"type" : "Linux",
"name" : "Red Hat Enterprise Linux Server release 7.0 (Maipo)",
"architecture" : "x86_64",
"version" : "Kernel 3.10.0-123.el7.x86_64"
}
},
"active" : true,
"currentOpTime" : "2019-03-21T00:32:25.307-0700",
"opid" : 16326,
"lsid" : {
"id" : UUID("7f3598ab-9cc1-47ec-9a2f-e81130cb7b58"),
"uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
},
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(80),
"op" : "command",
"ns" : "admin.$cmd.aggregate",
"command" : {
"currentOp" : 1,
"lsid" : {
"id" : UUID("7f3598ab-9cc1-47ec-9a2f-e81130cb7b58")
},
"$db" : "admin"
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
}
],
"ok" : 1
}
11. db.getLogComponents()
{
"verbosity" : 0,
"accessControl" : {
"verbosity" : -1
},
"command" : {
"verbosity" : -1
},
"control" : {
"verbosity" : -1
},
"executor" : {
"verbosity" : -1
},
"geo" : {
"verbosity" : -1
},
"index" : {
"verbosity" : -1
},
"network" : {
"verbosity" : -1,
"asio" : {
"verbosity" : -1
},
"bridge" : {
"verbosity" : -1
}
},
"query" : {
"verbosity" : -1
},
"replication" : {
"verbosity" : -1,
"heartbeats" : {
"verbosity" : -1
},
"rollback" : {
"verbosity" : -1
}
},
"sharding" : {
"verbosity" : -1,
"shardingCatalogRefresh" : {
"verbosity" : -1
}
},
"storage" : {
"verbosity" : -1,
"recovery" : {
"verbosity" : -1
},
"journal" : {
"verbosity" : -1
}
},
"write" : {
"verbosity" : -1
},
"ftdc" : {
"verbosity" : -1
},
"tracking" : {
"verbosity" : -1
},
"transaction" : {
"verbosity" : -1
}
}
12.db.adminCommand("getLog":"global")
>> db.runCommand({isMaster:1})
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2019-03-21T07:30:15.724Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1
}
We can run any database administration command using the db.runCommand method.
6.Current Ops
> db.currentOp(){
"inprog" : [
{
"host" : "localhost.localdomain:27017",
"desc" : "conn5",
"connectionId" : 5,
"client" : "127.0.0.1:33962",
"appName" : "MongoDB Shell",
"clientMetadata" : {
"application" : {
"name" : "MongoDB Shell"
},
"driver" : {
"name" : "MongoDB Internal Client",
"version" : "4.0.6"
},
"os" : {
"type" : "Linux",
"name" : "Red Hat Enterprise Linux Server release 7.0 (Maipo)",
"architecture" : "x86_64",
"version" : "Kernel 3.10.0-123.el7.x86_64"
}
},
"active" : true,
"currentOpTime" : "2019-03-21T00:32:25.307-0700",
"opid" : 16326,
"lsid" : {
"id" : UUID("7f3598ab-9cc1-47ec-9a2f-e81130cb7b58"),
"uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
},
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(80),
"op" : "command",
"ns" : "admin.$cmd.aggregate",
"command" : {
"currentOp" : 1,
"lsid" : {
"id" : UUID("7f3598ab-9cc1-47ec-9a2f-e81130cb7b58")
},
"$db" : "admin"
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
}
],
"ok" : 1
}
This method lets us know what is currently running on the server.
For performance, can check “secs_running” parameters.
Incase of replication, secondary reading from oplog using
getmore.
To Know total running operations:
> db.currentOp().inprog.length
1
7.Kill Ops
> db.killOp(16326)
{ "info" : "attempting to kill op", "ok" : 1 }
The method killOp() can be used kill the operation running on database using opId from
currentOps.
8. Collection Stats
> db.items.stats()
{
"ns" : "sample1.items",
"size" : 360,
"count" : 10,
"avgObjSize" : 36,
"storageSize" : 16384,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
},
------
"nindexes" : 1,
"totalIndexSize" : 16384,
"indexSizes" : {
"_id_" : 16384
},
"ok" : 1
}
To know details of collections we can check that using db.coll.stats()
9.Mongo Stat
#mongostat
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 157b 62.6k 1 Mar 24 00:37:24.330
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 157b 62.9k 1 Mar 24 00:37:25.330
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 157b 62.7k 1 Mar 24 00:37:26.335
*0 *0 *0 *0 0 2|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 159b 63.4k 1 Mar 24 00:37:27.328
10.Mongo Top
# mongotop
2019-03-24T00:38:10.492-0700 connected to: 127.0.0.1
ns total read write 2019-03-24T00:38:11-07:00
admin.$cmd.aggregate 0ms 0ms 0ms
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
config.system.profile 0ms 0ms 0ms
config.system.sessions 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
sample1.items 0ms 0ms 0ms
sample1.places 0ms 0ms 0ms
sample1.sample1 0ms 0ms 0ms
9.Mongo Stat
#mongostat
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 157b 62.6k 1 Mar 24 00:37:24.330
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 157b 62.9k 1 Mar 24 00:37:25.330
*0 *0 *0 *0 0 1|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 157b 62.7k 1 Mar 24 00:37:26.335
*0 *0 *0 *0 0 2|0 0.0% 0.1% 0 1.05G 90.0M 0|0 1|0 159b 63.4k 1 Mar 24 00:37:27.328
10.Mongo Top
# mongotop
2019-03-24T00:38:10.492-0700 connected to: 127.0.0.1
ns total read write 2019-03-24T00:38:11-07:00
admin.$cmd.aggregate 0ms 0ms 0ms
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
config.system.profile 0ms 0ms 0ms
config.system.sessions 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
sample1.items 0ms 0ms 0ms
sample1.places 0ms 0ms 0ms
sample1.sample1 0ms 0ms 0ms
11. db.getLogComponents()
{
"verbosity" : 0,
"accessControl" : {
"verbosity" : -1
},
"command" : {
"verbosity" : -1
},
"control" : {
"verbosity" : -1
},
"executor" : {
"verbosity" : -1
},
"geo" : {
"verbosity" : -1
},
"index" : {
"verbosity" : -1
},
"network" : {
"verbosity" : -1,
"asio" : {
"verbosity" : -1
},
"bridge" : {
"verbosity" : -1
}
},
"query" : {
"verbosity" : -1
},
"replication" : {
"verbosity" : -1,
"heartbeats" : {
"verbosity" : -1
},
"rollback" : {
"verbosity" : -1
}
},
"sharding" : {
"verbosity" : -1,
"shardingCatalogRefresh" : {
"verbosity" : -1
}
},
"storage" : {
"verbosity" : -1,
"recovery" : {
"verbosity" : -1
},
"journal" : {
"verbosity" : -1
}
},
"write" : {
"verbosity" : -1
},
"ftdc" : {
"verbosity" : -1
},
"tracking" : {
"verbosity" : -1
},
"transaction" : {
"verbosity" : -1
}
}
12.db.adminCommand("getLog":"global")
{
"totalLinesWritten" : 42,
"log" : [
0, $db: \"test\" } numYields:0 reslen:224 locks:{} protocol:op_msg 0ms",
"2010-09-15T04:41:57.871+0530 D INDEX [TTLMonitor] deleted: 0",
"2010-09-15T04:42:00.222+0530 I COMMAND [conn3] successfully set parameter logComponentVerbosity to { index: { verbosity: 0.0 } } (was { verbosity: 1, accessControl: { verbosity: -1 }, command: { verbosity: -1 }, control: { verbosity: -1 }, executor: { verbosity: -1 }, geo: { verbosity: -1 }, index: { verbosity: 1 }, network: { verbosity: -1, asio: { verbosity: -1 }, bridge: { verbosity: -1 } }, query: { verbosity: -1 }, replication: { verbosity: -1, heartbeats: { verbosity: -1 },
Log Message Severity Levels
F- Fatal
E-Error
W-Warning
I-Informational(Verbosity Level 0)
D-Debug(Verbosity Level 1-5)
Comments
Post a Comment