I like Mongo for running PoC projects where I need a quick database and don’t know how database is going to grow. It’s fast, works with JSON, and I don’t have to worry about fields being null or being of a certain data type.
While helping a coworker install the Private Bower locally, I realized how few resources there are for installing MongoDB on a Mac successfully. Instead you’ll have to traverse StackOverflow to find all the answers to all of the bugs you’ll come across.
The first step, if you have Homebrew installed, is to use Homebrew to get MongoDB.
$ brew install mongo
If you don’t have Homebrew, I steroid-injectedly suggest you get it. You might have to update X-Code or the X-Code CLI in the process. But once you have it installing big ticket items like Mongo will be so much easier.
So installing wasn’t hard. But does it work?
In a command line, try typing the following to start your Mongo server:
You’ll very likely get an error telling you something about how a data file doesn’t exist where it’s expected, or that it has permission errors and can’t write to those files. And this is where the adventure begins.
Mongo looks for a particular location on your machine to write data to. If it can’t find it, or it doesn’t have write access, it’ll throw an error and shut down. So let’s take care of that right now.
First, make sure that at the root of your machine, you have a folder called data/, and inside it a subdirectory called db/. If not, you can always run from the command line:
$ sudo mkdir /data $ sudo mkdir /data/db
Now your directories exist, and that’s awesome. But does your database have write access to them? Because if not, you’ll get another error and Mongo won’t start.
The simplest answer is to open that folder up for all users on your machine, so that whatever user Mongo is assigned to can always have access. In a production environment, you’ll want to lock it down much tighter to make sure that only the Mongo user has access. But hey, we’re in dev. We can fudge things a little 🙂
In the command line, we’ll change permissions to the dangerous but effective, spray-and-pray 777:
$ sudo chmod -R /data 777
That’s going to give every user on your machine access to the /data folder, and all of its subdirectories. Now if you try running your Mongo server, it should start up on port 27017. Ctrl+C to exit.