If you are reading this, chances are that you need to deal with unstructured data. =)
First, install mongodb using homebrew!
brew install mongodb
Now we need to set up a dir for mongodb to keep its data. For simplicity, we will just use its default db path /data/db. We will need to change the permission of that dir to your username and your group. Run ls -ld ~ to get that info.
sudo mkdir -p /data/db
sudo chown -R your_user_name /data
sudo chgrp -R your_group_name /data
At this point, we are ready to run/start mongod. We could run mongod directly (the log info will come into your terminal…). My preference is to run mongod in the background.
First install this. It will enable you to handle the launchctl on osx a lot easier.
brew tap gapple/services
Then you can now make the following calls whenever. brew services basically calls launchctl under the hood.
brew services start mongodb
brew services stop mongodb
While mongodb has been started with brew services, if you take a look at /Users/[your_user_name]/Library/LaunchAgents/, you will see the homebrew.mxcl.mongodb.plist file sitting there. Take a peek at the file inside, you will see that the conf file is at /usr/local/etc/mongod.conf, and stdout/stderr gets forwarded to /usr/local/var/log/mongodb/output.log (not so useful…).
In the mongod.conf file, we see that the log goes to /usr/local/var/log/mongodb/mongo.log. That would be where you look for the server pid, port, dbpath, etc.
Now start up mongod, and then call mongo to run some commands!
brew services start mongodb
mongo
Run a few of these below to play around
show dbs -- just admin and local
use my_db -- at this point, we haven't created my_db yet
db -- showing the current selected db, which is my_db
db.books.insert({'name': 'Harry Potter', 'Vol': 1})
-- "db" refers to my_db, "books" is our "collection"
show collections -- you will see "books" there, kinda like a db table
db.books.find() -- you will see our newly inserted data row
db.books.find({"Vol":1}) -- with condition
show dbs -- you will see our newly created db "my_db"
exit
See the documentation on db.dollection.find here.
To exit the mongo shell, just type exit. To stop mongod, issue brew services stop mongodb.
You may wonder how to create users and set up authentication. brew services doesn’t do this, so we will need to ditch brew services and do the following instead.
First make a copy of the default plist conf file.
brew services start mongodb
cp /Users/[your_user_name]/Library/LaunchAgents/homebrew.mxcl.mongodb.plist ~
brew services stop mongodb
Run mongod –noauth to start up the server (without authentication!!!). Then run mongo to connect.
use test -- this is our test db
db.createUser({user: "testUserAdmin", pwd: "password", roles: [{role: "readWrite", db: "test"}]})
exit
Notice you get the Successfully added user response from the createUser call. If you need to drop a user do something like db.dropUser(“testUserAdmin”).
The above createUser command created a testUserAdmin user with password being the secret password… See all the built-in roles.
Then Ctrl-C to quit server, and run mongod –auth to start up the server, this time with authentication. Then run mongo -u testUserAdmin -p password to login to the test db.
If you made a copy of homebrew.mxcl.mongodb.plist, you could look at the settings and run the server with something like the following. Then you can read the log with tail -f /usr/local/var/log/mongodb/mongo.log
mongod --auth --config /usr/local/etc/mongod.conf >>/usr/local/var/log/mongodb/output.log 2>>/usr/local/var/log/mongodb/output.log &
[Now your shell is freed up]
See more info at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/ and http://docs.mongodb.org/manual/tutorial/add-user-administrator/