標籤:dba ops mongodb
MongoDB 營運常用操作
分析方法:
1. 通過top、free、iostat、iftop等工具查看Linux伺服器平均負載、CPU利用率、IO、記憶體、swap、網路流量等,先定位到壓力源頭。
2. 通過mongostat、mongotop等分析MongoDB讀寫壓力。觀察Page Faults、Connections、Queues等效能指標。
3. 日誌中預設記錄超過100ms的請求,過濾出Overflow查詢,再使用Mtools跟蹤分析MongoDB記錄檔中的慢查詢語句。若是語句問題,可從日誌定位到出現效能問題時刻的源頭語句。
常用操作:
1. 重啟服務
#service mongod restart
2. 追蹤記錄檔分析查詢
$tail -f /var/log/mongodb/mongod.log
3. 分析當前讀寫狀態
$mongostat -uxucy -p
4. 查看資料庫狀態
>db.serverStatus()
5. 查看資料庫連接數
>db.serverStatus().connections
6. 查看複製集狀態
>rs.status()
7. 手動降級執行個體
>rs.stepDown(30)
8. 查看當前執行活躍查詢
>db.currentOp();
9. 殺掉正在執行的操作
>db.killOp("shard3:466404288");
10. 分析查詢語句的執行計畫
>db.Product.find({"_id": 10086}).explain().pretty();
11. 在後台建立索引
>db.Product.ensureIndex({ "Category": 1, "Status": 1}, { background: 1 });
12. 資料匯出
$ mongoexport -u xucy -p --authenticationDatabase admin -d DBName -c Product -q ‘{"AddDate":{"$gte":new Date(1445558400000),"$lte":new Date(1445731200000)}}‘ -f "_id,AddDate,Status" -o Product_20151026.json
13. 資料匯入
$mongoimport -h 192.168.1.101 -u xucy -p -d DBName --authenticationDatabase admin -c datacontent datacontent_bak.json
14. 資料庫/表備份
$ mongodump --authenticationDatabase admin -uxucy -p --db DBName -o /disk1/mongobackup/DBName_fullbackup_20150917
15. 資料庫/表恢複
$ mongorestore --authenticationDatabase admin -uxucy -p --db DBName --drop Temp_20150909/DBName/
16. 資料庫授權
>use dbname >db.createUser( { user: "peter", pwd: "xxxxxxxx", roles: [ { role: "readWrite", db: "dbname" } ] } )
本文出自 “SQL Server Deep Dives” 部落格,請務必保留此出處http://ultrasql.blog.51cto.com/9591438/1712499
MongoDB 營運常用操作