Install, Test and Run MongoDB

MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.

A most up-to-date official installation procedure can be found at the MongoDB document page here. The followings are the old procedures kept here for reference. After following the official installation procedure, you can then jump to step 3 below to used the database. Most importantly, you may need to create a user for you to connect to the database via any client (e.g. mongolab) following step 3.4 below.

1. Install MongoDB

1.1 Configure the package management system (APT)

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10

1.2 Create a /etc/apt/sources.list.d/mongodb.list file using the following command.

echo ‘deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen’ | sudo tee /etc/apt/sources.list.d/mongodb.list

1.3 reload repository

sudo apt-get update

1.4 install the package

sudo apt-get install mongodb-10gen

or

sudo apt-get install mongodb-10gen=2.4.10

for a specific version

1.5 (optional) To pin a package to avoid unintended auto-upgrade

echo “mongodb-10gen hold” | sudo dpkg –set-selections

2. Run MongoDB

2.1 Start

sudo service mongodb start (or sudo service mongod start)

2.2 Stop

sudo service mongodb stop (or sudo service mongod stop)

2.3 Restart

sudo service mongodb restart (or sudo service mongod restart)

2.4 check status

sudo service mongodb status (or sudo service mongod status)

3. Use MongoDB (Manual from console)

3.1 Connect to a Database

mongo –port port-number –host host-name

e.g. mongo –port 27017 –host localhost

3.2 Select a DB

use db-name

3.3 all commands are very easy-to-understand and straightforward, “help” will show all of them.

3.4. Add users and passwords

Mongo DB allows localhost exception after initial installation before creating any users/passwords, meaning that it allows access to any connection requests (client or console) without username and password. You can create user and password as follows:

3.4.1 connect via “mongo” console command in terminal (sudo may be required)

shell: mongo

MongoDB shell version: 2.6.0
connecting to: test

3.4.2 switch to admin table

> use admin

switched to db admin

 

3.4.3 create/dorp/show the user

> db.createUser({user: “user”,pwd: “password”,roles:[{role: “userAdminAnyDatabase”,”db” : “admin”}, {“role” : “readWrite”,”db” : “admin”}]})

Successfully added user: {
“user” : “username”,
“roles” : [
{
“role” : “userAdminAnyDatabase”,
“db” : “admin”
},
{
“role” : “readWrite”,
“db” : “admin”
}
]
}

> db.dropUser(“user”)

> show users

3.4.4 restart mongo

sudo service mongodb restart (or sudo service mongod status)

note: the defaulted installed mongo start command did not enable the authentication, it runs like below:

#  /usr/bin/mongod –config /etc/mongod.conf

After adding user as above, clients are only asked for credential when try to use database but not on connecting to the database server/cluster. Also local exception is still available. You will need to enable the authentication by changing the start command to

# /usr/bin/mongod –auth –config /etc/mongod.conf

or enabling authentication by adding the followings in /etc/mongod.conf

for MongoDB prior to 3.0

auth=true

for MongoDB 3.0+

security:
   authorization: enabled

after enabling authentication, connect to database via shall "mongo" command will require user name and password, e.g.

mongo localhost:27017/admin -u user-name -p password

4. Use MongoDB in applications (via MongoDB Drivers and Client Librapgadmin3ries)

4.1 Available MongoDB Drivers and Client Libraries

  • JavaScript
  • Python
  • Ruby
  • PHP
  • Perl
  • Java
  • Scala
  • C#
  • C
  • C++
  • Haskell
  • Erlang

5. Mongo Admin/Management GUIs

There are a lot of 3rd party GUI apps available for administrating and managing MongoDB. I installed a web based mViewer. Installation is simply download and extract that package to you desired directory (e.g. ~/workplace/db-admin/mongo) and start it in that directory via

/start_mViewer.sh <port>

Where port is optional and default is 8080.

you can then access the GUI web page via http://server-URI:8080/index.html

6. Allow remote client to connect to MongoDB

The default configuration comes with Mongo package only allows local connections. To allows remote clients, need to modify the Mongo config file “/etc/mongod.conf” as instructed in the config file:

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1

comment out “bind_ip = 127.0.0.1” as

#bind_ip = 127.0.0.1

will allow remote clients.