---
title: "MongoDB cluster 搭建"
author: "aryanwang"
date: "2012-04-10T09:27:13Z"
tags: []
categories: ["数据库"]
slug: "mongodb-cluster"
draft: false
lastmod: "2012-04-10T09:27:13Z"
id: 1479637
url: "https://keepmoving2012.iteye.com/blog/1479637"
---

**1.配置两个shard**

 

```
mongod.exe --shardsvr --dbpath D:\temp\cluster\shard11 --replSet shard1 --port 27017 --oplogSize 100 --logpath D:\temp\cluster\shard11\shard11.log --logappend
mongod.exe --shardsvr --dbpath D:\temp\cluster\shard12 --replSet shard1 --port 27017 --oplogSize 100 --logpath D:\temp\cluster\shard12\shard12.log --logappend
mongod.exe --shardsvr --dbpath D:\temp\cluster\shard13 --replSet shard1 --port 27017 --oplogSize 100 --logpath D:\temp\cluster\shard13\shard13.log --logappend
mongo.exe localhost:27017
config = {_id: 'shard1', members: [
                          {_id: 0, host: 'hostname1:27017'},
                          {_id: 1, host: 'hostname2:27017'},
                          {_id: 2, host: 'hostname3:27017'}]
           }
           
rs.initiate(config);


mongod.exe --shardsvr --dbpath D:\temp\cluster\shard21 --replSet shard2 --port 27018 --oplogSize 100 --logpath D:\temp\cluster\shard21\shard21.log --logappend
mongod.exe --shardsvr --dbpath D:\temp\cluster\shard22 --replSet shard2 --port 27018 --oplogSize 100 --logpath D:\temp\cluster\shard22\shard22.log --logappend
mongod.exe --shardsvr --dbpath D:\temp\cluster\shard23 --replSet shard2 --port 27018 --oplogSize 100 --logpath D:\temp\cluster\shard23\shard23.log --logappend
mongo.exe localhost:27018
config = {_id: 'shard2', members: [
                          {_id: 0, host: 'hostname1:27018'},
                          {_id: 1, host: 'hostname2:27018'},
                          {_id: 2, host: 'hostname3:27018'}]
           }
           
rs.initiate(config);
```

 

**2 配置config server**

 

```
mongod.exe --configsvr --dbpath D:\temp\cluster\config --port 20000 --oplogSize 100 --logpath D:\temp\cluster\config\config.log --logappend
mongod.exe --configsvr --dbpath D:\temp\cluster\config --port 20000 --oplogSize 100 --logpath D:\temp\cluster\config\config.log --logappend
mongod.exe --configsvr --dbpath D:\temp\cluster\config --port 20000 --oplogSize 100 --logpath D:\temp\cluster\config\config.log --logappend
```

 

**3 配置路由节点**

 

```
mongos.exe --configdb hostname1:20000,hostname2:20000,hostname3:20000 --port 30000 --chunkSize 5 --logpath D:\temp\cluster\mongos.log --logappend
mongos.exe --configdb hostname1:20000,hostname2:20000,hostname3:20000 --port 30000 --chunkSize 5 --logpath D:\temp\cluster\mongos.log --logappend
mongos.exe --configdb hostname1:20000,hostname2:20000,hostname3:20000 --port 30000 --chunkSize 5 --logpath D:\temp\cluster\mongos.log --logappend
```

 

**4. 添加shard**

 

```
mongo.exe hostname1:30000/admin

db.runCommand( { addshard : "shard1/hostname1:27017,hostname2:27017,hostname3:27017",name:"s1",maxsize:20480} );
db.runCommand( { addshard : "shard2/hostname1:27018,hostname2:27018,hostname3:27018",name:"s2",maxsize:20480} );
```

 

 5 设置数据库分片和集合分片

 

```
use admin
db.runCommand( { enablesharding : "test"} );
db.runCommand({shardcollection:'test.hello',key:{id1:1,id2:1}})
```