Jordan Wrong
Jordan Wrong

Reputation: 1245

Inserting data into mongoDB using pymongo

i seem to be unable to upload data into my mongo db hosted on atlas. I have copied the exact steps posted here. https://www.w3schools.com/python/python_mongodb_insert.asp

import pymongo
import requests

url= "mongodb://jordan:*********@jordandb-shard-00-00-ykcna.mongodb.net:27017,jordandb-shard-00-01-ykcna.mongodb.net:27017,jordandb-shard-00-02-ykcna.mongodb.net:27017/test?ssl=true&replicaSet=JordanDB-shard-0&authSource=admin&retryWrites=true"

client = pymongo.MongoClient(url)
mydb = client.test
mycol = mydb["customers"]
mydict = {"name":"John", "adress":"Highway 37"}
x = mycol.insert_one(mydict)

print(client.list_database_names())

I am receiving a timeout error. Every line works until i get to the insert line (x = ....). I am using pycharm and python 3.7. However I have also tried this on jupyter and have received the same error:

pymongo.errors.ServerSelectionTimeoutError: jordandb-shard-00-00-ykcna.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056),jordandb-shard-00-01-ykcna.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056),jordandb-shard-00-02-ykcna.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

Here are the steps I am following to get the url enter image description here enter image description here

Upvotes: 2

Views: 818

Answers (2)

Shriyash Warghade
Shriyash Warghade

Reputation: 213

You Can Use pymongo default function

connObj = MongoClient(MONGO_HOST, MONGO_PORT)
connObj[MONGO_DB].authenticate(MONGO_UNAME, MONGO_PASSWD)

Upvotes: 0

Torxed
Torxed

Reputation: 23480

There's a couple of reasons for this.
The most obvious one would be that you're missing the certificate chain.

You can get the chain information from issuing:

openssl s_client -showcerts -servername jordandb-shard-00-00-ykcna.mongodb.net -connect jordandb-shard-00-00-ykcna.mongodb.net:27017 </dev/null

Which will tell you that it's from DigiCert. So either you're missing that certificate chain in your local certificate store (some distro's might need you to install a root ca trust). But if you do have root ca's installed. It's time to check the validation times on the cert.
If you add | openssl x509 -noout -dates you'll get the valid dates for this certificate:

openssl s_client -showcerts -servername jordandb-shard-00-00-ykcna.mongodb.net -connect jordandb-shard-00-00-ykcna.mongodb.net:27017 </dev/null  | openssl x509 -noout -dates

Which tells you the certificate is from DigiCert.
And the certificate is valid from 7/02-19 00:00 GMT to 11/2-19 12:00 GMT.
Running date in any terminal should hopefully tell you that you're in between these two dates.

In any other case, I would say this is due to a self signed certificate.
In which case you would need to do one of two things:

MongoClient(..., ssl_ca_certs='/path/to/ca.pem')
MongoClient(..., ssl_cert_reqs=ssl.CERT_NONE)

To either supply your custom CA or tell Mongo to ignore certificate validation (the later being the worst possible option. Even if you say "I won't forget to fix that later", heh).

Upvotes: 1

Related Questions