欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

搭建MongoDB集群

程序员文章站 2022-04-24 20:20:17
...

Shards Server: 每个shard由一个或多个mongod进程组成,用于存储数据 Config Server: 用于存储集群的Metadata信息,包括每个Shar

集群由以下3个服务组成:

Shards Server: 每个shard由一个或多个mongod进程组成,用于存储数据
Config Server: 用于存储集群的Metadata信息,包括每个Shard的信息和chunks信息
Route Server: 用于提供路由服务,由Client连接,使整个Cluster看起来像单个DB服务器
另外,Chunks是指MongoDB中一段连续的数据块,默认大小是200M,一个Chunk位于其中一台Shard服务器上

下面,搭建一个Cluster,它由4台服务器组成,包括2个Shard,3个Config,,1个Route

其中每个Shard由一个Replica Set组成,每个Replica Set由2个Mongod节点,1个vote节点组成

搭建MongoDB集群

需要分别建立目录/opt/soft/mongodb/data/data ,/opt/soft/mongodb/log

192.168.11.229
/opt/soft/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set1 –dbpath /opt/soft/mongodb/data/data –logpath /opt/soft/mongodb/log/log.log
/opt/soft/mongodb/bin/mongod –fork –shardsvr –port 10001 –replSet set2 –dbpath /opt/soft/mongodb/data/data1 –logpath /opt/soft/mongodb/log/log1.log
192.168.11.17
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set1 –dbpath /opt/soft/mongodb/data/data –logpath /pvdata/mongodb_log/mongod.log
192.168.11.227
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set2 –dbpath /opt/soft/mongodb/data/data –logpath /opt/soft/mongodb/log/mongod.log
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10001 –replSet set1 –dbpath /opt/soft/mongodb/data/data1 –logpath /opt/soft/mongodb/log/mongod1.log

192.168.11.228
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set2 –dbpath /opt/soft/mongodb/data/data –logpath /opt/soft/mongodb/log/mongod.log

192.168.11.229上mongo –port 10000连接mongo

config = {_id: 'set1', members: [
{_id: 0, host: '192.168.11.229:10000'},
{_id: 1, host: '192.168.11.17:10000'},
{_id: 2, host: '192.168.11.227:10001', arbiterOnly: true}
]}
rs.initiate(config)
rs.status()

192.168.11.227上mongo –port 10000连接mongo
config = {_id: 'set2', members: [
{_id: 0, host: '192.168.11.227:10000'},
{_id: 1, host: '192.168.11.228:10000'},
{_id: 2, host: '192.168.11.229:10001', arbiterOnly: true}
]}
rs.initiate(config)
rs.status()

192.168.11.229:;192.168.11.17;192.168.11.228上启动进程
mongod --configsvr --fork --logpath /opt/soft/mongodb/log/config.log --logappend --dbpath /opt/soft/mongodb/data/configdata --port 20000

192.168.11.227上启动进程

mongos --fork --configdb "192.168.11.229:20000,192.168.11.17:20000,192.168.11.228:20000" --logpath /opt/soft/mongodb/log/mongos.log

192.168.11.227

mongos> use admin
mongos> db.adminCommand({ addShard : "set1/192.168.11229:10000,192.168.11.17:10000"})
mongos> db.adminCommand({ addShard :"set2/192.168.11.227:10000,192.168.11.228:10000"})
mongos> db.adminCommand({enablesharding:'test'})
mongos> db.adminCommand({listshards:1})
mongos> printShardingStatus()
mongos> db.adminCommand({shardcollection:'test.test2', key:{_id:1}, unique : true})

导入文本数据(数据以,号隔开)

mongoimport -h 192.168.11.227 -d test -c test --type csv -f ip,sid,uid,cateid,type --file /opt/dm/meta/uidip_20111017

搭建MongoDB集群