Reputation: 1116
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
Upvotes: 17
Views: 67866
Reputation: 4600
#!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
'''
Upvotes: 3
Reputation: 147
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)
Upvotes: 0
Reputation: 6972
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)
Upvotes: 6
Reputation: 470
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"})
Upvotes: 21
Reputation: 2990
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)
Upvotes: 0
Reputation: 965
Refer this PyMongo Connection to connect and Connection is deprecated
Upvotes: 2