MongoDB 3.6 开启慢查询

参考:
Profiling Levels:支持一下级别。
0 默认的profiler level,profiler 关闭并且不收集数据。
1 profiler 收集超过slowms的操作数据。
2 profiler 收集所有的数据。

设置收集数据:设置级别为1,慢查询标准为200ms.
 rs0:PRIMARY> db.setProfilingLevel(1,200)
{
        "was" : 1,
        "slowms" : 200,
        "sampleRate" : 1,
        "ok" : 1,
        "operationTime" : Timestamp(1536309385, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536309385, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

设置语句:
db.setProfilingLevel(1, { slowms: 200 })
查询验证配置:
rs0:PRIMARY> db.getProfilingStatus()
--查询级别:
rs0:PRIMARY> db.getProfilingLevel()
1
--关闭设置Profiling:
db.setProfilingLevel(0)

--设置慢查询抽样比例:
rs0:PRIMARY> db.setProfilingLevel(1, { sampleRate: 0.42 })
{
        "was" : 1,
        "slowms" : 200,
        "sampleRate" : 1,
        "ok" : 1,
        "operationTime" : Timestamp(1536309735, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536309735, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
rs0:PRIMARY>
rs0:PRIMARY> db.getProfilingStatus()
{
        "was" : 1,
        "slowms" : 200,
        "sampleRate" : 0.42,
        "operationTime" : Timestamp(1536309775, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536309775, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}


注释:
By default, sampleRate is set to 1.0, meaning all slow operations are profiled.
When sampleRate is set between 0 and 1, databases with profiling level 1 will
only profile a randomly sampled percentage of slow operations according to sampleRate.

sampleRate 参数的默认值是1.0,即收集所有慢查询,此参数的范围值是0到1.当级别设置为1会按照sampleRate 设置的值随机抽取百分比的慢操作。

除了在运行时设置慢查询外,还可以在命令模式下启动时设置:
mongod --profile 1 --slowms 15 --slowOpSampleRate 0.5
也可以将参数放置到配置文件:
profile=1
slowms=200
slowOpSampleRate=0.5

profiler的数据存储在MongoDB中的system.profile collection。
在主库修改system.profile的集合的大小:
步骤如下:
Disable profiling.
Drop the system.profile collection.
Create a new system.profile collection.
Re-enable profiling.

--修改为4M:
db.setProfilingLevel(0)
db.system.profile.drop()
db.createCollection( "system.profile", { capped: true, size:4000000 } )
db.setProfilingLevel(1)

--慢查询操作的查询:

--慢查询操作的可视化:
mongoDB的慢查询操作可以结合PMM的监控PMM-QAN,但是支持MongoDB 3.2及以上版本。
需要2个步骤:
1.设置必需的账号
2.开启profiler。
在MongoDB设置账号和权限:
db.getSiblingDB("admin").createUser({
    user: "mongodb_exporter",
    pwd: "mongodb_exporter",
    roles: [
        { role: "clusterMonitor", db: "admin" },
        { role: "read", db: "local" }
    ]
})

开启profiler:
$ mongod --dbpath=DATABASEDIR --profile 2 --slowms 200 --rateLimit 100
或者写入配置文件:
operationProfiling:
   slowOpThresholdMs: 200
   mode: slowOp
   rateLimit: 100

https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler/
https://www.percona.com/doc/percona-monitoring-and-management/conf-mongodb.html

 

你可能感兴趣的:(MongoDB)