启动route process 配置route process并且启动,代码如下: [root@localhost ~]# /usr/local/monGo/bin/mongos --port 40000
启动route process
配置route process并且启动,代码如下:
[root@localhost ~]# /usr/local/monGo/bin/mongos --port 40000 --configdb localhost:30000 --fork --logpath /data/shard/log/route.log --chunkSize 1
forked process: 2911
[root@localhost ~]# all output going to: /data/shard/log/route.log
配置sharding
[root@localhost ~]# /usr/local/mongo/bin/mongo --port 40000
mongoDB shell version: 2.0.4
connecting to: 127.0.0.1:40000/test
mongos>
这个地方一定要切换到admin数据库
mongos> use admin
switched to db admin
mongos> db
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 }
momongos> for(var i =1;i <= 500000;i++) db.users.insert({age:i,name:"fangwei",addr:"hangzhou",country:"China"})
mongos> db.users.find()
{ "_id" : ObjectId("53D4eefd67d21d1d69f5bcd0
{ "_id" : ObjectId("53d4f024b1e3fc47712a0576
{ "_id" : ObjectId("53d4f024b1e3fc47712a0577
---验证我们的分片
mongos> db.users.stats()
{
}
mongos>
查看磁盘上物理文件分布情况
mongos> exit
bye
[root@localhost ~]# ll /data/shard/s0/test
total 213004
-rw-------. 1 root root
-rw-------. 1 root root 134217728 Jul 27 20:09 test.1
-rw-------. 1 root root
drwxr-xr-x. 2 root root
[root@localhost ~]#
[root@localhost ~]# ll /data/shard/s1/test
total 475148
-rw-------. 1 root root
-rw-------. 1 root root 134217728 Jul 27 20:27 test.1
-rw-------. 1 root root 268435456 Jul 27 20:27 test.2
-rw-------. 1 root root
drwxr-xr-x. 2 root root
[root@localhost ~]#
---列出所有的shard server
mongos> use admin
switched to db admin
mongos> db.runCommand({listshards:1})
{
}
-----查看sharding信息
mongos> printShardingSizes()
--- Sharding Status ---
-------判断是佛sharding
> db.runCommand({isdbgrid:1})
{ "isdbgrid" : 1, "hostname" : "localhost.mongo102", "ok" : 1 }
>
----------对现有表执行sharding
> db
test
> db.users_2.stats()
{
}
对其进行分表
> db
admin
> db.runCommand({shardcollection:"test.users_2",key:{_ud:1}})
{ "collectionsharded" : "test.users_2", "ok" : 1 }
> use test
switched to db test
> db.users_2.stats()
{
}
>
-------新增shard server 分片
[root@localhost ~]# mkdir /data/shard/s2
[root@localhost ~]# /usr/local/mongo/bin/mongod --shardsvr --port 20002 --dbpath /data/shard/s2 --fork --logpath /data/shard/log/s2.log --directoryperdb
[root@localhost ~]# forked process: 3480
all output going to: /data/shard/log/s2.log
[root@localhost ~]# /usr/local/mongo/bin/mongo --port 40000
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:40000/test
mongos> db
test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"localhost:20002"})
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos>
--结束END--
本文标题: mongodb shard 分片技术
本文链接: https://lsjlt.com/news/46992.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0