Signup/Sign In

MySQL with Python

In this tutorial, we will learn What is MySQL, What are the prerequisites to use MySQL in Python and from where you can download and install MySQL and MySQL Python Connector.

First of all, let us see what are the Prerequisites needed to learn MySQL in Python:

Python MySQL - Prerequisites

Below are the prerequisites needed in order to gain complete understanding:

  • Python Variable, Data types, Control Structures, Loops, etc. Anyone who wants to put their step into this tutorial must be aware of these topics.

  • Basics of SQL. If you want to learn the basics of SQL then here is the link for our free course: Learn SQL.


In this tutorial our main aim to teach you how you can work with MySQL in Python.

What is MySQL?

Let us first learn what MySQL is:

MySQL is an open-source, relational database management system(RDBMS) that is based on Structured Query Language(SQL). One important thing to note here is that MySQL is used to store data and it is not used to create programs; Thus it is not a Programming Language.

  • Thus SQL can be used to program a MySQL Database.

  • The main advantage of MySQL is that it can run on any of the Operating System.

  • It is one of the Popular Database.

If you want to practice the code examples in our upcoming tutorials then it is mandatory for you to install MySQL on your Computer. It is freely available you can download and install the MySQL database from its official website :

After installing MySQL the next step is to install mysql connector for the python. Now the question arises what is MySQL connector?

MySQL Connector

Basically Python needs a MySQL Driver which is used to access the MySQL Database an in this case "MySQL Connector" is the Driver

MySQL Connector for Python is a connector that enables the Python programs to access the MySQL database.

There are two ways to download the MySQL Connector:

  1. The first way is you can download and install it from the link given:

  2. Another way is By Using PIP to install the "MySQL Connector" as PIP is already installed in your Python environment. So you can download and install just by navigating your command line to the location of PIP and By writing:

python -m pip install mysql-connector-python

We Recommend you to use the second way that is by PIP; also it is your choice.

Now, After this MySQL Driver that is "MySQL Connector" is downloaded and installed successfully in your Computer. But in order to confirm, we can Test the MySQL Connector.

Test the MySQL Connector

To check if the MySQL connector is installed correctly or not in your System you just need to write the following code:

import mysql.connector

If the above code runs successfully, without raising any error then it means installation is done correctly.

Now we will create the connection with the help of which further we can create the database, insert into it, and perform other queries too.

Creating the Connection

It is the first step that is creating the connection.

Now we will connect to the database using the username and the password of MySQL. If you forgot your username or password, create a new user with a password.

###Connecting to the database

###Let us import mysql.connector

import mysql.connector

## Now connecting to the database using 'connect()' method
## it takes 3 required parameters 'host', 'user', 'passwd'

mydb = mysql.connector.connect(


It will produce the Output as follows:

<mysql.connector.connection_cext.CMySQLConnection object at 0x0000023F50726518>

The above output indicates that you have connected to the MySQL Database. In our further tutorial, we will start querying the database with the SQL statements.