How to Install MySQL Driver in Python

0 min read 116 words

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

Step 2 – Test the MySQL Connector

Create a Python file and import the new package:

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:

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:

python app.py
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts