[SOLVED] Cant connect to Mysql database from pyspark, getting jdbc error


I am learning pyspark, and trying to connect to a mysql database.

But i am getting a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Exception while running the code. I have spent a whole day trying to fix it, any help would be appreciated 🙂

I am using pycharm community edition with anaconda and python 3.6.3

Here is my code:

from pyspark import SparkContext,SQLContext
sc= SparkContext()
sqlContext= SQLContext(sc)

df = sqlContext.read.format("jdbc").options(
url ="jdbc:mysql://",
driver = "com.mysql.jdbc.Driver",
dbtable = "billing",

Here is the error:

py4j.protocol.Py4JJavaError: An error occurred while calling o27.load.
: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver


This got asked 9 months ago at the time of writing, but since there’s no answer, there it goes. I was in the same situation, searched stackoverflow over and over, tried different suggestions but the answer finally is absurdly simple: You just have to COPY the MySQL driver into the “jars” folder of Spark!

Download here https://dev.mysql.com/downloads/connector/j/5.1.html

I’m using the 5.1 version, although 8.0 exists, but I had some other problems when running the latest version with Spark 2.3.2 (had also other problems running Spark 2.4 on Windows 10).

Once downloaded you can just copy it into your Spark folder
E:\spark232_hadoop27\jars\ (use your own drive:\folder_name — this is just an example)

You should have two files:

After that the following code launched through pyCharm or jupyter notebook should work (as long as you have a MySQL database set up, that is):

import findspark

import pyspark # only run after findspark.init()
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()

dataframe_mysql = spark.read.format("jdbc").options(
    driver = "com.mysql.jdbc.Driver",
    dbtable = "company",


Bear in mind, I’m working currently locally with my Spark setup, so no real clusters involved, and also no “production” kind of code which gets submitted to such a cluster. For something more elaborate this answer could help: MySQL read with PySpark

Answered By – Kondado

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.