17

I am using ipython 2.7. I am creating database name enron in mongodb. I have tried to connect to this database locally but the following error occurred - how do I fix it?

this my code:

import json
import pymongo  # pip install pymongo
from bson import json_util 
from pymongo import MongoClient# Comes with pymongo
conn = pymongo.Connection('mongodb://user:[email protected]:33499/enron')
client = MongoClient()

error:

ConnectionFailure: could not connect to localhost:27017: [Errno 10061] No connection     could be made because the target machine actively refused it

6 Answers 6

21

Below are commands to create connection and query

from pymongo import MongoClient
client = MongoClient('hostname', 27017)
db = client.database_name
collection = db.collection_name
collection.find_one({"name":"name1"})
Sign up to request clarification or add additional context in comments.

Comments

6
from pymongo import MongoClient
from bson import json_util


MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DB_NAME = 'Your DB name'
COLLECTION_NAME = 'collectionname'

@app.route("/")
def getDatas():
    connection = MongoClient(MONGODB_HOST, MONGODB_PORT)
    collection = connection[DB_NAME][COLLECTION_NAME]
    projects = collection.find()
    json_projects = []
    for project in projects:
        json_projects.append(project)
    json_projects = json.dumps(json_projects, default=json_util.default)
    connection.close()
    return json_projects

if __name__ == "__main__":
    app.run(host='0.0.0.0',port=5000,debug=True)

Comments

3

Solution worked for me

#!pip install pymysql

import pymysql

pymysql.install_as_MySQLdb()

import MySQLdb

#!pip install pymongo
import pymongo
import json
from pymongo import MongoClient


import pandas as pd
from pymongo import MongoClient
# if __name__ == '__main__':
#     client = MongoClient("localhost", 27017, maxPoolSize=50)
client = MongoClient("localhost", 27017, maxPoolSize=50)
db = client.mydb
collection = db.angel
data = pd.DataFrame(list(collection.find()))

data

'''
Address City    Name    State   ZIP _id
0   123 Main St Whereverville   Jane Doe    CA  90210   5af03bfa8ca570abf4a2f76c
1   555 Broadway Ave    New York    John Doe    NY  10010   5af03bfa8ca570abf4a2f76d
'''

Comments

2

Refer this PyMongo Connection to connect and Connection is deprecated

1 Comment

link is outdated
0

Run this command from command line in mongo installtion directory.

...>mongo>...>bin>

mongod --dbpath data/db (here data/db is a path where your database)

Comments

0

A derivation of @amit above, I use this to quickly test a mongodb connection:

# pip install pymongo
from pymongo import MongoClient
client = MongoClient('mongodb://user:password@host:port/database')

for db in client.list_databases():
    print(db)

1 Comment

Where to find the "user" and "password" parameters for a localhost installation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.