Getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver when connecting to db from databricks

Viewed 1561

I am getting the below error when trying to connect to db from databricks using Python:

Error:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Code:

jdbcHostname = "hostname"
jdbcDatabase = "dbname"
jdbcUsername="user"
jdbcPassword="pwd"
jdbcUrl = "jdbc:mysql://{0}/{1}".format(jdbcHostname, jdbcDatabase)
connectionProperties = {
  "user" : jdbcUsername,
  "pwd" : jdbcPassword,
  "driver" : "com.mysql.jdbc.Driver"
}

pushdown_query = "select top 10 [unique id] from table"
dfcontest= spark.read.jdbc(url=jdbcUrl, table=pushdown_query, properties=connectionProperties)
display(dfcontest)

What is wrong here? can you kindly help. should we add any jar?

3 Answers

Make sure, you have to attached the mysql-connector-java library to your cluster as a library.

To make third-party or locally-built code available to notebooks and jobs running on your clusters, you can install a library. Libraries can be written in Python, Java, Scala, and R. You can upload Java, Scala, and Python libraries and point to external packages in PyPI, Maven, and CRAN repositories.

Steps to install third party libraries:

Step1: Create Databricks Cluster.

Step2: Select the cluster created.

Step3: Select Libraries => Install New => Select Library Source = "Maven" => Coordinates => Search Packages => Select Maven Central => Search for the package required. Example: mysql-connector-java library => Select the version required => Install

enter image description here

Actually you should just pick default driver org.mariadb.jdbc for mysql. Works for me that way.

Try using

"driver" : "com.mysql.cj.jdbc.Driver"
Related