1

I'm using PyMongo. Everything works fine and can connect fine to MongoDB, that's on my computer but when I put the scripts on GitHub and run them through Heroku for my Discord bot I keep on getting the error saying:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

I don't know why this happens while it works fine on my computer, I put pymongo in requirements.txt. Below is how I connect to MongoDB (with PyMongo):

import pymongo
from pymongo import MongoClient, ReturnDocument
dbclient = MongoClient('mongodb://localhost:27017/')
# On Heroku I get error:"localhost:27017: [Errno 111] Connection refused"
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257

4 Answers4

1

On your local machine, you can set the specific port to use (e.g. 27017). Does heroku choose the port for you instead?

garyboland
  • 135
  • 12
  • I'm not quote sure about that, I don't think it does. –  Jun 02 '19 at 18:22
  • how do you start a process and choose the port it runs on in Heroku? – garyboland Jun 02 '19 at 18:24
  • pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused this tells you that pymongo is not running on that port. As you cannot tell heroku to pick that port then pymongo will not be running on that port. Hence the error you see. – garyboland Jun 04 '19 at 20:47
0

Heroku isn't a host where you can run arbitrary things on the local machine. You'll have to connect to a non-local MongoDB host instead of localhost. One easy way to do that is to select an appropriate add-on and add it to your app.

For example, you might choose to use the free starter version of mLab MongoDB. You can provision this add-on by running

heroku addons:create mongolab:sandbox

This addon will set an environment variable MONGODB_URI for you, which you can use to connect:

import os

# Use the default argument if you don't want to have to set MONGODB_URI on your
# dev machine
mongodb_uri = os.getenv('MONGODB_URI', default='mongodb://localhost:27017/')

dbclient = MongoClient(mongodb_uri)
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • When i do `heroku addons:create mongolab:sandbox` in Heroku Cli i get error saying "`Missing required flag: -a, --app APP app to run command against`" –  Jun 03 '19 at 00:20
  • So [provide that argument](https://stackoverflow.com/a/55802646/354577). Or [configure your app to have only one Heroku Git remote](https://stackoverflow.com/a/55477397/354577) so you don't have to. Or [provision it by clicking the "install" button on the add-on page](https://devcenter.heroku.com/articles/what-is-an-add-on#step-1-the-developer-initiates-provisioning). When you get an error message like that, a good first step is to search for what it means. In this case you probably would have found the same solutions I just recommended. – ChrisGPT was on strike Jun 03 '19 at 00:29
0

I decided to not use local host because i couldn't understand it, I'm now using an URL given by mongo DB to with my username and password which you can create by going to https://www.mongodb.com/cloud and creating a project and a cluster and collections then the url should be given to you, url should be something like this mongodb+srv://<username>:<password>@cluster-apc2i.mongodb.net/test?retryWrites=true&w=majorityadd that url to your script like this

client = MongoClient("mongodb+srv://<username>:<password>@cluster-apc2i.mongodb.net/test?retryWrites=true&w=majority")

Also make sure to add 0.0.0.0/0 to your allowed ip, that ip means all ip addresses with details are allowed to access, if you don't add that you may get an error saying, time out and other URL may be given to you after you create a new user from the Database Access Pannel on the left

-2

You should delete the mongod.lock lock from /var/lib/mongodb After that you can restart the service.

Also you can try to change part of client code by

client = MongoClient('localhost', 27017)
grad
  • 1
  • 4