[MongoDB] How to start two instances on different ports (Mac / Homebrew)

Tadashi Shigeoka ·  Thu, March 27, 2014

Here’s how to start two MongoDB instances on different ports on Mac with Homebrew.

MongoDB | モンゴディービー

MongoDB Configuration (First: Base)

% cat /usr/local/etc/mongod.conf 
# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb

# Append logs to /usr/local/var/log/mongodb/mongo.log
logpath = /usr/local/var/log/mongodb/mongo.log
logappend = true

# Only accept local connections
bind_ip = 127.0.0.1

MongoDB Configuration (Second: Different port)

The default starts on port 27017, so we’ll configure the second instance to start on port 27018.

% mkdir /usr/local/var/mongodb_27018
% cp /usr/local/etc/mongod.conf /usr/local/etc/mongod_27018.conf

Edit mongod_27018.conf as follows.

※ We edited dbpath and logpath, and changed the port from the default 27017 to 27018 to avoid duplication.

% cat /usr/local/etc/mongod_27018.conf
# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb_27018

# Append logs to /usr/local/var/log/mongodb/mongo.log
logpath = /usr/local/var/log/mongodb/mongo_27018.log
logappend = true

# Only accept local connections
bind_ip = 127.0.0.1

# Default port: 27017
port = 27018

Start mongod.

% mongod run --config /usr/local/etc/mongod_27018.conf &

Confirm that two mongod instances are running on different ports.

% lsof -i | grep mongod
mongod      365 your_username    9u  IPv4 0xd006e2ded5999795      0t0  TCP localhost:28017 (LISTEN)
mongod      365 your_username   10u  IPv4 0xd006e2deda2a3f7d      0t0  TCP localhost:27017 (LISTEN)
mongod    61419 your_username    9u  IPv4 0xd006e2dee3ff4795      0t0  TCP localhost:28018 (LISTEN)
mongod    61419 your_username   10u  IPv4 0xd006e2dee39db795      0t0  TCP localhost:27018 (LISTEN)

That’s it.

That’s all from the Gemba.