Wednesday, June 19, 2013

Changing the defalult dbpath in mongoDB & Use existing database in newer version of mongo server

I was working in mongoDB 2.2,  Recently installed mongoDB 2.4 & it's default location is /var/lib/mongodb.

I was used /data/db as a default path in mongoDB 2.2, after I installed 2.4, I thought of using existing database which I created using 2.2.

Instead of backup & restore or import & export  I just change the dbpath in /etc/mongodb.conf.

here simple step to re-configuring the default dbpath and

1) stop the mongoDB server
root@boss[bin]#/etc/init.d/mongodb stop 
 Stopping database: mongodb.

 2) Open a file & change dbpath to desired location
root@boss[bin]#vi /etc/mongodb.conf
.....
.....
#dbpath=/var/lib/mongodb
dbpath=/data/db
.....
.....

3) restart the mongoDB
root@boss[bin]#/etc/init.d/mongodb strat 
root@boss[bin]#tail -f /var/log/mongodb/mongodb.log  
***** SERVER RESTARTED *****

Tue Jun 18 15:32:41.170 [initandlisten] MongoDB starting : pid=20270 port=27017 dbpath=/data/db 32-bit host=boss
Tue Jun 18 15:32:41.170 [initandlisten] 
Tue Jun 18 15:32:41.170 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Tue Jun 18 15:32:41.170 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Tue Jun 18 15:32:41.170 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Tue Jun 18 15:32:41.170 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Tue Jun 18 15:32:41.170 [initandlisten] 
Tue Jun 18 15:32:41.170 [initandlisten] db version v2.4.4
Tue Jun 18 15:32:41.170 [initandlisten] git version: 4ec1fb96702c9d4c57b1e06dd34eb73a16e407d2
Tue Jun 18 15:32:41.170 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Tue Jun 18 15:32:41.170 [initandlisten] allocator: system
Tue Jun 18 15:32:41.170 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/data/db", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" }
Tue Jun 18 15:32:41.170 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Tue Jun 18 15:32:41.170 dbexit: 
Tue Jun 18 15:32:41.170 [initandlisten] shutdown: going to close listening sockets...
Tue Jun 18 15:32:41.170 [initandlisten] shutdown: going to flush diaglog...
Tue Jun 18 15:32:41.170 [initandlisten] shutdown: going to close sockets...
Tue Jun 18 15:32:41.170 [initandlisten] shutdown: waiting for fs preallocator...
Tue Jun 18 15:32:41.170 [initandlisten] shutdown: closing all files...
Tue Jun 18 15:32:41.171 [initandlisten] closeAllFiles() finished
Tue Jun 18 15:32:41.171 [initandlisten] shutdown: removing fs lock...
Tue Jun 18 15:32:41.171 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Tue Jun 18 15:32:41.171 dbexit: really exiting now
its failed to start, have checked log file in (/var/log/mongodb/mongodb.log)got above error.

then I changed the owner & permission of /data/db as like mongoDB 2.4 default path /var/lib/mongodb/ 

4) get the owner & access details
root@boss[bin]#ls -l /var/lib/mongodb/
total 32808
-rw------- 1 mongodb nogroup 16777216 Jun 18 15:33 local.0
-rw------- 1 mongodb nogroup 16777216 Jun 18 15:33 local.ns
-rwxr-xr-x 1 mongodb mongodb        0 Jun 18 15:40 mongod.lock

 5)change the owner in new path (i.e /data/db )
root@boss[bin]#chown -R mongodb:nogroup /data/db
root@boss[bin]#chown mongodb:mongodb /data/db/mongod.lock
then I restart the mongodb by /etc/init.d/mongo start ( step 3 ). this time server started successfully.
starting  mongoDB

root@boss[bin]#/etc/init.d/mongodb strat 
 Starting database: mongodb. 

6) Running mongo
root@boss[bin]#mongo 
MongoDB shell version: 2.4.4
connecting to: test
> db 
test
>show dbs 
admin 0.0625GB
local 0.03125GB
rs 0.0625GB
snps1 0.0625GB
test 0.0625GB
week4 0.0625GB
week5 0.0625GB
week6 0.999755859375GB

by using mongo2.4 can able to access the all database which was created using mongo2.2.

without dump & restore / import & export the old database created using mongoDB 2.2 can be readily accessible by newer version of mongodb 2.4 by simply changing / configuring  the dbpath attribute in /etc/mongodb.conf

7 comments:

shobana said...

it was working fine first time, after few restart of the mongodb server, "failed to start server". In log "Thu Jul 25 09:51:40.903 [initandlisten] exception in initAndListen: 12596 old lock file, terminating "

dataanalytics said...

1) vi /data/db/mongod.lock
/data/db => dbpath
2) Delete the file,make size as zero
3) Save & close the file

now
start the mongodb

shobana said...

ya its working, Thanks

Taylor said...

getting error even after remoing the content as u said eralier.
exception in initAndListen: 12596 old lock file, terminating


dataanalytics said...

remove the lock file

rm dbpath/mongod.lock

and/or

rm /var/lib/mongodb/mongod.lock
Note : if you are using ubuntu/debian/BOSS location of the lock file is /var/lib/mongodb/ else find the name mongod.lock and remove

Dream Chaser said...

I removed both the lock files at both the instances i.e. /var/lib/mongodb and /data/db.
But, I am still facing the same issue. Please help

dataanalytics said...

Share your log.. Will try to help u out