MONGODB_HOME="`pwd`"
${MONGODB_HOME}/bin/mongod \
--configsvr \
--dbpath ${MONGODB_HOME}/data/db \
--port 40000 \
--fork \
--pidfilepath ${MONGODB_HOME}/mongodb.pid \
--logpath ${MONGODB_HOME}/logs/mongod.log --logappend --logRotate rename
其中
MONGODB_HOME="`pwd`"
${MONGODB_HOME}/bin/mongos \
--configdb 192.168.20.111:40000,192.168.20.111:40001,192.168.20.111:40002 \
--port 50000 \
--fork \
--pidfilepath ${MONGODB_HOME}/mongodb.pid \
--logpath ${MONGODB_HOME}/logs/mongod.log --logappend --logRotate rename
其中,
--configdb 192.168.20.111:40000,192.168.20.111:40001,192.168.20.111:40002指定了mongos保存相关分片集群数据到配置服务器,配置服务器的配置地址。
另外,再建立端口50001,50002,成立三个接收客户端的mongos服务器
mongos>sh.addShard("shard2/192.168.20.111:20000,192.168.20.111:20001,192.168.20.111:20002")
可以在参数中指定副本集的所有成员,也可以不指定全部成员,因为mongos能够自动检测到没有包含在副本集成员表中的成员,
MONGODB_HOME="`pwd`"
${MONGODB_HOME}/bin/mongod \
--shardsvr \
--replSet shard3 \
--dbpath ${MONGODB_HOME}/data/db \
--port 30000 \
--fork \
--pidfilepath ${MONGODB_HOME}/mongodb.pid \
--logpath ${MONGODB_HOME}/logs/mongod.log --logappend --logRotate rename \
--oplogSize 10
mongos> use admin;
switched to db admin
mongos> db.runCommand({listshards:1});
{
"shards" : [
{
"_id" : "shard1",
"host" : "shard1/192.168.20.109:10000,192.168.20.109:10001"
},
{
"_id" : "shard2",
"host" : "shard2/192.168.20.109:20000,192.168.20.109:20001"
},
{
"_id" : "shard3",
"host" : "shard3/192.168.20.109:30000,192.168.20.109:30001"
}
],
"ok" : 1
}
mongos> db.enableSharding("test");
2016-12-03T06:19:35.901-0800 E QUERY [thread1] TypeError: db.enableSharding is not a function :
@(shell):1:1
没以上方法,则使用以下命令
mongos> db.runCommand({enablesharding:"test"});
{
"ok" : 0,
"errmsg" : "enableSharding may only be run against the admin database.",
"code" : 13
}
则表明需要切换到admin才可以执行enableSharding命令
mongos> use admin;
switched to db admin
mongos> db.runCommand({enablesharding:"test"});
{ "ok" : 1 }
>db.shardCollection("test.foo", {"name":1});
2016-12-03T06:24:44.049-0800 E QUERY [thread1] TypeError: db.shardCollection is not a function :
@(shell):1:1
则使用以下命令
>db.runCommand({shardCollection:"test.foo", key:{"name":1}});
{ "collectionsharded" : "test.foo", "ok" : 1 }
>use test;
switched to db test
mongos> db.foo.insert({"name":"abc"});
WriteResult({ "nInserted" : 1 })
在这里,我们说下分片的策略。
分片策略有两种:
升序分片(Ranged Sharding)
根据片键的值将集合分成多个块,每个块包含一个连续的范围,这样片键值相近的文档就很可能处于同一块中,
这种情况下,片键创建的是升序索引
>db.createIndex({ "userId":1 })
升序分片
如果应用中比较倾向于范围查询的情况下,可以考虑升序分片这种策略,
如将用户id作为升序分片的片键:
>sh.shardCollection( "database.collection", { "userId":1 } );
>db.createIndex({ "userId" : "hashed" })
哈希分片
这种分片可以查询速度很快,
使用哈希值可以比较平均地将查询平衡到不同的分片中,达到分片的真正作用,
不过,有个缺点就是,在哈希分片情况下,如果需要范围查询,则需要查询各个分片,然后再聚合结果,这对分片的使用不太理想。
哈希分片:
>sh.shardCollection( "database.collection", { "userId" : "hashed" } )
#查看数据在分片上情况
mongos> db.foo.stats();
#使用sh.status()查看分片、数据库和分片集合的摘要信息
mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5842d23e32e5f535ca644556")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.20.109:10000,192.168.20.109:10001" }
{ "_id" : "shard2", "host" : "shard2/192.168.20.109:20000,192.168.20.109:20001" }
{ "_id" : "shard3", "host" : "shard3/192.168.20.109:30000,192.168.20.109:30001" }
active mongoses:
"3.2.8" : 3
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "test", "primary" : "shard1", "partitioned" : true }
test.foo
shard key: { "name" : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
https://github.com/jjavaboy/lam-nio/blob/master/lam-mongodb/src/main/java/lam/mongo/demo/ShardDemoTest.java