Σχόλια 0

Το κείμενο του εγγράφου

First verify Mongodb runs in one process, a single mongod daemon which is the database server. Thereare several ways to start the mongo database:

1)

>sudo service mongod start

if you installed this as a service

2)

Using the command line;

>sudo mongod

Running mongod without any parameters will run mongod withdefault parameters for thedbpath(

/data/db), logfile path(stdout) and port(27017+1000) are used.

You should see an OK sign after starting the mongod process. If you don’t see this you have aconflict preventing mongod from starting.

Starting mongod: [ OK ]

Another

way to check and verify if mongod is running is to do ps which will list the processesrunning on

the machine:

[ec2-user@centos-ami ~]$ ps-ef | grep mongod

mongod 1261 1 0 Aug20 ? 00:00:01 /usr/bin/mongod-f /etc/mongod.conf

ec2-user 26587 7979 0 07:27 pts/0 00:00:00 grep mongod

[ec2-user@centos-ami ~]$

3)

To test the mongoddaemon is functioning correctly, start the mongo client using

>mongo. You should see something like:

[ec2-user@centos-ami ~]$ mongo

MongoDB shell version: 2.0.7

connecting to: test

>

Like a SQL database, check you can view the databases and you can write and read data. Usuallylisting the databases is good enough, reading and writing data into a database should work.

>show dbs

config 0.0625GB

local (empty)

>

OK for one node it looks like everything is functioning. We can insert data using the commandline interface as shown above or python to insert many records at a time.

To add/list data using the above mongo command line:

First create an object in JSON notation. JSON objects are enclosed in braces, {} and consist of keyvalue pairs. Here is a sample record:

> record = {date:new Date('09/01/1933'),name:'a',age:'100'}

To insert the record into a database test, we don’t need to create the table or collection test, wedeclare it and the mongo client will create the table or collection for us automatically if itdoesn’t exist. If it exists, it will check for duplicates and issue an error messsage if there is aduplciate.

>db.test.insert(record)

>db.test.find() , will display everything in the collection test in database db.

The problem with the command line interface is it doesn’t allow the user to define loops orvariables like a programing language command line interface.This is an improvement indatabaseuser interfaces

where users can interface to a database without having to write anODBC program.To insert multiple rows, use

pymongo.

You will have to install pymongo first if it isn’t already installed:

http://api.mongodb.org/python/current/installation.html

Before running the pymongo scripts below it is assumed the reader has some familiarity withpython.If you are learning python with little programming background you can use the Udacitymaterials to learn idioms which are enough to write database administration and testingprograms. There are 6 units with code, units 2-5 are useful and each unit takes about a week todo.This is a good introduction to Python :www.udacity.com

cs101.There is additional practicematerial on Unit 2 and Unit 3 at codingbat.com. Do strings1/2 after Unit 2 and Lists1/2 after Unit3.This approach has been tested on people with no programming background.

This screenshot is

now obsolete:

Starting up a cluster:

A mongodb cluster is one which has shards where data coming into the database is distributed acrossthe shards. If there are 3 shards, s1, s2 and s3 then the first write would go to s1, the second to s2 andthe third to s3. We should be able to see this in

action as we send data to the shard.

Start a cluster on one node first; make 3 data directories for each shard and start mongod daemons oneach data directory:

sudo mkdir–p /slave1/db

sudo mkdir–p /slave2/db

sudo mkdir–p /slave3/db

sudo mongod–dbpath /slave1/db–port 20000

sudo mongod–dbpath /slave2/db–port 25000

sudo mongod–dbpath /slave3/db–port 27000

The master is mongos which distributes the writes across each shard.

sudo mongos–configdb localhost:20000,localhost:25000,localhost:27000

Note: make sure there are no spaces after the commas.

Do we need a config server? yes

Start mongo shell

Use admin

Db.runCommand({“addShard”:”localhost:20000”})

What does the add shard command do?Now you can add data to localhost:27017 and you don’t get thecant find shard for db error.

The ip address/port of the server is set using bindip/port. Will assume we are running mongod on thelocal machine.

Do you need cpu=true for the cpu utilization for MMS?

One feature of mongodb is replication which allows recovery from a single point of failure.

Configuration 2:

Setting up master/slave

replication:

Finding your data:One of the concepts in distributed nosql databases which doesn’t exist in SQLdatabase is the existence of a hash function. Your data is routed to some server based on a hashfunction. There will be scenarios where you will need to find a particular pieceof data on a particularmachine and you will also need to know what happens to that piece of data if nodes are added andremoved either due to failure or maintenance.