MySQL-Connector-Python module in Python

MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command.

In this article, we will be discussing the MySQL Connector module of Python, how to install this module and a piece of code on how to connect this with the MySQL database. For any application, it is very important to store the database on a server for easy data access.

What is MYSQL Connector/Python?

MySQL Connector/Python enables Python programs to access MySQL databases, using an API that is compliant with the Python Database API Specification v2.0 (PEP 249). It is written in pure Python and does not have any dependencies except for the Python Standard Library.

There are various versions of MySQL Connector/Python available:

Connector/Python Version MySQL Server Versions Python Versions
8.0 8.0, 5.7, 5.6, 5.5 3.8, 3.7, 3.6, 3.5, 3.4, 2.7
2.2 (continues as 8.0) 5.7, 5.6, 5.5 3.5, 3.4, 2.7
2.1 5.7, 5.6, 5.5 3.5, 3.4, 2.7, 2.6
2.0 5.7, 5.6, 5.5 3.5, 3.4, 2.7, 2.6
1.2 5.7, 5.6, 5.5 (5.1, 5.0, 4.1) 3.4, 3.3, 3.2, 3.1, 2.7, 2.6

Installing module

This module does not come built-in with Python. To install it type the below command in the terminal.



pip install mysql-connector-python

python-mysql

python-mysql

If any issue is there while installing, one can explicitly specify the module version as follows:

pip install mysql-connector-python==8.0.17

Also, to uninstall current MySQL Connector/Python, you use the following command:

pip uninstall mysql-connector-python

Verifying MySQL Connector/Python installation

After installing the MySQL Python connector, we need to test it to make sure that it is working correctly and we are able to connect to the MySQL database server without any issues.

To verify the installation, we can use the following steps:

  • Open Python command line
  • Type the following code
filter_none

edit
close

play_arrow

link
brightness_4
code

import mysql.connector
  
  
mysql.connector.connect(host='localhost',
                        database='database',
                        user='root',
                        password='your password')

chevron_right


Output:

python-mysql-1

If you see the following output, it means that you have been successfully installing the MySQL Connector/Python on your system.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.