1

I've added the MongoLab add-on to a Heroku instance, but my app won't connect to the MongoDB host. I've seen this thread, but I didn't see anything helpful.

twitter_clone_python.py

f = Flask(__name__)

# 'TheFuture' is the name of my laptop. It signifies that we are in
# development mode.
if socket.gethostname().startswith('TheFuture'):
    f.config.from_object('config.DevelopmentConfig')
# If 'TheFuture' isn't the host, then we are in production.
else:
    f.config.from_object('config.ProductionConfig')

# MongoDB connection
connection = Connection(f.config['MONGODB_HOST'], f.config['MONGODB_PORT'])
db = connection['MONGODB_DB']

# Try authenticating. This will only work in production. In development,
# MONGODB_USER and MONGODB_PASSWORD will raise KeyErrors.
try:
    db.authenticate(f.config['MONGODB_USER'], f.config['MONGODB_PASSWORD'])
except KeyError:
    pass

config.py

class Config(object):
    '''Default configuration object.'''
    DEBUG = False
    TESTING = False
    PORT = int(os.environ.get('PORT', 5000))

class ProductionConfig(Config):
    '''Configuration object specific to production environments.'''
    REDIS_URL = os.environ.get('REDISTOGO_URL')
    if REDIS_URL:
        url = urlparse.urlparse(REDIS_URL)
        REDIS_HOST = url.hostname
        REDIS_PORT = url.port
        REDIS_PASSWORD = url.password

    MONGOLAB_URI = os.environ.get('MONGOLAB_URI')
    if MONGOLAB_URI:
        url = urlparse.urlparse(MONGOLAB_URI)
        MONGODB_USER = url.username
        MONGODB_PASSWORD = url.password
        MONGODB_HOST = url.hostname
        MONGODB_PORT = url.port
        MONGODB_DB = url.path[1:]

class DevelopmentConfig(Config):
    '''Configuration object specific to development environments.'''
    DEBUG = True

    MONGODB_HOST = 'localhost'
    MONGODB_PORT = 27017
    MONGODB_DB = 'twitter-clone-python-development'

Running heroku logs shows the error:

2012-09-28T13:57:25+00:00 app[web.1]: pymongo.errors.AutoReconnect: could not connect to ds037997.mongolab.com:27017: timed out
Community
  • 1
  • 1
  • Could you send us an email at support@mongolab.com? There might be an issue with your account that we can address quickly. Thanks, -Robert@MongoLab – robert Sep 28 '12 at 16:04
  • I just sent the email. I forgot to include my account details though, so here is my `MONGO_URI` config variable from Heroku: `mongodb://heroku_app7969952:***.mongolab.com/heroku_app7969952`. –  Sep 28 '12 at 16:20

0 Answers0