|
Setting Up MongoDB on Linux
Russell Bateman |
MongoDB may be easily installed on Ubuntu. Do Applications > Ubuntu Software Center and search for "mongodo". Click on "An object/document-oriented database (metapackage) (mongodb)". What will be installed are packages:
I found that I could use Mongo installed this way with the shell, but it would not give a connection to my application. Moral of the story: never trust what Ubuntu Software Center has in its repository. The version of Mongo installable using Ubuntu Software Center is unusable. It can't be connected to from JDBC, Django, etc.
If you find something installed and wish to remove it, do this:
Then I simply erased the following subdirectories:russ@tuonela:~> dpkg -l | grep mongodb rc mongodb-server 1:1.4.4-2ubuntu2 An object/document-oriented database (server package) russ@tuonela:~> sudo dpkg --purge mongodb-server [sudo] password for russ: (Reading database ... 273127 files and directories currently installed.) Removing mongodb-server ... Purging configuration files for mongodb-server ... dpkg: warning: while removing mongodb-server, directory '/var/lib/mongodb' not empty so not removed. dpkg: warning: while removing mongodb-server, directory '/var/log/mongodb' not empty so not removed. Processing triggers for ureadahead ... ureadahead will be reprofiled on next reboot
MongoDB's own packages are generally fresher than those included in the Ubuntu repositories. To install on Ubuntu server, Ubuntu desktop (ignoring Ubuntu Software Center), Debian or other platforms supporting Debian apt (advance package tool), first check to see if it's not already there:
$ dpkg -l | grep mongodb
If not, or if you'd like the latest, go download it from MongoDB downloads as a simple tarball.
In this way, you can get a private copy of MongoDB to run without installing it on your host (or so I think; I haven't tried not installing it). There is a bin subdirectory.
However, to hard-install Mongo on your host, please follow the steps in the next section.
This is done from links at the bottom of the downloads page for various, supported platforms. Because I've been an Ubuntu guy for a few years now, I'm concerned here with the Ubuntu and Debian packages. There are some complex details you'll have to sort out for yourself such as setting up the GNU privacy guard (GPG) key. These details are explained on that page, but there are problems with how they're explained; hence, the write-up below.
Note that after this step, your host will be running Mongo DB.
The page at MongoDB's web site explaining how to install everything (as compared just to downloading a private copy) gives misleading when not erroneous counsel.
root@tuonela:~# apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10 Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg \ --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg \ --keyserver keyserver.ubuntu.com --recv 7F0CEB10 gpg: requesting key 7F0CEB10 from hkp server keyserver.ubuntu.com gpg: key 7F0CEB10: public key "Richard Kreuter[email protected]>" imported gpg: no ultimately trusted keys found gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
If this step hangs then times out, you may be needing to go through a proxy. You'll probably need to set this up in your root user, then try the apt-key adv command again.
root@tuonela:~# env | grep http root@tuonela:~# export http_proxy=http://web-proxy.acme.com:8080 root@tuonela:~# export https_proxy=https://web-proxy.acme.com:8080
Next, add these lines to the bottom of /etc/apt/sources.list. Do not attempt to execute them on the command line; deb is not a Linux command. You'll see deb-src already paired in this file to other line entries. I have it here uncommented because it's not needed and caused the update to fail when I tried it.
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen # deb-src http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
Then, update apt ensuring that there is some MongoDB-related output as the latter's repository is added for apt to use.
root@tuonela:~# apt-get update | grep mongo Ign http://downloads-distro.mongodb.org dist InRelease Hit http://downloads-distro.mongodb.org dist Release.gpg Hit http://downloads-distro.mongodb.org dist Release Get:1 http://downloads-distro.mongodb.org dist/10gen i386 Packages [785 B] Ign http://downloads-distro.mongodb.org dist/10gen TranslationIndex Ign http://downloads-distro.mongodb.org dist/10gen Translation-en_US Ign http://downloads-distro.mongodb.org dist/10gen Translation-en
After that success, the last step is the installation.
root@tuonela:~# apt-get install mongodb-10gen Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: mongodb-10gen 0 upgraded, 1 newly installed, 0 to remove and 21 not upgraded. Need to get 26.9 MB of archives. After this operation, 68.2 MB of additional disk space will be used. Get:1 http://downloads-distro.mongodb.org/repo/ubuntu-upstart/ dist/10gen mongodb-10gen i386 1.8.1 [26.9 MB] Fetched 26.9 MB in 32s (830 kB/s) Selecting previously deselected package mongodb-10gen. (Reading database ... 51141 files and directories currently installed.) Unpacking mongodb-10gen (from .../mongodb-10gen_1.8.1_i386.deb) ... Processing triggers for man-db ... Processing triggers for ureadahead ... ureadahead will be reprofiled on next reboot Setting up mongodb-10gen (1.8.1) ... Adding system user `mongodb' (UID 106) ... Adding new user `mongodb' (UID 106) with group `nogroup' ... Not creating home directory `/home/mongodb'. Adding group `mongodb' (GID 115) ... Done. Adding user `mongodb' to group `mongodb' ... Adding user mongodb to group mongodb Done. mongodb start/running, process 5644
The Mongo DB service is now running.
To get started (this is all at the "learning" link below), you'll need to create and place for Mongo databases to live on your platform. For Unix and Linux this is as below. You need privileges too. You can give these to everyone if you like.
root@tuonela:~# mkdir -p data/db root@tuonela:~# chmod -R a+rw data root@tuonela:~# ll data total 1 drwxrwxrwx 3 root root 72 2011-06-28 10:47 . drwxr-xr-x 25 root root 728 2011-06-28 10:47 .. drwxrwxrwx 2 root root 48 2011-06-28 10:47 db
To ensure that all is set up correctly, browse to http://localhost:28017/. There you'll find an administrative page with the title, mongod hostname with a great deal of information on it including the contents of the log (from /var/log/mongodb.log). You'll need to refresh to see new additions. For example, if you were running the Mongo shell, exited, then launched it again, then refresh the admin page, you'll see two more lines at the end of the log:
11:09:33 [conn4] end connection 127.0.0.1:46841 11:09:38 [initandlisten] connection accepted from 127.0.0.1:41840 #5
Check out the MongDB website for a tutorial to get started. You'll use Mongo's shell at first. It's important to understand the concept. There are also high-powered GUI environments with which to access MongoDB. Later, you'll consume Mongo from your application.
You can also find the Java driver JAR and documentation for same on the downloads page.