Testing MongoDB Replica Pairs with Perl

I was just fixing this, and it’s a pain to setup/test, so I figured I’d put up what I did here, so at least I’d never have to figure it out again.  So, start two db servers and an arbiter:

$ mkdir ~/data1
$ mkdir ~/data2
$
$ ./mongod --pairwith localhost:27018 --arbiter localhost:27019 --dbpath ~/data1
$ ./mongod --port 27018 --pairwith localhost:27017 --arbiter localhost:27019 --dbpath ~/data2
$ ./mongod --port 27019 #arbiter

Then kick off the following Perl script:

use strict;
use warnings;
use Carp;
use Data::Dumper;

use MongoDB;

# order of left_host/right_host is unimportant
my $conn = MongoDB::Connection->new('right_host' => 'localhost', 'left_host' => 'localhost', 'left_port' => 27018);

my $db = $conn->get_database('x');
my $coll = $db->get_collection('y');

$coll->drop;

for (my $i=0; $ifind_one;
    };

    print $@."n";

    sleep 1;
}

Now, you’ll start getting stuff like:

finding...
finding...
finding...

That means everything is okay. Now, kill the first mongod process that you started. (The first one you started is probably master. If you don’t see a “pair: setting master=1 was 0” on the other one after a couple seconds, restart the first one and kill the second one.) You’ll notice your output changes to:

finding...couldn't find master at /usr/local/lib/perl5/site_perl/5.10.0/i686-linux/MongoDB/Connection.pm line 177.
finding...couldn't find master at /usr/local/lib/perl5/site_perl/5.10.0/i686-linux/MongoDB/Connection.pm line 177.
finding...couldn't find master at /usr/local/lib/perl5/site_perl/5.10.0/i686-linux/MongoDB/Connection.pm line 177.

The slave hasn’t become master yet, so there’s nothing the db can use (there will probably be a 2-10 second delay before the slave becomes master). Once you see “pair: setting master=1 was 0” scroll by on your db logs, you’ll notice the Perl script’s output change back to:

finding...
finding...
finding...

You can go back and forth, killing off master dbs and restarting them.

OSCON

I’m at OSCON, which is really fun (and exhausting).

Last night, when my company’s venture capitalist found out I had MongoDB stickers, he asked me to put them on the tables scattered around the conference area, where everyone sat to talk and surf the web.

For some reason, doing so made me feel like a total tool, but this morning, I sucked it up and put a little pile on each table.  As I was walking away from one table, a guy sitting there loudly said, “I bet she doesn’t even know what ‘Mongo’ is from.”  Jerk.  What geek isn’t a Mel Brooks fan?  Even if I hadn’t seen Blazing Saddles, I work on the freakin project.

When I put the last pile down on the table at which I was eating breakfast, the guy sitting next to me immediately picked one up and stuck it on his laptop.

“Do you use MongoDB?” I asked, surprised at the chances, but gratified.

“No, I’ve never heard of it, but it’s a lot nicer than the HP logo.”

So, good job Jason!  (The graphic designer for MongoDB.)  I mean, not only with the spiffy business cards and stickers, but he actually made our Confluence wiki pretty, which is no mean feat.

Got Mongo Working on Hostmonster!

This was written in April of 2009. It is very out of date. See http://rcrisman.net/article/11/installing-mongodb-on-hostmonster-bluehost-accounts for more up-to-date information (as of August 2010). Keep in mind that shared hosting with Hostmonster is very lame. They only lets you run a program for 5 minutes before killing it, so it’s fairly useless to install MongoDB unless you have a dedicated IP.

I finally got MongoDB working on this site, so I’m going to start switching stuff over from MySQL. I’m biased, but I think it’s just an easier database to use.

And, because I like writing tutorials… How I did it:

  1. Downloaded the binary I created of MongoDB for “legacy” Linux. I originally compiled this for a user on Mandriva 2006 (see previous post about VMWare), but it works fine for other old Linux distros, too.
  2. Run:
    $ tar zxvf mongodb-linux-i686-old-linux-1.tgz
  3. Make a directory for the database to put files in:
    $ mkdir /home/user/data
  4. Upload libjava.so, libjvm.so, and libverify.so. Make sure they have execute permissions and put them somewhere like /home/user/lib.
  5. Run:
    $ export LD_LIBRARY_PATH=/home/user/lib

    replacing the path wherever you put the .so’s above.

  6. Start the database:
    $ cd mongodb-linux-i686-old-linux-1
    $ bin/mongod --dbpath /home/user/data --nojni run
            

I cheated a bit and didn’t install Java, so I had to use the –nojni option. If you install Java, you won’t need that (and you won’t need to upload the individual .so files).

Now, what good is a database if you can’t use it, right? So, I downloaded my PHP driver (go to its Github repository and click “Download” for the latest version). I then followed the install instructions and put the .so generated by make in /home/user/extensions.

I changed the options under “PHP Config” in Hostmonster’s CPanel to use php.ini in /home/user/public_html/php.ini, and then edited that file to use my extension.

I made a simple test page with:


Which connected me to MongoDB, showing:

localhost:27017

when I loaded the page!