首页 > 代码库 > mongodb cluster

mongodb cluster

假设三台机器,ip分别为192.168.1.10,192.168.1.11,192.168.1.12,分别在每台电脑上下载并解压mongodb,关闭防火墙或者开放防火墙端口

一、安装shard

配置文件,假设地址为/etc/mongod.conf,配置如下

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/shard/mongod.pid
sharding:
  clusterRole: shardsvr


replication:
  replSetName: shard
  oplogSizeMB: 100
net:
  port: 27017
  bindIp: 192.168.1.<ip最后一段>
#storage:
  dbPath: /home/mongodb/data/db
  journal:
    enabled: true
systemLog:

  destination: file
  path: /home/mongodb/data/shard.log

  logAppend: true

三台机器,除了ip,其他配置都一样,注意目录若不存在,则需要先mkdir

然后分别运行一个mongodb实例

mongod --config /etc/mongod.conf

二、安装config

三台机器的配置改为如下

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/config/mongod.pid

sharding:

  clusterRole: configsvr

replication:

  replSetName: config

net:

  port: 28017
  port: 29017

#storage:
  dbPath: /home/mongodb/data/config
  journal:
    enabled: true
systemLog:

  destination: file
  path: /home/mongodb/config/shard.log

  logAppend: true
 

然后再分别运行mongodb实例

三、配置mongos

三天机器的配置改为如下

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongos/mongod.pid
sharding:
  configDB: config/192.168.1.10:28017,192.168.1.11:28017,192.168.1.13:28017
net:
  port: 29017
  bindIp: 192.168.1.<?>
systemLog:
  destination: file
  path: /home/mongodb/data/mongos.log
  logAppend: true

然后每台机器使用mongos运行实例

mongos --config /etc/mongod.conf

四、连接cluster

随便连接一个节点,并进入admin数据库

mongo 192.168.1.11:29017/admin

查看当前数据库

db

添加分片

db.runCommand({addShard: "shard/192.168.1.10:27017,192.168.1.11:27017,192.168.1.12:27017",name:"s1",maxSize:40960});

name为replica set 的唯一名称,maxSize为每个节点最大存储(MB)

此时可查看刚才添加的分片

db.runCommand({listShards: 1})

 

mongodb cluster