For the first part you will need to setup MongoDB 2.6+ using Homebrew in Mac OS X and second configure a database for building a REST API using NodeJS on a local machine. The second part should be similar for Linux and Windows with exceptions to the dbpath, mongod.conf, and mongo.log locations.
Setup
You will need to have Homebrew installed. To learn how to install Homebrew read the Homebrew Wiki.
Once you have Homebrew installed. Open a terminal window and enter the following:
1 |
brew install mongodb |
Once MongoDB has finished installing open /usr/local/etc/mongod.conf and make a note where the default database is going to be located for future reference, the dbpath. You will need this location later. Of course you can change the location to anywhere else.
Configure
In MongoDB version 2.6+, the format of mongod.conf is in YAMLformat. You can copy and paste the following into mongod.conf to update it to YAML format:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# Store data in /usr/local/var/mongodb instead of the default /data/db
storage:
dbPath: "/usr/local/var/mongodb"
# Append logs to /usr/local/var/log/mongodb/mongo.log
systemLog:
destination: file
path: "/usr/local/var/log/mongodb/mongo.log"
logAppend: true
net:
# Only accept local connections
bindIp: 127.0.0.1
# Enables the HTTP interface, default: false (v2.6)
http.enabled: true
# Enables the simple REST API, default: false
http.RESTInterfaceEnabled: true
|
For more on the configuration options see MongoDB Configuration File Options and MongoDB Network Exposure and Security.
In the following next steps you will need two (2) terminal windows open.
- In one terminal window enter (note that the port is an optional parameter, the default port is 27017). If you changed the path in dbpath, changed it below:1mongod --port 27017 --dbpath /usr/local/var/mongodb
- In the second terminal window enter mongo. This give you access to the database. If you enter show dbs in mongo, it will list database name: admin (empty).
- Next enter the following to create the database administrator user for the admin database:123456789use admindb.createUser( {user: "dbAdmin",pwd: "dbAdmin",roles:[ {role: "userAdminAnyDatabase",db: "admin"} ] } )
Reference: MongoDB Enable Authentication after Creating the User Administrator
- Now go back to the first terminal window and press control + c to terminate mongod. Then start mongod again by entering:1mongod --auth --config /usr/local/etc/mongod.conf
- Now it is time to add a user and to create a new database:123456789mongo -u dbAdmin -p dbAdmin --authenticationDatabase adminuse mydbdb.createUser( {user: "node",pwd: "node",roles: [{ role: "readWrite", db: "mydb" }] } )
For more on db.createUser see MongoDB db.createUser()
- Afterwards it should have created a new user and a database called mydb. You can test it out by doing:12345mongo -u node -p node --authenticationDatabase mydbuse mydbj = { name : "test" }db.mydb.insert(j)
It should display WriteResult({ “nInserted” : 1 }).
You can also use Robomongo (Mac OS X, Windows, Linux) to check if the database has correct information stored.
Reference: Getting Started with MongoDB