How to Connect MySQL with Python

In this Python tutorial, we will be working on MySQL database connection with Python. In easy words, we will learn how to connect MySQL with Python program.

So we are going to make this tutorial easy to understand. In order to do that we will separate this tutorial into three parts.

  • What we need to connect the MySQL Database with Python.
  • Starting Database server.
  • The Python Program to establish a connection with MySQL in Python.

With those above three steps, we gonna achieve our goal to connect MySQL Database with Python or Establish a connection between Python program and MySQL Database.

This tutorial is a part of our MySQL Tutorial in Python,

Connect MySQL Database in Python

Let’s take a look at the requirements to build a connection between our Python program and MySQL Database.

At the very first we must need to have a connector. We can use MySQL Connector/Python.

In my previous tutorial, I have described How to install MySQL Connector in Python step by step. If you don’t know how to do this please go through the tutorial.

After installing MySQL Connector/Python start your MySQL Server.

( You may start whenever you want before you run your Python program )

Run MySQL Server

To run or start your MySQL server you can use cmd. If you are using XAMPP server then it will be easy for you as you will see an option to start apache and MySQL both in the XAMPP control panel.

Python program to connect MySQL Database with Python

After staring your MySQL server use the following code to check if everything is alright or not.

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="your_username_here",
  passwd="your_mysql_password_here",
  database="your_database_name_here"
)

if mydb.cursor:
  print("done")

host – put your hostname here. In general, you are running on your local machine it will be localhost or 127.0.0.1

user and passwd are the variables to store your MySQL server username and password.

It is recommended to use MySQL administrative password so that you can get all the privileges.

database is the variable to store your database name.

Put your database name here. ( If you want to work with a particular database put the database name here )

So all the required things are now stored in the variable mydb

If everything is fine. You should get an output

done

Or if anything went wrong you will get errors while executing your Python program.

If you are still having a problem with your connection please let us know by commenting in the below comment section.

3 responses to “How to Connect MySQL with Python”

  1. Richard Balele says:

    Hi Guys -hope all good. I tried the above code in Jupyter editor however there is no response from MySQL stored in xampp. No errors.
    Would the exact file location need to be specified in the code? Thanks.

    • Saruque Ahamed Mollick says:

      There is no need to mention the location or any path. The only thing you need to check if the host is correct or not. Also, don’t forget to check if your MySQL server is running or not.
      You must run your server before you run the program.

  2. Richard Balele says:

    Thank you for the update – connection to MySQL server was successful.

Leave a Reply

Your email address will not be published. Required fields are marked *