PyMongo - ConnectionFailure

时间:2015-07-28 22:57:51

标签: python mongodb python-2.7 nginx

I want to connect to my mongoDB using pymongo. But I have this error :

raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: [Errno -2] Name or service not known

I have obviously checked url,password...etc of the database connection. My mongoDb instance is running.

sudo service mongodb status
mongodb start/running, process 10199

Thanks!

(Log)

Traceback (most recent call last):
  File "app.py", line 173, in <module>
    run_app(opts.config)
  File "app.py", line 159, in run_app
    Application(config).start(config)
  File "app.py", line 75, in __init__
    conn = pymongo.Connection(config.dbhost)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line 236, in __init__
    max_pool_size, document_class, tz_aware, _connect, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 369, in __init__

1 个答案:

答案 0 :(得分:-1)

使用MongoClient构造函数。

假设环回的默认端口,数据库为my_db而您的集合为my_coll,请尝试:

from pymongo import MongoClient
my_connection = MongoClient('mongodb://localhost:27017')
db = my_connection['my_db']
coll = db['my_coll']
#Sample cursor
my_cur = coll.find()