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:

1
python -m pip install mysql-connector-python

Step 2 – Test the MySQL Connector

Create a Python file and import the new package:

1
import mysql.connector

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:

1
2
3
4
5
6
7
8
9
import mysql.connector

your_database = mysql.connector.connect(
  host = "localhost"
  user = "username"
  password = "YoUrPaSsWoRd"
)

print(your_database)

Step 4 – Run the application

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

1
python app.py