Home »  Mongodb » Mongodb技巧与管理

第四部分 Mongodb-技巧与管理

奇淫技巧

一、调试

我们Mongodb客户端使用PHP,按照常规的调试方法无非就是print_r() var_dump,如果遇到简单的应用倒是无妨,可遇到复杂的,每次刷新页面打开浏览器终端,查看结果,一次次的刷新,一秒秒过去,等待着结果是否感到很烦躁?这里教你一招

aggregate($params);

上面的$params是聚合分析输入的一坨参数,问题出在这里,那么我们

print_r(json_encode($params, true));
die();
aggregate($params);

假如你使用的是Robomongo GUI 客户端,把打印的结果转移到这里,刷新问题解决!

二、数据迁移

备份

语义解析

mongodump
	--help
	-h, --host=<hostname>
	-u, --username=<username>                       username for authentication
	-p, --password=<password>                       password for authentication
	-d, --db=<database-name>                        database to use
	-c, --collection=<collection-name>              collection to use
	-o, --out=<filename>                            output file; if not specified
	-q, --query=<json>                              query filter, as a JSON string

example

mongodump -d user -o /data/backup

恢复

mongostore
	--help
	-h, --host=<hostname>
	-u, --username=<username>                       username for authentication
	-p, --password=<password>                       password for authentication
	-d, --db=<database-name>                        database to use
	-c, --collection=<collection-name>              collection to use
	--drop							                      if collection exsit drop it
	-o, --out=<filename>                            output file; if not specified
	-q, --query=<json>                              query filter, as a JSON string

example

mongorestore -d user --drop data/backup/user/

导出cvs或者CSV

语义分析

mongoexport
	--help
	-h, --host=<hostname>
	-u, --username=<username>                       username for authentication
	-p, --password=<password>                       password for authentication
	-d, --db=<database-name>                        database to use
	-c, --collection=<collection-name>              collection to use
	-o, --out=<filename>                            output file; if not specified
	-q, --query=<json>                              query filter, as a JSON string

更多参阅 https://docs.mongodb.com/manual/reference/program/mongoexport/

example

导出db是user,collection是user的数据

mongoexport --db user --collection user --out ~/user.json

导入JSON或者CSV

语义分析

mongoexport
	--help
	-h, --host=<hostname>
	-u, --username=<username>                       username for authentication
	-p, --password=<password>                       password for authentication
	-d, --db=<database-name>                        database to use
	-c, --collection=<collection-name>              collection to use

更多参阅 https://docs.mongodb.com/manual/reference/program/mongoimport/

example

mongoimport --db zips --collection zips ~/Downloads/zips.json

三、性能优化

索引函数

索引分类(按照官网说法来)

单字段索引

db.zips.ensureIndex({pop: 1});

复合索引

db.zips.ensureIndex({city: 1, state: 1});

子文档索引

db.user.ensureIndex({familly.father: 1});

空间索引(2d索引,2dsphere几何球体索引)

db.zips.ensureIndex({loc:'2d'});

索引类型

唯一索引

db.zips.ensureIndex({state: 1}, {unique: true});

稀疏索引(动态属性索引)

db.zips.ensureIndex({state: 1}, {sparse: true});

加入有一些文档不存在state属性,那么不予显示该文档。

维护索引

使用explain

db.getCollection('zips').find({
    city: 'AGAWAM'
}).explain()

查看collection下索引

db.zips.getIndexes();

四、性能监控

serverStatus

db.runCommand({serverStatus: 1});

serverStatus命令的输出内容较为丰富。统计的信息包含错误、B树访问率、打开连接数、以及总的插入、更新、查询和删除。下面是一段节选后的命令输出:

 {
	"host" : "MtaoMacBook-Pro.local",
	"advisoryHostFQDNs" : [
		"duforddemacbook-pro.local"
	],
	"version" : "3.2.6",
	"process" : "mongod",
	"pid" : NumberLong(486),
	"uptime" : 17094,
	"uptimeMillis" : NumberLong(17094108),
	"uptimeEstimate" : 4284,
	"localTime" : ISODate("2016-05-20T05:14:40.232Z"),
	"asserts" : {
		"regular" : 0,
		"warning" : 0,
		"msg" : 0,
		"user" : 6,
		"rollovers" : 0
	},
	"connections" : {
		"current" : 14,
		"available" : 3262,
		"totalCreated" : NumberLong(43)
	},
	"extra_info" : {
		"note" : "fields vary by platform",
		"page_faults" : 3116
	},
	"globalLock" : {
		"totalTime" : NumberLong("17094093000"),
		"currentQueue" : {
			"total" : 0,
			"readers" : 0,
			"writers" : 0
		},
		"activeClients" : {
			"total" : 21,
			"readers" : 0,
			"writers" : 0
		}
	},
	"writeBacksQueued" : false,
	"mem" : {
		"bits" : 64,
		"resident" : 108, //所占物理内存数量
		"virtual" : 2586, //虚拟内存字节数
		"supported" : true,
		"mapped" : 0,
		"mappedWithJournal" : 0
	},
	....
	},
	"ok" : 1
}

globalLock部分很重要,揭示了服务器花在写锁上的总时间,currentQueue是表示具体的瓶颈表述。 mem部分显示了mongod进程是如何使用内存的

top

db.runCommand({top:1});

top命令会显示每个数据库的操作计数器。如果应用使用了多个物理数据库,想看看平均耗时,那么这是个很有用的命令。

db.currentOp()

当前正在做什么

killop 杀死某个操作,结合db.currentOp使用

外部监控工具

诊断工具

五、架构

复制

分片集群

Mongodb的核心。对于大多数应用程序而言,在一台服务器上保存完整数据集是完全可以接受的。但随着数据量的增长,以及应用程序对读写吞吐量的要求越来越高,普通服务器的方案渐渐变得捉襟见肘了。尤其是这些服务器可能无法分配足够的内存,或者没有足够的CPU核数来有效处理工作负荷。分片技术横空出世


(未完待续)