手机版
你好,游客 登录 注册
背景:
阅读新闻

MongoDB集群搭建及Sharding的实现思路

[日期:2014-04-07] 来源:Linux社区  作者:yijiu [字体:大 中 小]

部署分片集群

规划:

服务器IP

服务器角色

操作系统

172.23.215.61

config server、mongos、mongod

Debian GNU/Linux 7.2

172.23.214.50

config server、mongos、mongod

Debian GNU/Linux 7.2

172.23.214.47

mongos、mongod

Debian GNU/Linux 7.2

配置文件路径:

/mongo/etc/configsvr.conf

/mongo/etc/mongod.conf

/mongo/etc/mongos.conf

日志路径:

config server /var/log/mongo/mongod_config_server.log

mongod /var/log/mongo/mongod.log

mongos /var/log/mongo/mongos.log

数据存放路径:

/mongo/data/configdb

/mongo/data/configsvr

/mongo/data/mongod

1.建立副本集

修改各节点配置文件,如下所示:

root@datanode1:/mongo/etc# cat mongod.conf | grep -v "#"
logpath=/var/log/mongo/mongod.log
logappend=true
fork = true
port = 27017
dbpath=/mongo/data
pidfilepath = /var/run/mongodb/mongod.pid
replSet = rs0
rest = true
maxConns=1024
oplogSize=2048

启动各个节点mongod

root@datanode1:/mongo/etc# mongod -f mongod.conf

将master添加进副本集

root@datanode1:/mongo/etc# mongo> rsconf = { _id: "rs0",members: [ { _id: 0, host: "172.23.215.61:27017" } ] }

{
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "172.23.215.61:27017"
}
]
}

激活副本集

> rs.initiate( rsconf )
{
"info" : "Config now savedlocally. Should come online in about a minute.",
"ok" : 1
}
> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "172.23.215.61:27017"
}
]
}

陆续添加mongod节点

rs0:PRIMARY> rs.add("172.23.214.50:27017")

rs0:PRIMARY> rs.add("172.23.214.47:27017")

查看成员

rs0:PRIMARY> rs.conf()

{

"_id" :"rs0",

"version" : 3,

"members" : [

{

"_id" : 0,

"host" :"172.23.215.61:27017"

},

{

"_id" : 1,

"host" :"172.23.214.50:27017"

},

{

"_id" : 2,

"host" :"172.23.214.47:27017"

}

]

}

测试:

新建集合并插入数据,在从库查看是否同步

rs0:PRIMARY> use test11;
switched to db test11
rs0:PRIMARY> for(i=1;i<=100;i++)db.testcoll.insert({Name:"User"+i,Age:i,Gender:"M",preferbook:["bluebook","yellow book"]})

去从库查看

rs0:SECONDARY> show dbs;
local 2.0771484375GB
test11 0.203125GB
testdb 0.203125GB

 

2.配置config server

新建立配置文件,各节点内容相同

root@datanode1:/mongo/etc# cat configsvr.conf
configsvr=true
dbpath=/mongo/data/configdb/
port=27019
logpath=/var/log/mongo/mongod_config_server.log
logappend=true
fork=true

启动config server

root@datanode1:/mongo/etc# mongod -f configsvr.conf

查看端口是否被监听

root@datanode1:/mongo/etc# netstat -lnt | grep 27

tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN

 

3.配置mongos

root@datanode1:/mongo/etc# cat mongos.conf

# mongo.conf

port=27020

configdb=172.23.215.61:27019,172.23.214.47:27019,172.23.214.50:27019 #指定各config server的ip地址或hostname,以及端口

fork = true

logpath=/var/log/mongo/mongos.log

chunkSize = 100

logappend = true

启动路由(mongos)

root@datanode1:/mongo/etc# mongos -f mongos.conf

使用mongo命令连入mongos实例

root@datanode1:/mongo/etc# mongo 172.23.215.61:27020

向分区集群中添加各shard服务器或副本集,如果添加的shard是副本集,则需要使用如下格式:

mongos> sh.addShard("rs0/172.23.215.61:27017")

{ "shardAdded" : "rs0", "ok" : 1 }

查看状态

mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" :ObjectId("532fa07065574826130abfbf")
}
shards:
{ "_id" : "rs0", "host" :"rs0/172.23.214.47:27017,172.23.214.50:27017,172.23.215.61:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config"}
{ "_id" : "test11", "partitioned" : false, "primary" : "rs0" }
{ "_id" : "testdb", "partitioned" : false, "primary" : "rs0" }

4.启用sharding功能

mongodb的shard功能实现于collection级别,但若要在collection上启动shard,还需要事先其相关的数据库上启用之。在数据库上启用shard功能后,MongoDB会为其指定一个主shard。

激活数据库分片

switched to db adminmongos

mongos> db.runCommand( { enablesharding : "test11" } );
{ "ok" : 1 }
mongos> db.runCommand( { enablesharding : "testdb" } );
{ "ok" : 1 }

激活collection分片

mongos> use test11

mongos> show collections
system.indexes
testcoll #
将此表进行激活

进入admin库,激活collection分片并配置shard key唯一

>use admin

mongos> db.runCommand( { shardcollection : "test11.testcoll",key : {_id : 1 } , unique : true } )
{ "collectionsharded" : "test11.testcoll", "ok" :1 }

查看分片状态信息

mongos> db.printShardingStatus();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" :ObjectId("532fa07065574826130abfbf")
}
shards:
{ "_id" : "rs0", "host" :"rs0/172.23.214.47:27017,172.23.214.50:27017,172.23.215.61:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config"}
{ "_id" : "test11", "partitioned" : true, "primary" : "rs0" }
test11.testcoll
shard key: { "_id" : 1 }
chunks:
rs0 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id": { "$maxKey" : 1 } } on : rs0 Timestamp(1, 0)
{ "_id" : "testdb", "partitioned" : true, "primary" : "rs0" }
{ "_id" : "test", "partitioned" : false, "primary" : "rs0" }

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款