更新时间:2022-10-04 18:03:43
分析方法:
1. 通过top、free、iostat、iftop等工具查看Linux服务器平均负载、CPU利用率、IO、内存、swap、网络流量等,先定位到压力源头。
2. 通过mongostat、mongotop等分析MongoDB读写压力。观察Page Faults、Connections、Queues等性能指标。
3. 日志中默认记录超过100ms的请求,过滤出Overflow查询,再使用Mtools跟踪分析MongoDB日志文件中的慢查询语句。若是语句问题,可从日志定位到出现性能问题时刻的源头语句。
常用操作:
1. 重启服务
1
|
#service mongod restart |
2. 跟踪日志分析查询
1
|
$ tail -f /var/log/mongodb/mongod .log
|
3. 分析当前读写状态
1
|
$mongostat -uxucy -p |
4. 查看数据库状态
1
|
>db.serverStatus() |
5. 查看数据库连接数
1
|
>db.serverStatus().connections |
6. 查看复制集状态
1
|
>rs.status() |
7. 手动降级实例
1
|
>rs.stepDown(30) |
8. 查看当前执行活跃查询
1
|
>db.currentOp(); |
9. 杀掉正在执行的操作
1
|
>db.killOp( "shard3:466404288" );
|
10. 分析查询语句的执行计划
1
|
>db.Product.find({ "_id" : 10086}).explain().pretty();
|
11. 在后台创建索引
1
|
>db.Product.ensureIndex({ "Category" : 1, "Status" : 1}, { background: 1 });
|
12. 数据导出
1
|
$ 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. 数据导入
1
|
$mongoimport -h 192.168.1.101 -u xucy -p -d DBName --authenticationDatabase admin -c datacontent datacontent_bak.json |
14. 数据库/表备份
1
|
$ mongodump --authenticationDatabase admin -uxucy -p --db DBName -o /disk1/mongobackup/DBName_fullbackup_20150917
|
15. 数据库/表恢复
1
|
$ mongorestore --authenticationDatabase admin -uxucy -p --db DBName --drop Temp_20150909 /DBName/
|
16. 数据库授权
1
2
3
4
5
6
7
8
9
10
|
>use dbname >db.createUser( {
user : "peter" ,
pwd: "xxxxxxxx" ,
roles: [
{ role: "readWrite" , db: "dbname" }
]
}
)
|