Too many mongo commands? You need to remember these 4 at least

Since the last article talked about non-relational database MongoDB index , everyone's response has been good, today we will continue to talk about mongo service commands, through the following commands to let you know more about your mongo service application status.

One, use the db.currentOp() function

This function will list all the operations in progress in the database. The most commonly used function is to find slower operations.

The unique identifier of the opid operation, which can be used to terminate the operation, and the execution of the operation can be terminated by db.killOp(opid).

op represents the type of operation, query, insert, update, delete

secs_running represents the time the operation has been executed. Use it to determine those queries that take too long or occupy too much database resources.

The default currentOp() operation displays a lot of data. You can add filter conditions to display the results that match the display, as shown below.

b6e62542d2120f974fe5efd6ae699b12.webp

Two, system analyzer setProfilingLevel

For operations that take too long to query, you can use the system profiler to enable setProfilingLevel.

Set 0, close the analyzer

Set 2, open the analyzer will record all content, all read and write records in the system.profile collection, but this will cause a certain performance loss.

Setting 1, only displays long time-consuming (> 100ms) operations by default. You can also set it by yourself, such as 300ms. Do not set the value too low here, otherwise, even if the analyzer is closed, operations exceeding the slowms value will appear in the log.

In this way, we can better discover where our query is time-consuming, and we can optimize our application at any time.

scanv_rs:PRIMARY> db.setProfilingLevel(1,300) 
{
    "was" : 0, # 状态
    "slowms" : 100, # 慢查询时间
    "sampleRate" : 1,
    "ok" : 1,
    "operationTime" : Timestamp(1541420614, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1541420614, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

Three, use the stats function to display the value of a collection or database

For example, db.users.stats() and db.stats() will slow down database operations when listing database information online. You need to be careful not to perform such operations during peak periods.

scanv_rs:PRIMARY> db.stats()
    "objects" : 1000000,  # 文档总数
    "avgObjSize" : 87.88889,
    "dataSize" : 87888890, # 此数据库数据占用空间大小
    "storageSize" : 27336704, # 数据库正在使用的总空间大小
    "numExtents" : 0,
    "indexes" : 3,
    "indexSize" : 33841152,
    "fsUsedSize" : 223878615040,
    "fsTotalSize" : 249779191808,
    "ok" : 1,
    "operationTime" : Timestamp(1541421778, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1541421778, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

Four, use mongotop and mongostat

mongotop is similar to the unix command top command, mongostat provides server information, by default it outputs a list of current status once per second

2b54cfffa99c47c2566709bd5ecee4f7.webp

Through the above commands, we can better understand what happened to our service, and what to do to change the state of the application, such as adding indexes, adjusting query conditions, etc., to keep our service in the best state at all times.


Guess you like

Origin blog.51cto.com/15009257/2552304