Saturday, September 27, 2014

Working with Mongodb on Rails

Installing and running on Windows

Download and installl the mogodb for windows from the following link. If you're using 64bit, choose the 64bit ones.

http://www.mongodb.org/downloads

Now you need to install related gem files

gem install bson
gem install mongo
gem install bson_ext

if the installing of bson_ext failed, trying updating the gems and then t
gem update  --system
gem install bson_ext

if it still doesn't work, try installing the lower version
gem install bson_ext -v=1.9.2

Now set the database path to any place, in this example, I set it to c:/mongodb/data/

mongod.exe --dbpath C:\mongodb\data\
or
mongod.exe if the above command is already run

and by now, the data path will create and you will start seeing the waiting for connections

Running the mongodb shell for testing

C:\mongodb\bin\mongo.exe

Now try running the following command and your mongodb environment is set up successfully.

db.test.save( { a: 1 } )
db.test.find()

Setting up mongodb as a windows service


Configure the System

md C:\mongodb\log
echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg

Running as service

C:\mongodb\bin\mongod.exe --config C:\mongodb\mongod.cfg --install
net start MongoDB

Stop/Removing the service

net stop MongoDB
C:\mongodb\bin\mongod.exe --remove

Installing and running on Linux/Ununtu

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Create a /etc/apt/sources.list.d/10gen.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/10gen.list
Now you can update your repository

sudo apt-get update
And then install the package

sudo apt-get install mongodb-10gen
To check that it works, just issue a mongo in your shell and you should see something like

MongoDB shell version: x.x.x
connecting to: test
>

Download and install the mongodb for linux
curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.4.tgz
curl -O http://downloads.mongodb.org/linux/mongodb-linux-i686-2.6.4.tgz (32Bit)
tar -zxvf mongodb-linux-x86_64-2.6.4.tgz

(Copy to target directory - Optional)
mkdir -p mongodb 
cp -R -n mongodb-linux-i686-2.6.4/ mongodb

Configure Search path (Optional)

To ensure that the downloaded binaries are in your PATH, you can modify your PATH and/or create symbolic links to the MongoDB binaries in your /usr/local/bin directory (/usr/local/bin is already in your PATH). You can find the MongoDB binaries in the bin/ directory within the archive.

Running the MongoDB

mkdir -p /data/db
chown mongodb /data/db
Note : You must create the user manually

Starting the MongoDB

mongod or 
mongod --dbpath <some alternate directory>

Ctrl + C to stop the server as usual

No comments:

Post a Comment