UNPKG

6.39 kBMarkdownView Raw
1# The MongoDB Topology Management API
2
3The MongoDB Topology Management API is an API to allow to programatically spin up a MongoDB instance, Replicaset or Sharded cluster on your local machine.
4
5## Setting up a single instance
6
7It's very simple to create a single running MongoDB instance. All examples are using **ES6**.
8
9```js
10var Server = require('mongodb-topology-manager').Server;
11// Create new instance
12var server = new Server('binary', {
13 dbpath: f('%s/db', __dirname)
14});
15
16// Perform discovery
17var result = yield server.discover();
18// Purge the directory
19yield server.purge();
20// Start process
21yield server.start();
22// Stop the process
23yield server.stop();
24```
25
26## Setting up a replicaset
27
28It's equally easy to create a new Replicaset instance.
29
30```js
31var ReplSet = require('mongodb-topology-manager').ReplSet;
32
33// Create new instance
34var topology = new ReplSet('mongod', [{
35 // mongod process options
36 options: {
37 bind_ip: 'localhost', port: 31000, dbpath: f('%s/../db/31000', __dirname)
38 }
39}, {
40 // mongod process options
41 options: {
42 bind_ip: 'localhost', port: 31001, dbpath: f('%s/../db/31001', __dirname)
43 }
44}, {
45 // Type of node
46 arbiterOnly: true,
47 // mongod process options
48 options: {
49 bind_ip: 'localhost', port: 31002, dbpath: f('%s/../db/31002', __dirname)
50 }
51}], {
52 replSet: 'rs'
53});
54
55// Perform discovery
56var result = yield server.discover();
57// Purge the directory
58yield server.purge();
59// Start process
60yield server.start();
61// Stop the process
62yield server.stop();
63```
64
65Each of the node objects can take the following options at the top level.
66
67Field | Description
68----------------------------------|-------------------------
69arbiter | node should become an arbiter.
70builIndexes | should build indexes on the node.
71hidden | node should be hidden.
72builIndexes | should build indexes on the node.
73priority | node should have the following priority.
74tags | tags for the node.
75slaveDelay | the node slaveDelay for replication.
76votes | additional votes for the specific node.
77
78The **object** contains the options that are used to start up the actual `mongod` instance.
79
80The Replicaset manager has the following methods
81
82Method | Description
83----------------------------------|-------------------------
84Replset.prototype.discover | Return the information from running mongod with --version flag.
85Replset.prototype.start | Start the Replicaset.
86Replset.prototype.primary | Return the current Primary server manager.
87Replset.prototype.shardUrl | Return a add shard url string.
88Replset.prototype.url | Return a connection url.
89Replset.prototype.arbiters | Return a list of arbiter managers.
90Replset.prototype.secondaries | Return a list of secondary managers.
91Replset.prototype.passives | Return a list of secondary passive managers.
92Replset.prototype.waitForPrimary | Wait for a new primary to be elected or for a specific timeout period.
93Replset.prototype.stepDownPrimary | Stepdown the primary.
94Replset.prototype.configuration | Return the replicaset configuration.
95Replset.prototype.reconfigure | Perform a reconfiguration of the replicaset.
96Replset.prototype.serverConfiguration | Get the initial node configuration for specific server manager.
97Replset.prototype.addMember | Add a new member to the set.
98Replset.prototype.removeMember | Remove a member to the set.
99Replset.prototype.maintenance | Put a node into maintenance mode.
100Replset.prototype.stop | Stop the replicaset.
101Replset.prototype.restart | Restart the replicaset.
102Replset.prototype.purge | Purge all the data directories for the replicaset.
103
104## Setting up a sharded system
105
106It's a little bit more complicated to set up a Sharded system but not much more.
107
108```js
109var Sharded = require('mongodb-topology-manager').Sharded;
110
111// Create new instance
112var topology = new Sharded({
113 mongod: 'mongod', mongos: 'mongos'
114});
115
116// Add one shard
117yield topology.addShard([{
118 options: {
119 bind_ip: 'localhost', port: 31000, dbpath: f('%s/../db/31000', __dirname)
120 }
121}, {
122 options: {
123 bind_ip: 'localhost', port: 31001, dbpath: f('%s/../db/31001', __dirname)
124 }
125}, {
126 // Type of node
127 arbiter: true,
128 // mongod process options
129 options: {
130 bind_ip: 'localhost', port: 31002, dbpath: f('%s/../db/31002', __dirname)
131 }
132}], {
133 replSet: 'rs1'
134});
135
136// Add one shard
137yield topology.addShard([{
138 options: {
139 bind_ip: 'localhost', port: 31010, dbpath: f('%s/../db/31010', __dirname)
140 }
141}, {
142 options: {
143 bind_ip: 'localhost', port: 31011, dbpath: f('%s/../db/31011', __dirname)
144 }
145}, {
146 // Type of node
147 arbiter: true,
148 // mongod process options
149 options: {
150 bind_ip: 'localhost', port: 31012, dbpath: f('%s/../db/31012', __dirname)
151 }
152}], {
153 replSet: 'rs2'
154});
155
156// Add configuration servers
157yield topology.addConfigurationServers([{
158 options: {
159 bind_ip: 'localhost', port: 35000, dbpath: f('%s/../db/35000', __dirname)
160 }
161}, {
162 options: {
163 bind_ip: 'localhost', port: 35001, dbpath: f('%s/../db/35001', __dirname)
164 }
165}, {
166 options: {
167 bind_ip: 'localhost', port: 35002, dbpath: f('%s/../db/35002', __dirname)
168 }
169}], {
170 replSet: 'rs3'
171});
172
173// Add proxies
174yield topology.addProxies([{
175 bind_ip: 'localhost', port: 51000, configdb: 'localhost:35000,localhost:35001,localhost:35002'
176}, {
177 bind_ip: 'localhost', port: 51001, configdb: 'localhost:35000,localhost:35001,localhost:35002'
178}], {
179 binary: 'mongos'
180});
181
182// Start up topology
183yield topology.start();
184
185// Shard db
186yield topology.enableSharding('test');
187
188// Shard a collection
189yield topology.shardCollection('test', 'testcollection', {_id: 1});
190
191// Stop the topology
192yield topology.stop();
193```
194
195The Sharded manager has the following methods
196
197Method | Description
198----------------------------------|-------------------------
199Replset.prototype.discover | Return the information from running mongod with --version flag.
200Replset.prototype.start | Start the Sharded cluster.
201Replset.prototype.stop | Stop the replicaset.
202Replset.prototype.restart | Restart the replicaset.
203Replset.prototype.purge | Purge all the data directories for the replicaset.
204Replset.prototype.addShard | Add a new shard to the cluster.
205Replset.prototype.addConfigurationServers | Add a set of nodes to be configuration servers.
206ReplSet.prototype.addProxies | Add a set of mongo proxies to the cluster.
207ReplSet.prototype.enableSharding | Enable sharding on a specific db.
208ReplSet.prototype.shardCollection | Shard a collection.