Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
Showing results for 
Search instead for 
Did you mean: 

Unable to connect mongo with Databricks

New Contributor II


I am trying to connect mongo with Databricks. I also used SSL certificate.

I created my own cluster and installed maven library org.mongodb.spark:mongo-spark-connector_2.12:3.0.1.

This is my code:


connection_string =f"mongodb://{secret['user']}:{secret['password']}@{secret['host']}:{secret['port']}/?authSource={secret['database']}&tls=true&tlsCAFile=temp/CA-certificate.pem"

df ="com.mongodb.spark.sql.DefaultSource")\
    .option("database", database)\
    .option("collection", collection)\
    .option("spark.mongodb.input.uri", connectionString)\
    .option("ssl", "true")\
    .option("sslCertificate", sslCertificateFilePath)\


When I am running above code getting below error:


Please provide me solution for this.




Esteemed Contributor
Esteemed Contributor

@InTimetec - can you please check/list if the sslCertificate is available in the sslCertifcateFilePath mentioned above? 

Yes, I verified. The path is correct.

Community Manager
Community Manager

Hi @InTimetecHere are some steps to help you resolve it:

  1. Firewall and IP Unblock:

    • Ensure that the target IP address is unblocked on your firewall. Sometimes, connectivity issues arise due to blocked IPs.
    • Make sure your MongoDB server allows incoming connections from your Databricks cluster’s IP address.
  2. Certificate Location:

    • Verify that the path to your .pem certificate file is correctly specified in the URL.
    • The format should be something like this:, data=d, verify=path_to_your_certificate.pemfile)
  3. Check the MongoDB Connection Options:

    • Try different connection options to see if any of them work:
      • Use --tlsCAFile=/path/to/your/ca.pem when connecting via the MongoDB shell.
      • Alternatively, try --tlsUseSystemCA or --tlsAllowInvalidCertificates.
  4. Regenerate Certificates (if needed):

    • If you’ve made changes to your certificates, consider regenerating them:
      openssl genrsa -out mongodb.key 2048
      openssl req -new -key mongodb.key -out mongodb.csr

Remember that SSL/TLS configuration can be sensitive, so double-check your settings and ensure that your certificates are valid. Hopefully, one of these steps will help you resolve the issue! 🚀1234


New Contributor II

@Kaniz I updated my code as below:

 df ="com.mongodb.spark.sql.DefaultSource")\
        .option("database", database)\
        .option("collection", collection)\
        .option("spark.mongodb.input.uri", connectionString)\

Now I am getting below error:

Error: An error occurred while calling o516.load.
: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{, type=UNKNOWN, state=CONNECTING}]
	at com.mongodb.internal.connection.BaseCluster.getDescription(
	at com.mongodb.internal.connection.SingleServerCluster.getDescription(
	at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(
	at com.mongodb.client.internal.MongoClientDelegate.createClientSession(
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(
	at com.mongodb.client.internal.MongoDatabaseImpl.executeCommand(
	at com.mongodb.client.internal.MongoDatabaseImpl.runCommand(
	at com.mongodb.client.internal.MongoDatabaseImpl.runCommand(
	at com.mongodb.spark.MongoConnector.$anonfun$hasSampleAggregateOperator$1(MongoConnector.scala:234)
	at com.mongodb.spark.MongoConnector.$anonfun$withDatabaseDo$1(MongoConnector.scala:171)
	at com.mongodb.spark.MongoConnector.withMongoClientDo(MongoConnector.scala:154)
	at com.mongodb.spark.MongoConnector.withDatabaseDo(MongoConnector.scala:171)
	at com.mongodb.spark.MongoConnector.hasSampleAggregateOperator(MongoConnector.scala:234)
	at com.mongodb.spark.rdd.MongoRDD.hasSampleAggregateOperator$lzycompute(MongoRDD.scala:221)
	at com.mongodb.spark.rdd.MongoRDD.hasSampleAggregateOperator(MongoRDD.scala:221)
	at com.mongodb.spark.sql.MongoInferSchema$.apply(MongoInferSchema.scala:68)
	at com.mongodb.spark.sql.DefaultSource.constructRelation(DefaultSource.scala:97)
	at com.mongodb.spark.sql.DefaultSource.createRelation(DefaultSource.scala:50)
	at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:390)
	at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:378)
	at org.apache.spark.sql.DataFrameReader.$anonfun$load$2(DataFrameReader.scala:334)
	at scala.Option.getOrElse(Option.scala:189)
	at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:334)
	at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:226)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at py4j.reflection.MethodInvoker.invoke(
	at py4j.reflection.ReflectionEngine.invoke(
	at py4j.Gateway.invoke(
	at py4j.commands.AbstractCommand.invokeMethod(
	at py4j.commands.CallCommand.execute(
	at py4j.ClientServerConnection.waitForCommands(

I tried to increase timeout as well. for this I added below code.

.option("spark.mongodb.input.connectionTimeoutMs", "120000")

 But still getting same error. 

Esteemed Contributor
Esteemed Contributor

@InTimetec  - could you please check if the port 27017 is accessible from the workspace VPC network? Also, please check with your internal network team for any connectivity issues. 

Per this documented observation - you can use DBR 13.3 LTS cluster for compatibility.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!