mongodb 配置慢查詢日誌

參考:
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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章