Skip to content

How to Install MySQL Driver in Python

To begin using MySQL in Python, you need to do the following:

Step 1 – Install the MySQL Connector

Using pip, we can install the MySQL Connector package:

python -m pip install mysql-connector-python
Code language: Bash (bash)

Step 2 – Test the MySQL Connector

Create a Python file and import the new package:

import mysql.connector
Code language: Python (python)

Step 3 – Create a Connection to the MySQL Database

Now you can create a connection to your MySQL database.

Create a file called app.py and place the following inside:

import mysql.connector your_database = mysql.connector.connect( host = "localhost" user = "username" password = "YoUrPaSsWoRd" ) print(your_database)
Code language: Python (python)

Step 4 – Run the application

Now run the app.py Python script to see our connection occur:

python app.py
Code language: Bash (bash)

See also  Overloading Operators in Java
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x