摘要:命令行方式獲取顯示當前的用戶操作。類似于的可以監控所有慢的以及不慢的查詢。以及其他第三方鏈接性能調優場景現實的首頁我們希望現實最近發布的條。自動刪除舊文檔為了給新文檔創建空間,在集合中自動刪除老舊的文檔,不需要執行額外的腳本和操作。
轉載請注明出處 http://www.paraller.com
原文排版地址 http://www.paraller.com/2016/10/22/mongo%E7%9B%91%E6%8E%A7%E5%8F%8A%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98/
mongo監控的策略主要有以下兩種:
一系列的工具能夠報告實時信息
數據庫命令能夠返回當前數據庫的詳細精確信息
mongostatmongostat captures and returns the counts of database operations by type (e.g. insert, query, update, delete, etc.). These counts report on the load distribution on the server.
Use mongostat to understand the distribution of operation types and to inform capacity planning. See the mongostat manual for details.
root@db94dfee43b5:/# mongostatinsert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time *0 *0 *0 *0 0 2|0 0.0% 0.1% 0 270M 59.0M 0|0 0|0 160b 45.5k 2 Aug 29 08:48:07.749 *0 *0 *0 *0 0 1|0 0.0% 0.1% 0 270M 59.0M 0|0 0|0 157b 44.8k 2 Aug 29 08:48:08.749
每個字段代表的意思
inserts
The number of objects inserted into the database per second. If followed by an asterisk (e.g. *), the datum refers to a replicated operation.
query
The number of query operations per second.
update
The number of update operations per second.
delete
The number of delete operations per second.
getmore
The number of get more (i.e. cursor batch) operations per second.
command
The number of commands per second. On slave and secondary systems, mongostat presents two values separated by a pipe character (e.g. |), in the form of local|replicated commands.
flushes
Changed in version 3.0.
For the WiredTiger Storage Engine, flushes refers to the number of WiredTiger checkpoints triggered between each polling interval.
For the MMAPv1 Storage Engine, flushes represents the number of fsync operations per second.
dirty
New in version 3.0.
Only for WiredTiger Storage Engine. The percentage of the WiredTiger cache with dirty bytes, calculated by wiredTiger.cache.tracked dirty bytes in the cache / wiredTiger.cache.maximum bytes configured.
used
New in version 3.0.
Only for WiredTiger Storage Engine. The percentage of the WiredTiger cache that is in use, calculated by wiredTiger.cache.bytes currently in the cache / wiredTiger.cache.maximum bytes configured.
mapped
Changed in version 3.0.
Only for MMAPv1 Storage Engine. The total amount of data mapped in megabytes. This is the total data size at the time of the last mongostat call.
vsize
The amount of virtual memory in megabytes used by the process at the time of the last mongostat call.
res
The amount of resident memory in megabytes used by the process at the time of the last mongostat call.
faults
Changed in version 3.0.
Only for MMAPv1 Storage Engine. The number of page faults per second.
Changed in version 2.1: Before version 2.1, this value was only provided for MongoDB instances running on Linux hosts.
lr
New in version 3.2.
Only for MMAPv1 Storage Engine. The percentage of read lock acquisitions that had to wait. mongostat displays lr|lw if a lock acquisition waited.
lw
New in version 3.2.
Only for MMAPv1 Storage Engine. The percentage of write lock acquisitions that had to wait. mongostat displays lr|lw if a lock acquisition waited.
locked
Changed in version 3.0: Only appears when mongostat runs against pre-3.0 versions of MongoDB instances.
The percent of time in a global write lock.
idx miss
Changed in version 3.0.
Only for MMAPv1 Storage Engine. The percent of index access attempts that required a page fault to load a btree node. This is a sampled value.
qr
The length of the queue of clients waiting to read data from the MongoDB instance.
qw
The length of the queue of clients waiting to write data from the MongoDB instance.
ar
The number of active clients performing read operations.
aw
The number of active clients performing write operations.
netIn
The amount of network traffic, in bytes, received by the MongoDB instance.
This includes traffic from mongostat itself.
conn
The total number of open connections.
set
The name, if applicable, of the replica set.
Add Fields to mongostat Output
root@db94dfee43b5:/# mongostat -O "host,version,network.numRequests=network requests" insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time host version network requests *0 *0 *0 *0 0 2|0 0.0% 0.1% 0 271M 59.0M 0|0 0|0 158b 44.8k 2 Aug 29 09:45:11.850 localhost:27017 3.4.2 6319 *0 *0 *0 *0 0 2|0 0.0% 0.1% 0 271M 59.0M 0|0 0|0 158b 44.9k 2 Aug 29 09:45:12.847 localhost:27017 3.4.2 6323 *0 *0 *0 *0 0 1|0 0.0% 0.1% 0 271M 59.0M 0|0 0|0 157b 44.7k 2 Aug 29 09:45:13.849 localhost:27017 3.4.2 6327 *0 *0 *0 *0 0 1|0 0.0% 0.1% 0 271M 59.0M 0|0 0|0 157b 44.7k 2 Aug 29 09:45:14.850 localhost:27017 3.4.2 6331mongotop
racks and reports the current read and write activity of a MongoDB instance, and reports these statistics on a per collection basis.
Http Consolemongo: image: mongo:latest volumes: - /Users/zhidaliao/bak_dir/mongo:/data/db ports: - "27017:27017" - "1000:1000" - "28017:28017" command: ["mongod", "--rest"]
--httpinterface 還可以增加參數進行接口操作
在瀏覽器中訪問 http://localhost:28017/ 可以查看實時信息,不推薦在生產環境中使用,安全性和穩定性。
命令行方式獲取 db.currentOp()顯示當前的用戶操作。
db.serverStatus()詳細的Mongo服務器信息。主要關注的點
connections 當前連接和可用連接數,聽過一個同行介紹過,mongodb最大處理到2000個連接就不行了(要根據你的機器性能和業務來設定),所以設大了沒意義。設個合理值的話,到達這個值mongodb就拒絕新的連接請求,避免被太多的連接拖垮。
indexCounters:btree:misses 索引的不命中數,和hits的比例高就要考慮索引是否正確建立。你看我的”missRatio” : 3.543930204420982e-7,很健康吧。所以miss率在mongostat里面也可以看
db.stats()> db.stats() { "db" : "yea", "collections" : 31, "views" : 0, "objects" : 45827, "avgObjSize" : 345.1348768193423, "dataSize" : 15816496, "storageSize" : 7372800, "numExtents" : 0, "indexes" : 54, "indexSize" : 2985984, "ok" : 1 }db.collection.stats()
mongo數據庫的集合詳細信息。
replSetGetStatusThe replSetGetStatus command (rs.status() from the shell) returns an overview of your replica set’s status.
profiler類似于MySQL的slow log, MongoDB可以監控所有慢的以及不慢的查詢。
Profiler默認是關閉的,你可以選擇全部開啟,或者有慢查詢的時候開啟。
> use test switched to db test > db.setProfilingLevel(2); {"was" : 0 , "slowms" : 100, "ok" : 1} > db.getProfilingLevel() 2
slowms:代表輸出 操作大于100毫秒的語句
To change the threshold, pass two parameters to the db.setProfilingLevel() helper in the mongo shell. The first parameter sets the profiling level for the current database, and the second sets the default slow operation threshold for the entire mongod instance.
設置0代表當前的數據庫禁止profiler功能,但是這個Mongo實例下的數據庫,只要等級是1的,都會使用20ms的配置
db.setProfilingLevel(0,20)
查看Profile日志
> db.system.profile.find().sort({$natural:-1}) {"ts" : "Thu Jan 29 2009 15:19:32 GMT-0500 (EST)" , "info" : "query test.$cmd ntoreturn:1 reslen:66 nscanned:0 query: { profile: 2 } nreturned:1 bytes:50" , "millis" : 0} ...
3個字段的意義
ts:時間戳
info:具體的操作
millis:操作所花時間,毫秒
Database Profiler?
第三方監測工具MongoDB Monitoring Service : MongoDB Monitoring Service(MMS)是Mongodb廠商提供的監控服務,可以在網頁和Android客戶端上監控你的MongoDB狀況。
以及其他第三方鏈接:
Third Party Tools
場景:現實blog的首頁-我們希望現實最近發布的10條posts。ts為時間字段。
db.posts.find().sort({ts:-1});創建索引
db.posts.ensureIndex({ts:1});
數據庫就可以基于索引信息排序,不會直接查看每個document
限定結果MongoDB游標返回一組document,我們叫這個為chunks.
articles = db.posts.find().sort({ts:-1}).limit(10);查詢特定的字段
db.oauth_clients.find({},{clientId:1,clientSecret:1})
如果你選擇了要查詢的字段,那么返回的就是部分對象。這個對象并不能直接進行更新
讀寫分離如果讀寫都在主節點的話,從節點就一直處在空置狀態,這是一種浪費。對于報表或者搜索這種讀操作來說完全可以在從節點實現,因此要做的是在 connection string 中設置成 secondarypreferred。
Capped CollectionsCapped Collections?
Capped collections 是固定大小的集合,基于插入的順序,支持高速的讀寫文檔的操作, Capped collections 工作機制于循環緩存類似: 一旦一個集合填充了他被分配的空間,對于新插入的文檔他會覆蓋舊文檔的空間
表現形式Capped collections 保證插入順序.所以查詢的時候不需要根據索引返回結果,而是通過插入順序返回,沒有索引, capped collections 可以支持高速插入操作,類似于數據庫。
為了給新文檔創建空間, capped collections 在集合中自動刪除老舊的文檔,不需要執行額外的腳本和操作。
舉個例子,oplog.rs collection 在副本形式下保存的操作日志就是使用capped collection。以下的潛在的場景:
在高速系統中儲存日志,或者在隊列形式的事件機制中保持順序。
capped collections緩存少量的數據,因為緩存的讀操作別寫操作更頻繁,你可以確保你的集合總是保存在你的工作區(比如內存),或者你能接受一些需要保持順序的寫操作。
Capped collections have an _id field and an index on the _id field by default.
優缺點如果你準備更新文檔,創建了索引所以這些更新操作不需要 經過集合掃描。
Changed in version 3.2.
文檔大小是固定的,如果 更新和替換操作,改變了文檔大小,操作將會失敗
你不能刪除文檔,只能使用drop()方法刪掉集合并重新創建新的內容。
capped collection 不支持分片操作
使用自然的排序去獲取最近插入集合的元素是更有效率的,類似誒于在日志文件中tail 的命令
$out 管道操作對capped collection無效
程序操作db.createCollection()命令創建,但是要指定參數預分配空間,單位字節。
db.createCollection( "log", { capped: true, size: 100000 } )
如果空間小于或者等于4096, 集合將會劃分出 4096 bytes. MongoDB規劃的大小以256的倍數字節擴增
另外,我們可以指定max參數,用來限制最大的文檔數量(不是集合的空間大小,而是數量)
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )Query a Capped Collection 查詢
當你使用find函數沒有指定排序的時候,mongodb會保證按照插入順序返回。如果要按照插入順序的逆序返回結果,find()函數 要緊接著 sort()函數,使用natural參數指定為-1
db.cappedCollection.find().sort( { $natural: -1 } )
檢查是否是Capped 集合
db.collection.isCapped()
Convert a Collection to Capped ,普通集合轉換成 Capped類型
db.runCommand({"convertToCapped": "mycoll", size: 100000});
This command obtains a global write lock and will block other operations until it has completed.
TTL
Setting TTL. These indexes allow you to expire and remove data from normal collections using a special type, based on the value of a date-typed field and a TTL value for the index.
TTL Collections are not compatible with capped collections.
Tailable Cursor
You can use a tailable cursor with capped collections. Similar to the Unix tail -f command, the tailable cursor “tails” the end of a capped collection. As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.
IMPORTANT
size參數總是必須的,即使你指定了max數量,mongoDb會移除更舊的文檔,如果你的集合到達了指定的最大空間限制,在最大的條數限制條數之前。
MongoDB 優化
Monitoring for MongoDB?
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/19119.html
摘要:注這是我們應用性能分析系列的第一篇,閱讀第二篇可深入了解,第三篇則關注于性能調優實踐。性能分析的行為也會影響應用性能。主動被動性能分析主動分析器在開發過程中使用,由開發人員啟用。它對性能的影響最小,同時收集足夠的信息用于診斷性能問題。 注:這是我們 PHP 應用性能分析系列的第一篇,閱讀第二篇可深入了解 xhgui,第三篇則關注于性能調優實踐。 什么是性能分析? 性能分析是衡量應用程...
摘要:的一鍵安裝開發環境非侵入式監控平臺優化系統性能定位的神器之前在用做本地開發環境,因為沒有這些對程序性能追蹤及分析的工具,所以索性基于的編排了一套自己使用。 DNMP PLUS dnmp = Docker + Nginx + MySQL + PHP + Redis + MongDB plus = xhgui + xhprof + tideways dnmp-plus = PHPer 的一...
閱讀 3323·2021-11-25 09:43
閱讀 3008·2021-10-15 09:43
閱讀 1965·2021-09-08 09:36
閱讀 2918·2019-08-30 15:56
閱讀 742·2019-08-30 15:54
閱讀 2684·2019-08-30 15:54
閱讀 2973·2019-08-30 11:26
閱讀 1237·2019-08-29 17:27