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:user123@localhost27017.mongolab.com: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
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"})
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)
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)
#!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
'''
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.