Skip to main content

Connecting to MongoLab - perl and python

[update 2011.10.13] I thought I would add the following quote from the MongoLab support pages: "If you connect to your database from outside EC2 or Rackspace your data is less secure. While your database does require username / passord authentication, you are potentially vulnerable to others "sniffing" your traffic. We are currently exploring ways to provide for more secure methods of connecting to MongoLab databases from outside the cloud."

I'm working on a mojolicious project as currently mentioned on this site. The next logical step for the application is a connection to the DB. I was originally going to deploy a mongoDB instance on my own server... and that would be great. But I've decided to use MongoLabs instead.  I suppose I could also use MongoHQ and try them independently and for comparison. That's a story for another day.

Connecting to MondoLab was pretty simple:

  • create an account

  • create a database

  • create a collection

  • create a user for interacting with the collection

That's it.  The nice thing is that MongoLab gives you the mongo-cli interface sample so you know exactly what's what. From here you can test the connection on your PC if you have the mongoDB client installed.

From outside of Rackspace:
mongo -u <username> -p <password>
From within Rackspace:
mongo -u <username> -p <password>

Looking at the CPAN help for MongoDB (previously installed in mojolicious part 1) So now we have to test the connection with this sample code (I got it from the CPAN and then made some corrections).
use MongoDB;

my $connection = MongoDB::Connection->new(host => 'mongodb://');
$connection->authenticate('mydb', 'username', 'password');
my $database = $connection->mydb;
my $collection = $database->get_collection(my_collection);
my $id = $collection->insert({ some => 'data' });
my $data = $collection->find_one({ _id => $id });

Once I executed the program I verified that the data was written to the DB by logging into the webGUI and checking the collection. The data was there and ready.

Then I took this program and made a few modifications so that I could dump the record I just inserted. The code looks like this:
use MongoDB;

my $connection = MongoDB::Connection->new(host => 'mongodb://');
$connection->authenticate('mydb', 'username', 'password');
my $database = $connection->mydb;
my $collection = $database->get_collection(my_collection);
my $data = $collection->find_one();
while (($key, $value) = each(%$data)){
print $key.", ".$value."<br />";

It's not a very sophisticated dumper and there are some good libs for that sort of thing, however, my mission was to dump the data and so I did.
I'd like to take a sidebar moment to mention that I recently read an article "why perl". The take away from the article was that perl programmers are 'A' and that perl programs are 'B'. Granted there is no real evidence of this, however, there is a corollary. If you want to hire smart people who take an interest in their craft and you do not want to go through throngs of java resumes post an erlang position. So what I'm saying is that perl is a edge language where python, ruby, javascript, java are in the median space and therefore "mostly" attracting median skilled programmers.(let the trolling begin)

So I implemented the same exact program (to pull the data from the DB) in python. It took half the time because I was already familiar with the syntax etc... as there is some nuance in perl that I've flushed from my cache I wanted to make a connection to my my python side.
from pymongo import Connection
connection = Connection('',27999)
db = connection.mydb
collection = db.mycollection
print collection.find_one()

Worked like a charm. The output was prettier because python makes that easy. I'm looking forward to part 3. In the meantime I'm going to try this against mongoHQ.


Popular posts from this blog

Entry level cost for CoreOS+Tectonic

CoreOS and Tectonic start their pricing at 10 servers. Managed CoreOS starts at $1000 per month for those first 10 servers and Tectonic is $5000 for the same 10 servers. Annualized that is $85K or at least one employee depending on your market. As a single employee company I'd rather hire the employee. Specially since I only have 3 servers.

The pricing is biased toward the largest servers with the largest capacities; my dual core 32GB i5 IntelNuc can never be mistaken for a 96-CPU dual or quad core DELL

If CoreOS does not figure out a different barrier of entry they are going to follow the Borland path to obscurity.

UPDATE 2017-10-30: With gratitude the CoreOS team has provided updated information on their pricing, however, I stand by my conclusion that the effective cost is lower when you deploy monster machines. The cost per node of my 1 CPU Intel NUC is the same as a 96 CPU server when you get beyond 10 nodes. I'll also reiterate that while my pricing notes are not currently…

eGalax touch on default Ubuntu 14.04.2 LTS

I have not had success with the touch drivers as yet.  The touch works and evtest also seems to report events, however, I have noticed that the button click is not working and no matter what I do xinput refuses to configure the buttons correctly.  When I downgraded to ubuntu 10.04 LTS everything sort of worked... there must have been something in the kermel as 10.04 was in the 2.6 kernel and 4.04 is in the 3.x branch.

One thing ... all of the documentation pointed to the wrong website or one in Taiwanese. I was finally able to locate the drivers again: (it would have been nice if they provided the install instructions in text rather than PDF)
Please open the document "EETI_eGTouch_Programming_Guide" under the Guide directory, and follow the Guidline to install driver.
download the appropriate versionunzip the fileread the programming manual And from that I'm distilling to the following: execute the answer all of the questio…

Prometheus vs Bosun

In conclusion... while Bosun(B) is still not the ideal monitoring system neither is Prometheus(P).


I am running Bosun in a Docker container hosted on CoreOS. Fleet service/unit files keep it running. However in once case I have experienced at least one severe crash as a result of a disk full condition. That it is implemented as part golang, java and python is an annoyance. The MIT license is about the only good thing.

I am trying to integrate Prometheus into my pipeline but losing steam fast. The Prometheus design seems to desire that you integrate your own cache inside your application and then allow the server to scrape the data, however, if the interval between scrapes is shorter than the longest transient session of your application then you need a gateway. A place to shuttle your data that will be a little more persistent.

(1) storing the data in my application might get me started more quickly
(2) getting the server to pull the data might be more secure
(3) using a push g…