简体   繁体   中英

OpenShift Python mongoDB environment variables not set / can't connect

This is in my application file head:

import os
import sys
from cgi import parse_qs, escape
import pymongo
from pymongo import MongoClient

I have the mongoDB 2.4 gear installed, and am trying to connect via

client = MongoClient('mongodb:$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/')

I get the errors:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/lib/openshift/531b77fd500446980900010d/python/virtenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 239, in __init__
    res = uri_parser.parse_uri(entity, port)
  File "/var/lib/openshift/531b77fd500446980900010d/python/virtenv/lib/python2.7/site-packages/pymongo/uri_parser.py", line 269, in parse_uri
    nodes = split_hosts(hosts, default_port=default_port)
  File "/var/lib/openshift/531b77fd500446980900010d/python/virtenv/lib/python2.7/site-packages/pymongo/uri_parser.py", line 209, in split_hosts
    nodes.append(parse_host(entity, port))
  File "/var/lib/openshift/531b77fd500446980900010d/python/virtenv/lib/python2.7/site-packages/pymongo/uri_parser.py", line 137, in parse_host
    raise ConfigurationError("Port number must be an integer.")
pymongo.errors.ConfigurationError: Port number must be an integer.

looks like OPENSHIFT_MONGODB_DB_PORT isn't set

print  OPENSHIFT_MONGODB_DB_PORT --> NameError: name 'OPENSHIFT_MONGODB_DB_PORT' is not defined

Same with OPENSHIFT_MONGODB_DB_HOST

What would I need to do, to set up a connection?

Update:

I was able to connect directly via client by hardcoding info from rockmongo

client = MongoClient('mongodb://admin:password@[ip addr]:[port]/') 

but when I do

client = MongoClient('mongodb:admin:password@%s:%s/' %  os.environ['OPENSHIFT_MONGODB_DB_HOST'], os.environ['OPENSHIFT_MONGODB_DB_PORT'])) 

I get

[error] (<type 'exceptions.KeyError'>, KeyError('OPENSHIFT_MONGODB_DB_HOST',), <traceback object at 0x7f7bc8367248>)

The OpenShift connection variables are defined as environment variables, they cannot be accessed as normal Python variables. So the print statement you gave does not work, the following should;

import os
print os.environ['OPENSHIFT_MONGODB_DB_PORT']

You should change your code to;

client = MongoClient('mongodb:%s:%s/' % (os.environ['OPENSHIFT_MONGODB_DB_HOST'], os.environ['OPENSHIFT_MONGODB_DB_PORT))

You can refer to an example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM