2 / 7
May 2024

Hi I am unable to connect to mongodb atlas server from Databricks. I have configured Allow all hosts in network access section and i am able to connect to Mongodb from a Mongo Compass client from my system. I am not sure why it’s not working from Databricks cloud. Below is the code i am using, i have installed the required library as specified in this url:

https://docs.databricks.com/en/_extras/notebooks/source/mongodb.html

val connectionString1 = "mongodb+srv://phanivyr:MyPassword@cluster0.awdeasr.mongodb.net/sample_airbnb?retryWrites=true&w=majority&appName=Cluster0" val database = "sample_airbnb" val collection = "listingsAndReviews" val df = spark.read.format("mongodb").option("database", database).option("spark.mongodb.input.uri", connectionString1).option("collection", collection).load() df.printSchema()

Hi Aasawari, Below is the error i am getting:

MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]

26 days later
3 months later

Hi all,

Thank you for reaching out.
There could be more than one reason to find the solution to this issue being seen depends on further information.

If you are looking to find a solution for the same, please raise a support ticket with the support team and provide all the information.

Regards
Aasawari

Hi @Phani_Kanakamedala, @Aasawari

You can use the code below to connect to Mongo Altas using Databricks.

df = spark.read.format("mongodb")\ .option("database", "sample_airbnb")\ .option("collection", "listingsAndReviews")\ .option("spark.mongodb.read.connection.uri", "mongodb+srv://phanivyr:MyPassword@cluster0.awdeasr.mongodb.net")\ .load()

Note: My Databricks compute configuration is as below
Spark: 3.5.3
scala - 2.13
mongo-connector : org.mongodb.spark:mongo-spark-connector_2.13:10.4.0
Install the mongo-connector from Maven Central