Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to connect using multiple nodes #855

Closed
ghost opened this issue Apr 19, 2016 · 14 comments
Closed

How to connect using multiple nodes #855

ghost opened this issue Apr 19, 2016 · 14 comments

Comments

@ghost
Copy link

ghost commented Apr 19, 2016

Hi, thank you for this awesome project!

I am having trouble trying to connect to a Mongo 3.2.1 database which uses this string, provided by Compose.io:

mongodb://<user>:<password>@<host1>:<port1>,<host2>:<port2>/<db-name>?ssl=true

I tried using MONGO_URI in the configuration dictionary but it tries to connect to a local server:

/vagrant/env/local/lib/python2.7/site-packages/pymongo/topology.pyc in select_servers(self, selector, server_selection_timeout, address)
    116                 if server_timeout == 0 or now > end_time:
    117                     raise ServerSelectionTimeoutError(
--> 118                         self._error_message(selector))
    119 
    120                 self._ensure_opened()

ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

Not sure how can I set my connection string, currently I am using just one node using these variables:

MONGO_HOST
MONGO_PORT
MONGO_DBNAME
MONGO_USERNAME
MONGO_PASSWORD

But I need to be able to use both to handle failover between nodes.

Any idea?

@nicolaiarocci
Copy link
Member

The connection string is simply passed to pymongo. Could you try the pymongo uri parser and see if your string is parsed correctly?

@ghost
Copy link
Author

ghost commented Apr 20, 2016

Hi, it seems is parsed correctly (replaced actual values):

{'collection': None,
'database': 'something',
'nodelist': [('host1', 10520),
('host2', 10208)],
'options': {'ssl': True},
'password': 'secret',
'username': 'user'}

What can I do?

@nicolaiarocci
Copy link
Member

Could you could try setting up a little python script which uses pymongo and your uri, bypassing Eve?

@ghost
Copy link
Author

ghost commented Apr 20, 2016

Good idea, I wrote this script and it worked (I needed ssl_ca_certs to make it work):

import ssl
from pymongo import MongoClient

uri = 'mongodb://user:password@host1:port1,host2:port2/database?ssl=true&ssl_ca_certs=cert.pem'
client = MongoClient(uri)
db = client['database']
collection = db['collection']
for c in collection.find():
    print c

I tried to use the same connection string in Eve using MONGO_URI option but it failed again, trying to connect to localhost instead of remote MongoDB.
ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

How can I pass these options to Eve?

@nicolaiarocci
Copy link
Member

nicolaiarocci commented Apr 21, 2016

Eve relies on Flask-PyMongo. Now, according to this ticket FPM does not support ssl and/or cert ca as parameters. I suspect that's the issue with your connection string.

You might want do the same experiment with Flask-PyMongo instead of barebone PyMongo, and see if it works on your setup. If the issue is with FPM, we will have to wait until it is fixed upstream.

@ghost
Copy link
Author

ghost commented Apr 21, 2016

I did this test and it worked:

from flask import Flask
from flask.ext.pymongo import PyMongo
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://user:password@host1:port1,host2:port2/database?ssl=true&ssl_ca_certs=cert.pem"
mongo = PyMongo(app)

with app.app_context():
    for c in mongo.db.collection.find():
        print c

So it seems Flask-PyMongo actually supports this kind of connections.

@ghost
Copy link
Author

ghost commented Apr 25, 2016

Any idea on how can we fix this on Eve? Thanks!

@nicolaiarocci
Copy link
Member

Hi, I just merged #990 with a fix for MONGO_URI. Maybe it helps with your issue?

@ghost
Copy link
Author

ghost commented Mar 13, 2017

Hi, I need to test again, as I am currently using an older version of Eve.

@tyomo4ka
Copy link
Contributor

@nicolaiarocci @gustavo-gomez

It seems the problem is in Flask-PyMongo. I tried the following:

  1. Create a cluster using Mongo Atlas.
  2. Put the URI provided by Atlas into Eve configuration MONGO_URI field – it doesn't work.
  3. Create a separate Python script with Flask-PyMongo – it doesn't work as well.
  4. Create a separate Python script with just pymongo – it connects and shows my documents.

I played a little with Flask-PyMongo and it seems they parse MONGO_URI and then assemble it again instead of just bypassing to pymongo. Don't know why. Doesn't seem to me like a good idea.

@tyomo4ka
Copy link
Contributor

@nicolaiarocci

The Flask-PyMongo seems to be abandoned. 0 contributions in 2017.

@nicolaiarocci
Copy link
Member

Flask-PyMongo dependency was just dropped on branch v0.8.

@carlwgeorge
Copy link
Contributor

FYI, the v0.8 tag still requires flask-pymongo in setup.py.

@nicolaiarocci
Copy link
Member

Yes, good catch. It is fixed in v0.8.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants