【2】单机安装MongoDB集群
单机安装MongoDB集群
英文链接: http://www.mongodb.org/display/DOCS/A+Sample+Configuration+Session 如果安装mongodb,请参考http://chakey.iteye.com/blog/802336
在单个服务器上,安装2个shards(shard:通常的datanode,这里也可以是一个目录),1个config db 和 一个mongos处理器。
步骤一:创建Shard 首先要启动两个mongod服务 $ mkdir /data/db/a /data/db/b $ ./mongod --shardsvr --dbpath /data/db/a --port 27020 > /tmp/sharda.log & $ cat /tmp/sharda.log $ ./mongod --shardsvr --dbpath /data/db/b --port 27021 > /tmp/shardb.log & $ cat /tmp/shardb.log
其次要启动配置服务器(configuration server)和mongos $ mkdir /data/db/config $ ./mongod --configsvr --dbpath /data/db/config --port 27022 > /tmp/configdb.log & $ cat /tmp/configdb.log $ ./mongos --configdb localhost:27022 > /tmp/mongos.log & $ cat /tmp/mongos.log
mongos不需要一个数据存放的文件夹,但是需要将config server的port传递给它。 注意了: 在一个真实的应用场景里,mongod 、mongos和configs应该在不同的服务器上。这里面的“localhost”,最好用 ip地址或者hostname代替。这里只是单机举个例子。 mongos 通过追加 --chunkSize (单位是M 例如 --chunkSize 1 代表每个chunk大小为1M) $ ./mongos --configdb localhost:27022 --chunkSize 1 > /tmp/mongos.log &
步骤二:建立集群喽 启动shell,需要连接到mongos上(这个默认的连接是 localhost:27017)
现在我们把两个shard加入到集群中: $ ./mongo --port 27017 MongoDB shell version: 1.6.3 connecting to: test
use admin switched to db admin db.runCommand( { addshard : "localhost:10000" } ) { "shardadded" : "shard0000", "ok" : 1 } db.runCommand( { addshard : "localhost:10001" } ) { "shardadded" : "shard0001", "ok" : 1 }
下面你需要告诉数据库你想在数据库和数据集的级别来分散存放你的数据。 这个时候,你需要分配一个key(或分配多个keys)来划分partition. 这个和对一个collection建立一个索引是类似的。
db.runCommand( { enablesharding : "test" } ) {"ok" : 1} db.runCommand( { shardcollection : "test.people", key : {name : 1} } ) {"ok" : 1}
步骤三:管理 通过config数据库,来查看集群状况
use config switched to db config show collections chunks databases lockpings locks mongos settings shards system.indexes version 这些collects包含了所有额sharding配置信息
是坤哥吗?呵呵 :D :D