`
wenxin2009
  • 浏览: 319966 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

MongoDB学习笔记

    博客分类:
  • java
 
阅读更多

《MongoDB管理与开发精要》这一书实战性极强

以下在Mac系统中运行

1、启动mongoDB命令:/software/mongodb/bin/mongod --dbpath=/software/mongodb/data/db --logpath=/software/mongodb/logs/mongodb.log

2、停止mongoDB可以直接按ctrl+c,可也以通过/software/mongodb/bin/mongo --port 27017 然后输入use admin 然后再执行db.shutdownServer()

/software/mongodb/bin/mongo --port 27017   客户端连接到数据库

3、数据增删查发操作

//连接test数据库

bteki-MacBook-Pro:mongodb b$ bin/mongo
MongoDB shell version: 2.2.2
connecting to: test
> j={name:"mongo"};
{ "name" : "mongo" }
> t={x:3};
{ "x" : 3 }
> db.things.save(j);
> db.things.save(t);
> db.things.find();
{ "_id" : ObjectId("513e9023e1cf5189d96d383b"), "name" : "mongo" }
{ "_id" : ObjectId("513e9032e1cf5189d96d383c"), "x" : 3 }
> for (var i=1;i<10;i++) db.things.save({x:4,j:i});
> db.things.find();
{ "_id" : ObjectId("513e9023e1cf5189d96d383b"), "name" : "mongo" }
{ "_id" : ObjectId("513e9032e1cf5189d96d383c"), "x" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3841"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3842"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3843"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3844"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3845"), "x" : 4, "j" : 9 }
> db.c1.find();
> var cursor=db.things.find();
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("513e9023e1cf5189d96d383b"), "name" : "mongo" }
{ "_id" : ObjectId("513e9032e1cf5189d96d383c"), "x" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3841"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3842"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3843"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3844"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3845"), "x" : 4, "j" : 9 }
> printjson(cursor[4]);
undefined
> var cursor=db.things.find();
> printjson(cursor[4]);
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
> var arr = db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
> db.things.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3841"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3842"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3843"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3844"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3845"), "x" : 4, "j" : 9 }
> db.things.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3841"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3842"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3843"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3844"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3845"), "x" : 4, "j" : 9 }
> db.things.find(x:4).forEach(pringjson);
Tue Mar 12 10:26:13 SyntaxError: missing ) after argument list (shell):1
> db.things.find({x:4}).forEach(pringjson);
Tue Mar 12 10:26:26 ReferenceError: pringjson is not defined (shell):1
> db.things.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3841"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3842"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3843"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3844"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3845"), "x" : 4, "j" : 9 }
> printjosn(db.things.findOne({name:"mongo"}));
Tue Mar 12 10:27:10 ReferenceError: printjosn is not defined (shell):1
> printjson(db.things.findOne({name:"mongo"}));
{ "_id" : ObjectId("513e9023e1cf5189d96d383b"), "name" : "mongo" }
> db.things.find().limit(3);
{ "_id" : ObjectId("513e9023e1cf5189d96d383b"), "name" : "mongo" }
{ "_id" : ObjectId("513e9032e1cf5189d96d383c"), "x" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
> history
Tue Mar 12 10:29:33 ReferenceError: history is not defined (shell):1
> db.things.update({name:"mongo"},{$set:{name:"mongo_new"}});
> db.things.find();
{ "_id" : ObjectId("513e9032e1cf5189d96d383c"), "x" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3841"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3842"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3843"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3844"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3845"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("513e9023e1cf5189d96d383b"), "name" : "mongo_new" }
> db.things.remove({name:"mongo_new"});
> db.things.find();
{ "_id" : ObjectId("513e9032e1cf5189d96d383c"), "x" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383d"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383e"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d383f"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3840"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3841"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3842"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3843"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3844"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("513e90d7e1cf5189d96d3845"), "x" : 4, "j" : 9 }

 

->use Admin         (切换到创建用户)

->db.TestDb          (创建数据库) 

->db.addUser(“userName”,”Pwd”)    创建用户

->db.auth(“userName”,”Pwd”)       设置用户为允许连接的用户

->db.createCollection(“TableName”)                                     创建表

->show collections                          查看表是否创建成功

->db.TableName.Save({age:1})                 添加数据

->db.TableName.find()                        查看添加的数据是否成功

//创建表时可设表大小

> db.createCollection("mycoll",{capped:true,size:100000});
{ "ok" : 1 }
> db.mycoll.isCapped();
true
> db.students.isCapped();
false

//把表转换成上限形式
> db.runCommand({"convertToCapped":"students",size:100000});
{ "ok" : 1 }
> db.students.isCapped();
true

 

4、存储过程

//名称为addNumbers存储过程

> db.system.js.save({_id:"addNumbers",value:function(x,y){return x+y;}});
> db.eval('addNumbers(3,4.2)');
7.2

 

5、索引

//创建索引

> db.students.ensureIndex({age:1});

//查询索引
> db.students.getIndexes()

//删除索引

> db.students.dropIndex({age:1})

//重建索引(在collection大量删除的时候,磁盘空间并不会自动回收,长期这样会产生大量的磁盘碎片,影响磁盘读取性能,所以数据索引需要周期性地重建)

> db.students.reIndex()

//对数据库中所有需要回收空间的表重建索引

> db.repairDatabase()

//explain查看执行计划

> db.students.find({age:{$lt:30}}).explain()

 

6、优化器Profiler(0:不开启,1:记录慢命令,2:记录所有命令)

//获取当前Profiler级别

> db.getProfilingLevel()
0

//设置Profiler级别
> db.setProfilingLevel(1,20)
{ "was" : 0, "slowms" : 100, "ok" : 1 }

//查找执行时间大于5ms的日志
> db.system.profile.find({millis:{$gt:5}})

//使用hint强制要求查询操作使用某个索引

> db.students.find({name:"Tom",age:23}).hint({age:1})

 

7、性能

//检查服务器运行状态

> db.serverStatus()

//查看特定数据库的详细运行状态,更细粒度地分析故障

> db.stats()

http://127.0.0.1:28017/      ---------可通过Web页面来查看MongoDB运行状态

 

8、架构

1>主从集群分布布署,复制集

2>读写分离:机制是将那些使cpu及内存消耗严重的操作分离到一台或几台性能很高的机器上,而频繁读取的操作放到几台配置较低的机器上;然后,能过数据同步机制,实现多个数据库之间快速高效地同步数据,从而实现将“读写请求”按实际负载情况进行均衡分布的效果。

3>高性能集群,支持故障转移。

 

Replica Sets复制集,集群命令步骤:

 

bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/data/r0
bteki-MacBook-Pro:mongodbSets b$ ls
GNU-AGPL-3.0		THIRD-PARTY-NOTICES	data
README			bin
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/data/r1
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/data/r2
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/log
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/key
bteki-MacBook-Pro:mongodbSets b$ echo "this is rs1 super secret key" > data/key/r0
bteki-MacBook-Pro:mongodbSets b$ echo "this is rs1 super secret key" > data/key/r1
bteki-MacBook-Pro:mongodbSets b$ echo "this is rs1 super secret key" > data/key/r2
bteki-MacBook-Pro:mongodbSets b$ chmod 600 data/key/r*
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongod --replSet rs1 --keyFile /software/mongodbSets/data/key/r0 --fork --port 28010 --dbpath /software/mongodbSets/data/data/r0 --logpath=/software/mongodbSets/data/log/r0.log --logappend
forked process: 8138
all output going to: /software/mongodbSets/data/log/r0.log
child process started successfully, parent exiting
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongod --replSet rs1 --keyFile /software/mongodbSets/data/key/r1 --fork --port 28011 --dbpath /software/mongodbSets/data/data/r1 --logpath=/software/mongodbSets/data/log/r1.log --logappend
forked process: 8147
all output going to: /software/mongodbSets/data/log/r1.log
child process started successfully, parent exiting
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongod --replSet rs1 --keyFile /software/mongodbSets/data/key/r2 --fork --port 28012 --dbpath /software/mongodbSets/data/data/r2 --logpath=/software/mongodbSets/data/log/r2.log --logappend
forked process: 8150
all output going to: /software/mongodbSets/data/log/r2.log
child process started successfully, parent exiting
bteki-MacBook-Pro:mongodbSets b$ bin/mongo -port 28010
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:28010/test
> config_rs1={_id:'rs1',members:[
... {_id:0,host:'localhost:28010'},
... {_id:1,host:'localhost:28011'},
... {_id:2,host:'localhost:28012'}]}
{
	"_id" : "rs1",
	"members" : [
		{
			"_id" : 0,
			"host" : "localhost:28010"
		},
		{
			"_id" : 1,
			"host" : "localhost:28011"
		},
		{
			"_id" : 2,
			"host" : "localhost:28012"
		}
	]
}
> rs.initiate(config_rs1);
{
	"info" : "Config now saved locally.  Should come online in about a minute.",
	"ok" : 1
}
> rs.status()
{
	"set" : "rs1",
	"date" : ISODate("2013-03-13T05:54:33Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "localhost:28010",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 497,
			"optime" : Timestamp(1363154023000, 1),
			"optimeDate" : ISODate("2013-03-13T05:53:43Z"),
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "localhost:28011",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 47,
			"optime" : Timestamp(1363154023000, 1),
			"optimeDate" : ISODate("2013-03-13T05:53:43Z"),
			"lastHeartbeat" : ISODate("2013-03-13T05:54:31Z"),
			"pingMs" : 509
		},
		{
			"_id" : 2,
			"name" : "localhost:28012",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 47,
			"optime" : Timestamp(1363154023000, 1),
			"optimeDate" : ISODate("2013-03-13T05:53:43Z"),
			"lastHeartbeat" : ISODate("2013-03-13T05:54:31Z"),
			"pingMs" : 504
		}
	],
	"ok" : 1
}
rs1:PRIMARY>
//-------------主从操作日志
rs1:PRIMARY> use local
switched to db local
rs1:PRIMARY> show collections
oplog.rs
system.replset
rs1:PRIMARY> db.oplog.rs.find()
{ "ts" : Timestamp(1363154023000, 1), "h" : NumberLong(0), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } }
rs1:PRIMARY> db.printReplicationInfo()
configured oplog size:   183.10546875MB
log length start to end: 0secs (0hrs)
oplog first event time:  Wed Mar 13 2013 13:53:43 GMT+0800 (CST)
oplog last event time:   Wed Mar 13 2013 13:53:43 GMT+0800 (CST)
now:                     Wed Mar 13 2013 14:01:30 GMT+0800 (CST)
rs1:PRIMARY> db.printSlaveReplicationInfo()
source:   localhost:28011
	 syncedTo: Wed Mar 13 2013 13:53:43 GMT+0800 (CST)
		 = 535 secs ago (0.15hrs)
source:   localhost:28012
	 syncedTo: Wed Mar 13 2013 13:53:43 GMT+0800 (CST)
		 = 535 secs ago (0.15hrs)
//----------------主从配置信息
rs1:PRIMARY> db.system.replset.find()
{ "_id" : "rs1", "version" : 1, "members" : [ 	{ 	"_id" : 0, 	"host" : "localhost:28010" }, 	{ 	"_id" : 1, 	"host" : "localhost:28011" }, 	{ 	"_id" : 2, 	"host" : "localhost:28012" } ] }

//----------------管理Replica Sets---主从切换
//28011端口服务
bteki-MacBook-Pro:mongodbSets b$ bin/mongo --port 28011
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:28011/test
rs1:SECONDARY> rs.freeze(30)
{ "ok" : 1 }
rs1:SECONDARY> rs.status()
{
	"set" : "rs1",
	"date" : ISODate("2013-03-13T06:08:09Z"),
	"myState" : 2,
	"syncingTo" : "localhost:28012",
	"members" : [
		{
			"_id" : 0,
			"name" : "localhost:28010",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 854,
			"optime" : Timestamp(1363154023000, 1),
			"optimeDate" : ISODate("2013-03-13T05:53:43Z"),
			"lastHeartbeat" : ISODate("2013-03-13T06:08:08Z"),
			"pingMs" : 0,
			"errmsg" : "syncing to: localhost:28012"
		},
		{
			"_id" : 1,
			"name" : "localhost:28011",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1198,
			"optime" : Timestamp(1363154023000, 1),
			"optimeDate" : ISODate("2013-03-13T05:53:43Z"),
			"errmsg" : "syncing to: localhost:28012",
			"self" : true
		},
		{
			"_id" : 2,
			"name" : "localhost:28012",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 838,
			"optime" : Timestamp(1363154023000, 1),
			"optimeDate" : ISODate("2013-03-13T05:53:43Z"),
			"lastHeartbeat" : ISODate("2013-03-13T06:08:08Z"),
			"pingMs" : 0
		}
	],
	"ok" : 1
}
rs1:SECONDARY> 

//28010端口服务
rs1:PRIMARY> rs.stepDown(30)
Wed Mar 13 14:07:46 DBClientCursor::init call() failed
Wed Mar 13 14:07:46 query failed : admin.$cmd { replSetStepDown: 30.0 } to: 127.0.0.1:28010
Wed Mar 13 14:07:46 Error: error doing query: failed src/mongo/shell/collection.js:155
Wed Mar 13 14:07:46 trying reconnect to 127.0.0.1:28010
Wed Mar 13 14:07:46 reconnect 127.0.0.1:28010 ok

//---------------读写分离
//主
bteki-MacBook-Pro:mongodbSets b$ bin/mongo -port 28012
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:28012/test
rs1:PRIMARY> db.createCollection("Users")
{ "ok" : 1 }
rs1:PRIMARY> db.Users.insert({"name":"test1",age:20})
rs1:PRIMARY> show collections
Users
system.indexes
rs1:PRIMARY> db.Users.find()
{ "_id" : ObjectId("51401a2b5b02c26e64cba918"), "name" : "test1", "age" : 20 }
rs1:PRIMARY> 
//从
bteki-MacBook-Pro:mongodbSets b$ bin/mongo -port 28010
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:28010/test
rs1:SECONDARY> show collections
Wed Mar 13 14:22:45 uncaught exception: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
rs1:SECONDARY> db.getMongo().setSlaveOk()
rs1:SECONDARY> show collections
Users
system.indexes
rs1:SECONDARY> db.Users.find();
{ "_id" : ObjectId("51401a2b5b02c26e64cba918"), "name" : "test1", "age" : 20 }

//-----------增减节点
//先添加一个28013节点,然后再执行下面语句添加到集群中
rs1:PRIMARY> rs.add("localhost:28013")
{ "ok" : 1 }
//减少、删除节点
rs1:PRIMARY> rs.remove("localhost:28013")
//判断是否是主节点(只有主节点才能插入数据操作)
rs1:PRIMARY>db.isMaster()

 4>分片Sharding
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/shard/s0
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/shard/s1
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/shard/log
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongod --shardsvr --port 20000 --dbpath /software/mongodbSets/data/shard/s0 --fork --logpath /software/mongodbSets/data/shard/log/s0.log --directoryperdb
forked process: 8291
all output going to: /software/mongodbSets/data/shard/log/s0.log
child process started successfully, parent exiting
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongod --shardsvr --port 20001 --dbpath /software/mongodbSets/data/shard/s1 --fork --logpath /software/mongodbSets/data/shard/log/s1.log --directoryperdb
forked process: 8296
all output going to: /software/mongodbSets/data/shard/log/s1.log
child process started successfully, parent exiting
bteki-MacBook-Pro:mongodbSets b$ mkdir -p data/shard/config
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongod --configsvr --port 30000 --dbpath /s
sbin/     software/ 
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongod --configsvr --port 30000 --dbpath /software/mongodbSets/data/shard/config --fork --logpath /software/mongodbSets/data/shard/log/config.log --directoryperdb
forked process: 8300
all output going to: /software/mongodbSets/data/shard/log/config.log
child process started successfully, parent exiting
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongos --port 40000 --configdb localhost:30000 --fork --logpath /software/mongodbSets/data/shard/log/route.log --chunkSize 1
forked process: 8303
all output going to: /software/mongodbSets/data/shard/log/route.log
child process started successfully, parent exiting
bteki-MacBook-Pro:mongodbSets b$ /software/mongodbSets/bin/mongo admin --port 40000
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:40000/admin
mongos> db.runCommand({addshard:"localhost:20000"})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"localhost:20001"})
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand({enablesharding:"test"})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"test.users",key:{_id:1}})
{ "collectionsharded" : "test.users", "ok" : 1 }
mongos> use test
switched to db test
mongos> for(var i=1;i<=500000;i++) db.users.insert({age:i,name:"wangwenlong",addr:"Beijing",country:"China"})
mongos> db.users.stats()
{
	"sharded" : true,
	"ns" : "test.users",
	"count" : 500000,
	"numExtents" : 16,
	"size" : 48000000,
	"storageSize" : 80949248,
	"totalIndexSize" : 17210480,
	"indexSizes" : {
		"_id_" : 17210480
	},
	"avgObjSize" : 96,
	"nindexes" : 1,
	"nchunks" : 39,
	"shards" : {
		"shard0000" : {
			"ns" : "test.users",
			"count" : 191220,
			"size" : 18357120,
			"avgObjSize" : 96,
			"storageSize" : 22507520,
			"numExtents" : 7,
			"nindexes" : 1,
			"lastExtentSize" : 11325440,
			"paddingFactor" : 1,
			"systemFlags" : 1,
			"userFlags" : 0,
			"totalIndexSize" : 7096768,
			"indexSizes" : {
				"_id_" : 7096768
			},
			"ok" : 1
		},
		"shard0001" : {
			"ns" : "test.users",
			"count" : 308780,
			"size" : 29642880,
			"avgObjSize" : 96,
			"storageSize" : 58441728,
			"numExtents" : 9,
			"nindexes" : 1,
			"lastExtentSize" : 20643840,
			"paddingFactor" : 1,
			"systemFlags" : 1,
			"userFlags" : 0,
			"totalIndexSize" : 10113712,
			"indexSizes" : {
				"_id_" : 10113712
			},
			"ok" : 1
		}
	},
	"ok" : 1
}
 5>Replica Sets+Sharding架构方案

 

 

 

================================================================================

java操作MongoDB:

java驱动包下载:https://github.com/mongodb/mongo-java-driver/downloads

代码如下:

package com.dwen.mongodb;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

public class HelloWorld {

    /**
     * mongoDB增删查改
     * @author Dwen
     * @param args
     */
    public static void main(String[] args) {
        Mongo mongo  = null;
        try {
            mongo=new Mongo("127.0.0.1", 27017);
        } catch (UnknownHostException e) {
           e.printStackTrace();
        }
        DB db=mongo.getDB("zhao");
        db.authenticate("zhao", "zhao".toCharArray());
//        insert(db);
//        select(db);
//        update(db);
//        getAllData(db);
//        gridFS(db);
//        readGridFSImage(db);
        removeGridFSImage(db);
    }
    
    /**
     * 插入数据
     * @param db
     */
    private static void insert(DB db){
      //声明一个collection对象
        DBCollection collection = db.getCollection("students");
        BasicDBObject doc = new BasicDBObject();
        doc.put("name", "MongoDB");
        doc.put("age", 19);
        collection.insert(doc);
    }
    
    /**
     * 查询
     * @param db
     */
    private static void select(DB db){
        DBCollection collection = db.getCollection("students");
        DBObject myDoc = collection.findOne();
        System.out.println(myDoc);
    }
    
    /**
     * 获取所有数据
     * @param db
     */
    private static void getAllData(DB db){
        DBCollection collection=db.getCollection("students");
        //用游标获取所有数据
        DBCursor cur = collection.find();
        while (cur.hasNext()) {
            System.out.println(cur.next());
        }
    }
    
    /**
     * 更新数据
     * @param db
     */
    private static void update(DB db){
        DBCollection collection = db.getCollection("students");
        //定义一个查询对象,相当于sql中的where语句
        DBObject updateCondition = new BasicDBObject();
        updateCondition.put("name", "MongoDB");
        //定义一个更新换对象,相当于sql中的set语句
        DBObject updateValue = new BasicDBObject();
        updateValue.put("age", 80);
        DBObject updateSetValue = new BasicDBObject("$set", updateValue);
      //将查询对象和更新对象作为参数传给update来完成更新
        collection.update(updateCondition, updateSetValue);
    }
    
    /**
     * 用GridFS类将本地图片保存到mongodb中
     * @param db
     */
    private static void gridFS(DB db){
        String fileName = "water_pic";
        File imageFile = new File("/software/test.png");
        GridFS gFs=new GridFS(db,"photo");
        GridFSInputFile gFsInputFile = null;
        //将本地文件存储到MongoDB中
        try {
            gFsInputFile=gFs.createFile(imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        gFsInputFile.setFilename(fileName);
        gFsInputFile.save();
    }
    
    /**
     * 读取图片
     * @param db
     */
    private static void readGridFSImage(DB db){
        String fileName = "water_pic";
        GridFS gfsphoto = new GridFS(db,"photo");
        //读取GridFS数据,并保存到本地
        GridFSDBFile imageForOutput = gfsphoto.findOne(fileName);
        try {
            imageForOutput.writeTo("/software/my.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 删除图片
     * @param db
     */
    private static void removeGridFSImage(DB db){
        String fileName = "water_pic";
        GridFS gfsphoto = new GridFS(db,"photo");
        //删除指定GridFS数据
        gfsphoto.remove(gfsphoto.findOne(fileName));
    }
    

}

 

 GridFS存储图片。驱动首先会在当前数据库创建两个集合:"fs.files"和"fs.chunks"集合,前者记录了文件名,文件创建时间,文件类型等基本信息;后者分块存储了文件的二进制数据(并支持加密这些二进制数据)。分块的意思是把文件按照指定大小分割,然后存入多个文档中。

 

mongodb数据导出:

[root@localhost bin]# ./mongoexport -d my_mongodb -c user -o user.dat

参数说明
-d  指明使用的库,  本例中为” my_mongodb”
-c  指明要导出的表,  本例中为”user”
-o  指明要导出的文件名,  本例中为”user.dat”
mongodb数据导入
[root@localhost bin]# ./mongoimport -d my_mongodb -c user user.dat
远程数据导入:
bteki-MacBook-Pro:mongodb b$ bin/mongoimport -h 192.168.6.188:10000 -d zhao-market -c weixin_sender --file weixin_sender.dat

选项解释:

-d    指定把数据导入到哪一个数据库中

-c    指定把数据导入到哪一个集合中

--type    指定导入的数据类型

--file       指定从哪一个文件中导入数据

--headerline    仅适用于导入csv,tsv格式的数据,表示文件中的第一行作为数据头

--upsert  以新增或者更新的方式来导入数据

数据备份:

bteki-MacBook-Pro:mongodb b$ bin/mongodump -d my_mongodb

 

 

分享到:
评论

相关推荐

    mongodb学习笔记资料

    mongodb学习笔记资料,从安装到操作库collection 对document的crud 索引 replicaSet sharding 备份与恢复

    Mongodb学习笔记.docx

    MongoDB 学习笔记 本文档是 MongoDB 学习笔记,涵盖了 NoSQL、Cache、运行平台等多个方面的知识点。 一、NoSQL 数据库 MongoDB MongoDB 是一个强大、灵活、可扩展性好的文档数据存储器,可以完成大部分关系数据库...

    mongodb学习笔记

    mongodb学习笔记 MongoDB 是一个基于分布式文件存储的开源数据库系统,主要用于处理大量数据的存储和查询。以下是 MongoDB 的一些基本操作和技巧: 插入文档 MongoDB 中可以使用 `insert` 方法来插入文档,例如 ...

    mongoDB学习笔记及工具.zip

    本压缩包“mongoDB学习笔记及工具.zip”包含了一些资源,帮助你深入理解和掌握MongoDB的相关知识。 1. **笔记(note.txt)**: 这个文件可能是对MongoDB的基础概念、安装过程、基本操作和进阶特性的详细记录。笔记...

    MongoDB学习笔记思维导图.pdf

    本篇学习笔记将从基础操作到高级功能,详细阐述MongoDB的关键知识点。 首先,MongoDB的基本单元是集合(collection),类似于关系型数据库中的表,而集合中的元素称为文档(document),文档是键值对的集合,类似于...

    mongodb学习笔记和mongodb权威指南

    首先,`mongodb_and_python`学习笔记可能涵盖了如何使用Python编程语言与MongoDB进行交互。Python是常见的MongoDB驱动程序之一,通过PyMongo库,开发者可以方便地执行CRUD(创建、读取、更新、删除)操作,构建复杂...

    MongoDB学习笔记思维导图

    总结的MongoDB的学习笔记,基本上包括了MongoDB的方方面面,每个知识点,都有示例代码,有需要的朋友,可以下载下来进一步补充完善。

    MongoDB学习笔记1

    MongoDB 是一种流行的开源、非关系型数据库系统,以其灵活性、高性能和易用性而备受开发者青睐。在本文中,我们将深入探讨 ...在实际项目中,学习和掌握 MongoDB 及其驱动程序能够帮助你更好地应对现代数据存储需求。

    MongoDB学习笔记(六) MongoDB索引用法和效率分析

    MongoDB是一种流行的NoSQL数据库,它的索引机制与传统的关系型数据库类似,旨在提升查询和排序的速度。在MongoDB中,索引对于优化查询性能至关重要,尤其在处理大量数据时。下面将详细介绍MongoDB中不同类型的索引...

    MongoDB学习笔记之GridFS使用介绍

    MongoDB的GridFS是一个强大的文件存储系统,专为在数据库中存储和检索大量小文件而设计。GridFS将文件拆分为多个数据块(chunk),每个块都有自己的文档存储在`fs.chunks`集合中,而文件元信息则保存在`fs.files`集合...

    mongoDB学习笔记

    本文介绍了 MongoDB 的基础使用、技术架构、应用场景、主要特点以及安装步骤。内容涵盖了 MongoDB 的基本操作和配置,深入探讨了其内部技术架构,讨论了适合的应用场景和实际案例,并详细说明了安装过程以帮助读者...

    MongoDB_学习笔记

    ### MongoDB 学习笔记知识点详解 #### 一、MongoDB 命令行基础操作 **1.1 帮助命令** - **help**:显示 MongoDB shell 的基本帮助信息。 - **db.help()**:提供当前数据库的帮助信息。 - **db.mycoll.help()**:...

Global site tag (gtag.js) - Google Analytics