1 实验使用gridfs存放一个大文件,观察fs.chunks和fs.files的情况
2 实施分片,验证分片成功。
**********************************************************************************************************************************
1.1 加载文件到数据库
[root@localhost mongodb]# mongofiles put /home/mongodb/admin.1
connected to: 127.0.0.1[root@localhost mongodb]#
1.2 查看加载的文件:
> show collections
card
emp
fs.chunks
fs.files
system.indexes
system.users
test
testcoll
user
> db.fs.files.find()
{ "_id" : ObjectId("5328fac6b8dc05f67d4a2458"), "filename" : "/home/mongodb/admin.1", "chunkSize" : 262144, "uploadDate" : ISODate("2014-03-19T02:02:47.559Z"), "md5" : "fde9e0818281836e4fc0edfede2b8762", "length" : 134217728 }
>
1.3 为了测试,再上传一个小一些的日志文件:
[root@localhost mongodb]# mongofiles put test.log
connected to: 127.0.0.1
added file: { _id: ObjectId('5328fbea8c393b97f385e329'), filename: "test.log", chunkSize: 262144, uploadDate: new Date(1395194858685), md5: "27261643abd87a9f0eb0aa8a76ccb7d5", length: 1397 }
done!
> db.fs.files.find()
{ "_id" : ObjectId("5328fac6b8dc05f67d4a2458"), "filename" : "/home/mongodb/admin.1", "chunkSize" : 262144, "uploadDate" : ISODate("2014-03-19T02:02:47.559Z"), "md5" : "fde9e0818281836e4fc0edfede2b8762", "length" : 134217728 }
{ "_id" : ObjectId("5328fbea8c393b97f385e329"), "filename" : "test.log", "chunkSize" : 262144, "uploadDate" : ISODate("2014-03-19T02:07:38.685Z"), "md5" : "27261643abd87a9f0eb0aa8a76ccb7d5", "length" : 1397 }
1.4 按上面的文件ID,可以在fs.chunks 表中,找到相应的记录:
> db.fs.chunks.find({"files_id":ObjectId("5328fbea8c393b97f385e329")})
{ "_id" : ObjectId("5328fbea31a4804b225989ca"), "files_id" : ObjectId("5328fbea8c393b97f385e329"), "n" : 0, "data" : BinData(0,"5oC755So6YePIDE0MDkwOTYKLXJ3LS0tLS0tLS4gMSBtb25nb2QgbW9uZ29kICA2NzEwODg2NCAz5pyIICAxMiAxODo0MyBhZG1pbi4wCi1ydy0tLS0tLS0uIDE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zNDIxNzcyOCAz5pyIICAxOSAxMDowMiB0ZXN0LjEKLXJ3LS0tLS0tLS4gMSBtb25nb2QgbW9uZ29kIDI2ODQzNTQ1NiAz5pyIICAxOSAxMDowMiB0ZXN0LjIKLXJ3LXItLXItLS4gMSByb290ICAgcm9vdCAgICAgICAgICAgMCAz5pyIICAxOSAxMDowNyB0ZXN0LmxvZwotcnctLS0tLS0tLiAxIG1vbmdvZCBtb25nb2QgIDE2Nzc3MjE2IDPmnIggIDE5IDEwOjAyIHRlc3QubnMKZHJ3eHIteHIteC4gMiBtb25nb2QgbW9uZ29kICAgICAgNDA5NiAz5pyIICAxOSAxMDowMyBfdG1wCi1ydy0tLS0tLS0uIDEgbW9uZ29kIG1vbmdvZCAgNjcxMDg4NjQgMeaciCAgMTYgMTE6MzEgV09SS0lOR19DT0xMRUNUSU9OLjAKLXJ3LS0tLS0tLS4gMSBtb25nb2QgbW9uZ29kIDEzNDIxNzcyOCAx5pyIICAxNiAxMTozMSBXT1JLSU5HX0NPTExFQ1RJT04uMQotcnctLS0tLS0tLiAxIG1vbmdvZCBtb25nb2QgIDE2Nzc3MjE2IDHmnIggIDE2IDExOjMxIFdPUktJTkdfQ09MTEVDVElPTi5ucwo=") }
>
-------------------------------------------------------------------------------------
2.配置集群+分片:
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r1 -dbpath=D:\Program_file\mongo\db1 -logpath=D:\Program_file\mongo\log\mongodb1.log --logappend --journal --port 28011
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r2 -dbpath=D:\Program_file\mongo\db2 -logpath=D:\Program_file\mongo\log\mongodb2.log --logappend --journal --port 28012
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r3 -dbpath=D:\Program_file\mongo\db3 -logpath=D:\Program_file\mongo\log\mongodb3.log --logappend --journal --port 28013
--文件拷贝后添加--fastsync 参数启动(拷贝的如果不是primary,启动后拷贝前的节点会变为primary)
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r4 -dbpath=D:\Program_file\mongo\db4 -logpath=D:\Program_file\mongo\log\mongodb4.log --logappend --journal --port 28014 --fastsync
#share:
D:\Program_file\mongo\mongodb\bin\mongod.exe --shardsvr --port 20000 -dbpath=D:\Program_file\mongo\shard\shard0\db -logpath=D:\Program_file\mongo\shard\shard0\log\mongo.log --logappend --journal --directoryperdb
D:\Program_file\mongo\mongodb\bin\mongod.exe --shardsvr --port 20001 -dbpath=D:\Program_file\mongo\shard\shard1\db -logpath=D:\Program_file\mongo\shard\shard1\log\mongo.log --logappend --journal --directoryperdb
#config
D:\Program_file\mongo\mongodb\bin\mongod.exe --configsvr --port 30000 -dbpath=D:\Program_file\mongo\shard\config -logpath=D:\Program_file\mongo\shard\config\mongo.log --logappend --journal --directoryperdb
#route
D:\Program_file\mongo\mongodb\bin\mongos.exe --port 40000 --configdb localhost:30000 --logpath=D:\Program_file\mongo\shard\config\route.log --logappend --chunkSize 1
2.2 测试连接:
D:\Program_file\mongo\mongodb\bin>mongo -port 40000
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:40000/test
mongos> show collections
system.indexes
user
users
mongos>