Skip to content
Advertisement

Unable to connect to a database using JDBC within Spark with Scala

I’m trying to read data from JDBC in Spark Scala. Below is the code written in Databricks.

val df = spark
  .read
  .format("jdbc")
  .option("url", <connection-string>)
  .option("dbtable", <table-name>)
  .option("user", <username>)
  .option("password", <password>)
  .option("ssl", True)
  .option("sslmode", "require")
 .load()

I’m getting the following error message:

java.sql.SQLNonTransientConnectionException: Could not connect to 10.6.8.86:3306 : PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Could someone please let me know how to resolve this issue.

Advertisement

Answer

The certificate used by your host is not trusted by java.

Solution 1 (Easy, not recommended)

Disabled certificate checking and always trust the certificate provided by server.

Add trustServerCertificate property.

val df = (spark
  .read
  .format("jdbc")
  .option("url", <connection-string>)
  .option("dbtable", <table-name>)
  .option("user", <username>)
  .option("password", <password>)
  .option("ssl", True)
  .option("trustServerCertificate", True)
  .option("sslmode", "require")
 .load()

Solution 2 (Difficult, Recommended)

Get the certificate and store it in the trusted store of your system.

Read more about it here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement