MongoDB

From GeilThings

Jump to: navigation, search
MongoDB
Logo-mongodb-onwhite.png
General
Version 2.4.4
Stable 2.4.5
Function Database
SQL NoSQL
Port 27017
Data File /var/lib/mongodb/*
Log File /var/log/mongodb.log
Scripts
Version Script mongo --version
Start mongod --dbpath /var/lib/mongodb/ --fork --auth --logpath /var/log/mongodb.log --logappend --oplogSize 8 --noprealloc --smallfiles --nssize 8
Cli mongo

Contents

     Mongodb cup.jpg

General

Installation

For all 64-bit RPM-based distros with yum, put the next lines in /etc/yum.repos.d/10gen.repo:

[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
yum install mongo-10gen mongo-10gen-server
 
mkdir /var/lib/mongodb
 
# I run mongo manually.
chkconfig --level 345 mongod off

Configuration

  • Start mongod with the option --oplogSize set to a small size; oplogSize is the custom size for replication operation log; e.g. --oplogSize 10 (otherwise, as of October 2011, mongod takes 5% of disk space for 64 bit installations, see here).
  • Start mongod with the option --noprealloc to avoid automatic preallocation of (huge) disk space for databases.
  • As of the 1.8.2 or 2.0.1 versions, restarting the service mongod with --noprealloc and a small --oplogSize will not reduce the size of the (existing) databases. Starting mongod with the option --repair also did not reduce the size of the databases. Only connecting to the mongodb cli and using "use databasename" followed by "db.repairDatabase()" deleted the file databasename.1 (> 130 MB) but the file databasename.0 remained intact (67 MB).
  • The option --smallfiles does reduce the starting size of a database to 16 MB.
  • The option --nssize (in MBs) sets the size of the namespaces.
  • Comments about using MongoDB with smaller databases: http://groups.google.com/group/mongodb-user/browse_thread/thread/b0668b1799da824d
  • Common commands:
> use test
> show collections
> show dbs
admin   (empty)
local   (empty)
test    (empty)
> show users
> use test
switched to db test
> db.createCollection("HelloWorld", {capped:false, size:100000})
{ "ok" : 1 }
> db.HelloWorld.find();
> db.HelloWorld.save({language : "PHP", message_text : "Hello World from PHP and MongoDB"});
> use admin
switched to db admin
> db.addUser("adminuser", "adminpassword")
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
{
        "user" : "adminuser",
        "readOnly" : false,
        "pwd" : "nadiesabeloquehayaqui234234",
        "_id" : ObjectId("4f4e62741f15cba52d398a28")
}
 
> db.auth("adminuser", "adminpassword")
1
> use test
switched to db test
> db.addUser("normaluser", "normaluserpassword", true)
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
{
        "user" : "normaluser",
        "readOnly" : true,
        "pwd" : "unalonganizadeletrasynumeros3453453",
        "_id" : ObjectId("4f4e62a81f15cba52d398a29")
}
> db.system.users.find()
{ "_id" : ObjectId("4f4e62a81f15cba52d398a29"), "user" : "normaluser", "readOnly" : true, "pwd" : "unalonganizadeletrasynumeros3453453" }
> use admin
> db.shutdownServer()
Wed Feb 29 18:39:29 DBClientCursor::init call() failed
Wed Feb 29 18:39:29 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1
server should be down...
Wed Feb 29 18:39:29 trying reconnect to 127.0.0.1
Wed Feb 29 18:39:29 reconnect 127.0.0.1 ok
Wed Feb 29 18:39:29 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
Wed Feb 29 18:39:29 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [1] server [127.0.0.1:27017]
Wed Feb 29 18:39:29 DBClientCursor::init call() failed
Wed Feb 29 18:39:29 query failed : admin.$cmd { getlasterror: 1.0, w: 1.0 } to: 127.0.0.1
Wed Feb 29 18:39:29 Error: error doing query: failed shell/collection.js:151
> quit()

Running MongoDB

  • Even if after starting the server (mongod) there is a Warning: ** WARNING: You are running in OpenVZ. This is known to be broken!!!, it does work in this minimal setting under CentOS 5.6 or CentOS 5.7.
# Cli.
mongo
 
use admin
db.auth("adminuser", "adminuserpassword")
 
# Create database: Use it, the database is created after data is added to it.
use <database_name>
 
# Delete database.
use <database_name>
db.dropDatabase()
 
# Create collection. Either create it or use it, it will be created after data is inserted.
 
use test
db.addUser("readeruser", "readerpassword", true)
db.createCollection("HelloWorld", {capped:false, size:100000})
db.HelloWorld.save({language : "PHP", message_text : "Hello World from PHP and MongoDB"});
db.HelloWorld.save({language : "Perl", message_text : "Hello World from Perl and MongoDB"});
db.HelloWorld.save({language : "Java", message_text : "Hello World from Java and MongoDB"});
db.HelloWorld.save({language : "node.js", message_text : "Hello World from node.js and MongoDB"});
db.HelloWorld.save({language : "Scala", message_text : "Hello World from Scala and MongoDB"});
db.HelloWorld.save({language : "Python", message_text : "Hello World from Python and MongoDB"})
db.HelloWorld.save({language : "Ruby", message_text : "Hello World from Ruby and MongoDB"})
 
# Show all records (20) in a collection:
db.<collection_name>.find();
 
# Delete all records in a collection.
db.<collection_name>.remove({});
 
# Delete record with <record_id>=4f8509691beb313ced643210
db.HelloWorld.remove({_id: ObjectId("4f8509691beb313ced643210")});
 
quit();

Backup MongoDB

APPNAME=mongodb
RAWVERSION=`mongo --version`
# MongoDB shell version: 2.0.2
 
# Get Version as Substring.
OLDVERSION=${RAWVERSION:23}
 
# Check.
echo $OLDVERSION
 
DATE=`date +%Y-%m-%d`
 
# The script breaks if variable names are not enclosed with {}
BACKUPDIR=/home/backup/${APPNAME}/${APPNAME}_${OLDVERSION}_${DATE}
 
# Create backup directory if it does not exist.
if [ ! -d "${BACKUPDIR}" ]
then
    mkdir -m 755 -p ${BACKUPDIR}
fi
 
cp -rp /var/lib/mongodb/* ${BACKUPDIR}

Upgrade MongoDB

  • Usually there are upgrading instructions as part of the release notes, e.g.
  1. http://www.mongodb.org/display/DOCS/2.0+Release+Notes#2.0ReleaseNotes-Upgrading
  • Started with MongoDB 1.8.2.


######### START OF EDIT #########
 
APPNAME=mongodb
# If I want to backup, set it to TRUE.
ISBACKUP=TRUE
# BACKUPROOTDIR is only relevant if ISBACKUP=TRUE
BACKUPROOTDIR=/home/backup
DATE=`date +%Y-%m-%d`
 
######### END OF EDIT #########
 
# Check version.
RAWVERSIONSCRIPT=`curl -sd "action=ask&query=[[$APPNAME]]|%3FVersion_Script&format=json" http://www.geilthings.com/api.php` 
DIRTYVERSIONSCRIPT=`echo "$RAWVERSIONSCRIPT" | grep -Po '^.*?\K(?<=Version Script\":\[\").*?(?=\")'`
# Forward slahes will be preceded by Back slashes
CLEANVERSIONSCRIPT=$(echo "$DIRTYVERSIONSCRIPT" |  sed -e 's/\\//g')
RAWOLDVERSION=`$CLEANVERSIONSCRIPT`
OLDVERSION=${RAWOLDVERSION:23}
echo $OLDVERSION
 
RAWNEWVERSION=`curl -sd "action=ask&query=[[$APPNAME]]|%3FStable&format=json" http://www.geilthings.com/api.php`  
NEWVERSION=`echo "$RAWNEWVERSION" | grep -Po '^.*?\K(?<=Stable\":\[\").*?(?=\")'`  
echo $NEWVERSION
 
if [[ ${OLDVERSION} != ${NEWVERSION} ]]; then
  echo ${APPNAME}: installed: ${OLDVERSION}, actual: ${NEWVERSION}
  echo "You could update ${APPNAME} to ${NEWVERSION}"
  echo "Would you like to install ${APPNAME} ${NEWVERSION} y/n?"
  read item
  if [[ "$item" == "y" || "$item" == "Y" ]]; then
    echo "Installing ${APPNAME} ${NEWVERSION}. Follow instructions."
  else
    echo "Exit"
  fi
else
  echo "You don't need to update ${APPNAME}; it is uptodate (${NEWVERSION})"
fi
 
# Stop the server.
mongo
## use admin
## db.auth("adminname", "password")
## db.shutdownServer()
## quit()
 
# Backup.
if [[ ${ISBACKUP} == "TRUE" ]]; then
  # The script breaks if variable names are not enclosed with {}
  BACKUPDIR=${BACKUPROOTDIR}/${APPNAME}/${APPNAME}_${OLDVERSION}_${DATE}
  if [ ! -d "${BACKUPDIR}" ]; then
    mkdir -m 755 -p ${BACKUPDIR}
  fi
  echo $BACKUPDIR
  cp -rp /var/lib/mongodb/* ${BACKUPDIR}
fi
 
yum upgrade mongo-10gen mongo-10gen-server
y
 
# Restart server.
mongod --dbpath /var/lib/mongodb/ --fork --auth --logpath /var/log/mongodb.log --logappend --oplogSize 8 --noprealloc --smallfiles --nssize 8
 
# Check.
ps aux | grep mongo
 
# Check version.
RAWVERSIONSCRIPT=`curl -sd "action=ask&query=[[$APPNAME]]|%3FVersion_Script&format=json" http://www.geilthings.com/api.php` 
DIRTYVERSIONSCRIPT=`echo "$RAWVERSIONSCRIPT" | grep -Po '^.*?\K(?<=Version Script\":\[\").*?(?=\")'`
CLEANVERSIONSCRIPT=$(echo "$DIRTYVERSIONSCRIPT" |  sed -e 's/\\//g')
RAWINSTALLEDVERSION=`$CLEANVERSIONSCRIPT`
INSTALLEDVERSION=${RAWINSTALLEDVERSION:23}
echo $INSTALLEDVERSION
 
# Test applications.

Cloud

MongoDB hosted on the cloud:

Provider Website Wikipedia
MongoHQ mongohq.com
MongoLab mongolab.com
MongoOd mongood.com

Programming MongoDB


Versions

Software name Version number Version date
MongoDB 2.2.2
2.2.3
2.4.0
2.4.1
2.4.3
2.4.4
2.4.5
1 December 2012
2 February 2013
19 March 2013
29 March 2013
27 April 2013
22 June 2013
28 July 2013

Comments

blog comments powered by Disqus