cancel
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.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How do I connect to a MySQL Database in Python?

Kaniz_Fatma
Community Manager
Community Manager
 
1 ACCEPTED SOLUTION

Accepted Solutions

spark-user
Contributor

Hi Kaniz,

One way you could do this is to use the connector API and call its constructor as follows

import mysql.connector
 
sqlConnect = mysql.connector.connect(user='userName', password='password', host='127.0.0.1', database='databaseName')
 

You would also want to close the connection after you are done using it

sqlConnet.close()

View solution in original post

2 REPLIES 2

spark-user
Contributor

Hi Kaniz,

One way you could do this is to use the connector API and call its constructor as follows

import mysql.connector
 
sqlConnect = mysql.connector.connect(user='userName', password='password', host='127.0.0.1', database='databaseName')
 

You would also want to close the connection after you are done using it

sqlConnet.close()

_raman_
New Contributor II

I have tried to connect the mysql database using above code but failed to connect.

getting this as error : DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (111)

and after using host as 127.0.0.1 getting this as error : 

DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group