简体   繁体   中英

Django(djongo) can't connect to MondoDB Atlas after Heroku deployment

I managed to get it working locally (different cluster, separate settings.py), but not after when deployed to Heroku.

Heroku - automatically adds DATABASE_URL config var with a postgresql, and I cannot remove/edit it.

MongoDB Atlas - I've set the MongoDB Atlas cluster to allow IPs from everywhere. And the password has no funny characters.

django production settings.py

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'DBProd',
        'CLIENT': {
          'host': "mongodb+srv://XXX:YYY@ZZZ.pc4rx.mongodb.net/DBProd?retryWrites=true&w=majority",
        }
    }
}

I ran migrate straight after the deployment and it's all green OKs heroku run python manage.py migrate

Everything works functional wise, just that the data are not stored in the MongoDB Atlas cluster . There are lots of posts from various sites on this, but they all have different instructions... Some of the posts I tried to follow:

-- A very confused beginner

For whoever lands on this. Add 'authMechanism': 'SCRAM-SHA-1', below 'host' will fix the issue.

This happened to me as well, in my case, somehow Heroku added postgresql as an add-on for my app so have to delete that first. Check Heroku documentation about postgresql and how to destroy add-on: https://devcenter.heroku.com/changelog-items/438

https://devcenter.heroku.com/articles/managing-add-ons

Then you need to configure MongoDB as your database by adding a MONGODB_URI config var with your database url. You should be ready to connect to your MongoDB now!

The following doc also helps: https://www.mongodb.com/developer/how-to/use-atlas-on-heroku/

I've been having the same issue. Everything works fine locally. The problem is when deploying on Heroku. I have added 'authMechanism': 'SCRAM-SHA-1' and I have also configured MongoDB as my database by adding a MONGODB_URI config var . Heroku still autoconfigures DATABASE_URL config var with a postgresql , and I cannot remove/edit it.

In my Javascript code I used fetch('127.0.0.1:8000/<something>') which I specified in my urls.py and that way views.py read data from pymongo and returned it as a response.

After deploying my app to Heroku (and hardcoding the 127.0.0.1:8000 to <myappname>.heroku.com ) the same fetch method seems to return [] instead of json containing values from MongoDB.

This is the most identical issue I found in a post I hope I'm not out of subject.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM