pymongo: Can connect at command line but not cgi script

123 views
Skip to first unread message

Emily

unread,
Apr 15, 2011, 9:10:54 PM4/15/11
to mongodb-user
I've installed pymongo and wrote a little test.cgi to make sure I can
connect to my database. If I run it at the command line with "python
test.cgi," it prints out the exact HTML it should.

If I try to access it at http://whatever.../test.cgi, it fails on the
line "connection = Connection('localhost')" (I also tried "connection
= Connection()") with the error "AutoReconnect: could not find master/
primary." It also says

/home/me/public_html/fff/pymongo/connection.py in
__init__(self=Connection(None, None), host=['localhost'], port=27017,
max_pool_size=10, slave_okay=False, network_timeout=None,
document_class=<type 'dict'>, tz_aware=False, _connect=True)

The port is correct but the location of pymongo at the start of that
line is wrong. Also I don't have any kind of master/slave setup. I
think what's going on is that I've added the actual location of
pymongo to $PYTHONPATH on the command line so it works there but from
the web it isn't looking for pymongo in the right place. I started off
test.cgi with

#!/usr/local/bin/python
import sys
sys.path.append("/home/me/python/mongopython")

But that doesn't seem to have worked. I don't have much Python
experience--am only using it because I can't install the PHP driver on
this machine--do I have a pymongo problem here or a pythonpath
problem? Thanks.

Bernie Hackett

unread,
Apr 16, 2011, 1:54:40 AM4/16/11
to mongodb-user
> AutoReconnect: could not find master/primary

Are you sure that mongod is running (at 'localhost:27017')? That error
means that pymongo couldn't make a connection to mongod.

> Also I don't have any kind of master/slave setup.

If you only have one mongod instance running it is the primary or
master.

> I started off test.cgi with...

I think what you wanted to do was:
import os
os.environ['PYTHONPATH'] = '/home/me/python/mongopython'

You're actually better off just installing pymongo using easy_install
or pip instead of cloning pymongo master from github. That way you
don't have to bother with PYTHONPATH at all.
http://api.mongodb.org/python/1.10.1%2B/installation.html

On Apr 15, 6:10 pm, Emily <emily.morton.ow...@gmail.com> wrote:
> I've installed pymongo and wrote a little test.cgi to make sure I can
> connect to my database. If I run it at the command line with "python
> test.cgi," it prints out the exact HTML it should.
>
> If I try to access it athttp://whatever.../test.cgi, it fails on the

Emily

unread,
Apr 16, 2011, 10:07:28 AM4/16/11
to mongodb-user
Thanks. mongod is running, and on the same machine, so I thought
'localhost' was correct although I'm not sure how to confirm that. As
for the port, I did overlook something last night--when mongod started
up it displayed the ports, but they're so similar I didn't spot that
they were different:

Fri Apr 15 20:28:40 [initandlisten] waiting for connections on port
27017
Fri Apr 15 20:28:40 [websvr] web admin interface listening on port
28017

Thinking that 28017 may be the right one for a webpage, I tried
editing my script to specify "connection = Connection('localhost',
28017)" and that didn't work. I also tried using os.environ as you
suggested, including the place where the rest of python is stored and
where I put pymongo, and it actually failed earlier in the script,
unable to find pymongo.

Since it was so keen to look for connection.py etc. in /home/me/
public_html/fff/pymongo/, I tried copying everything to that location
just to see what would happen, and now I seem to be getting a more
detailed error. In connection.py at line 370, it raises
AutoReconnect("could not find master/primary"). Line 369 is "if
_connect", which makes me think it's connecting to *something.*

Unfortunately, I don't seem to be able to use easy_install or pip at
all because it's a shared university machine that has a lot of
permissions roadblocks. I'm only using Python at all after spending
several very frustrating evenings wrangling with the machine trying to
enable PEAR so I could install the PHP driver, and giving up. Getting
this cgi script to run on the command line is by far the closest I've
come to getting a working frontend for this project! :/

On Apr 16, 1:54 am, Bernie Hackett <ber...@10gen.com> wrote:
> > AutoReconnect: could not find master/primary
>
> Are you sure that mongod is running (at 'localhost:27017')? That error
> means that pymongo couldn't make a connection to mongod.
>
> > Also I don't have any kind of master/slave setup.
>
> If you only have one mongod instance running it is the primary or
> master.
>
> > I started off test.cgi with...
>
> I think what you wanted to do was:
> import os
> os.environ['PYTHONPATH'] = '/home/me/python/mongopython'
>
> You're actually better off just installing pymongo using easy_install
> or pip instead of cloning pymongo master from github. That way you
> don't have to bother with PYTHONPATH at all.http://api.mongodb.org/python/1.10.1%2B/installation.html

Eliot Horowitz

unread,
Apr 16, 2011, 12:00:06 PM4/16/11
to mongod...@googlegroups.com
Try using 127.0.0.1:27017 instead of localhost.
Some machines are setup odd.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Emily

unread,
Apr 16, 2011, 8:06:05 PM4/16/11
to mongodb-user
Thanks! One way or another, this seems to have worked and I'm in.

If anyone is still reading this, I could use an explanation of how
ObjectId objects work in Python. I'm trying to do something like a
MySQL join and maybe I haven't set it up right because I can't seem to
retrieve things.

I have Collection 1 that has an entry like this:
{
"_id" : ObjectId("4da1041a0b0334fed8f0b667"),
"books" : [
"4da1041a9fa15ed096b84f2b",
"4da1041a9fa15ed096b84f62"
],
"place" : "Russia"
}

And then Collection 2 has:

{ "_id" : ObjectId("4da1041a9fa15ed096b84f2b"), "title" : "War and
Peace" }
{ "_id" : ObjectId("4da1041a9fa15ed096b84f62"), "title" : "Anna
Karenina" }

And I want to get the titles where the place is Russia. I can get a
string like 4da1041a9fa15ed096b84f2b or I can query the second
collection get the object based on the title, but any attempt to do
something like secondcoll.find_one({"_id":theStringIGotBefore}) either
retrieves "none" because I haven't formatted the query right, or
there's a type mismatch. I can't seem to find any examples of querying
based on id in Python.

I know I could stuff the second collection's documents into the first
collection but that doesn't make sense in this case because, say, Anna
Karenina could also be associated with Germany and I wouldn't want to
insert it in two places.

Here's an example of what's *not* working:

for place in index.find({"name":place}):
for bookid in place['books']:
print("<p>BookID: " + bookid + "</p>") #okay, these are the right
ids
book = dictionary.find_one({"_id":bookid}) #but even when I know
that id exists
print("<p>" + book['title'] + "</p>") #this just prints None

Turan Öztürk

unread,
Apr 16, 2011, 9:01:48 PM4/16/11
to mongod...@googlegroups.com
from
pymongo.objectid import ObjectId
....{"_id":ObjectId(bookid)......

2011/4/17 Emily <emily.mor...@gmail.com>

Turan Öztürk

unread,
Apr 16, 2011, 9:03:10 PM4/16/11
to mongod...@googlegroups.com

from

pymongo.objectid import ObjectId

..{

"_id":ObjectId(bookid)..

2011/4/17 Turan Öztürk <esa...@gmail.com>
Reply all
Reply to author
Forward
0 new messages