This post was originally published in February 2013 on my old blog. Some of this information may no longer work. I am posting this just as a reference for those curious as to how I did this originally back in 2013. For more up-to-date information, I would check the MongoDB site or Google.
I ran into an interesting problem the other day. I was working on a problem that required MongoDB’s new aggregation framework that comes standard with MongoDB ~> 2.2. However, I already had a local version of MongoDB v2.0 installed on my mac via Homebrew which I was using to test and development a production database that we were running. Searching the web, I found very little information about how to setup multiple versions of MongoDB on the same local environment.
So, here are the steps I used for setting up a separate instance of MongoDB. Again, I had already installed MongoDB via Homebrew using brew install mongodb, so I’ll show you how to install your own version outside of homebrew.
First, you’ll need to download a version of MongoDB from their website. In this case, we’ll use the latest production release which is 2.2.2 at the time of this post. In addition, I will be using the 64-bit OSX version from their site.
Then create an empty directory wherever you would like MongoDB to live. I call it mongo2_2 here but you can call it whatever you would like:
Then you need to untar the download into that directory. In this case it’s:
tar –zxvf ../mongodb-osx-x86_64-2.2.2.tgz
This should create a new directory mongodb-osx-x86_64-2.2.2. ‘cd’ into that directory and you will see that it has created a “bin” folder with all the MongoDB commands and executables. Some of these you may recognize, like the actual MongoDB process – mongod and the mongo shell, mongo.
Ok, you have mongo setup, but now you need a place to store the data. By default, mongod writes data to the /data/db/ directory. However, if you have installed MongoDB via Homebrew, then mongo is configured to write to /usr/local/var/mongodb
For the ease of use, we’re just going to use the directory we are currently in. We’ll create a “data” directory in the current directory which should be something like:
Ok, now you should be all setup for a new instance of MongoDB. But first, before we fire that up, there are a couple of things to check first. If you installed MongoDB via Homebrew, then more than likely, MongoDB is running on startup. You can check if this just by running the mongo shell with the “mongo” command:
If an instance of mongod process is running, then it should have no problem connecting. It might give you a message like
MongoDB shell version: 2.0.x
Uh-oh, not what we wanted. This means that your other version of MongoDB is still running and you need to stop it before you can start up the new version. To double-check this run:
ps -ef | grep mongo
If mongo is running, you should see mongod in your list.
usr/local/bin/mongod run --config /usr/local/etc/mongod.conf
To triple-check this, MongoDB runs off port 27017 by default, you can go to localhost:27017 and you should see a message like this:
You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number
You can cancel a mongod process by running the mongo shell with the “mongo” command then issuing the following commands in the shell:
You could try to just kill the mongod process in the terminal, but there are no guarantees about how that might affect your current MongoDB setup and database. I preferred the safe route this time.
Ok, now if you try to connect to MongoDB again using the mongo command. You should get an error:
Error: couldn't connect to server
Good, now we know that process is gone. However, if you plan to use this setup more than once, I would recommend one more change. The homebrew version of MongoDB is still setup to start automatically on startup. To turn this off, you need to edit a LaunchAgent file that Homebrew sets up by default. First, cd into the LaunchAgent directory
You should see a file called homebrew.mxcl.mongodb.plist. Open up this file in your favorite file editing software and change the following line:
The next time you start up again, MongoDB will not start by default.
We just need to set the configurations for our new MongoDB instance and then we should be good to go. ‘cd’ back into the directory where you setup MongoDB.
In order to tell MongoDB your settings when you start a mongod process, you will need to setup a config file. Create a new mongod.conf in this directory and include the following code:
# Store data in ./my_data instead of the default /data/db
dbpath = ./data
port = 28028
# Only accept local connections
bind_ip = 127.0.0.1%
dbpath tells mongo where you data should be stored, in this case, the data directory we created earlier. Port sets the port where the mongod instance should run. You’ll have to set this to something other than the default 27017 or mongo will complain. Bind_ip is important and prevents outsiders from trying to connect to your db.
You can now start your new instance of mongodb. However, you can’t use the “mongod” command, this is pointing to the executable of your homebrew version. ‘cd’ into the mongodb-osx-x86_64-2.2.2 and run:
./bin/mongod --config mongod.conf
This will tell the terminal to look for the mongod executable in the current directory and to use your config file for setup. You should see a message like:
MongoDB starting : port=29029 dbpath=./data
Woo-hoo! Alright, now you can connect via the shell to make sure. Open a new terminal tab. You’ll need to run the “mongo” command from the ./bin directory like we did with mongod. You will also have to specify the port or the shell will try to connect to the default port and throw and an error. Run this command
./bin/mongo –port 28028
You should see something like this:
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:29029/test
Success! Now you are free to play with a different version of MongoDB all you want without having to touch your current setup. When you are done, just remove the mongo2_2 directory and change the LaunchAgent back and you are all set!