The Wayback Machine - https://web.archive.org/web/20240717024631/https://www.geeksforgeeks.org/mysql-connector-python-module-in-python/
Open In App

MySQL-Connector-Python module in Python

Last Updated : 09 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


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.


Previous Article
Next Article

Similar Reads

Connect MySQL database using MySQL-Connector Python
While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python.MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Pytho
2 min read
How to install MySQL connector package in Python?
Prerequisite: Python Language Introduction 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. A connector is employed when we have to use MySQL with other programmin
2 min read
Extract Data from Database using MySQL-Connector and XAMPP in Python
Prerequisites: MySQL-Connector, XAMPP Installation A connector is employed when we have to use MySQL with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. RequirementsXAMPP: Database / Server
2 min read
Modulenotfounderror: No Module Named Mysql in Python
Python is a versatile and widely used programming language that supports various libraries and modules for different functionalities. One common issue that developers may encounter is the "ModuleNotFoundError: No module named 'MySQL'" error. This error arises when the Python interpreter cannot find the required MySQL module, preventing the executio
3 min read
twitter-text-python (ttp) module - Python
twitter-text-python is a Tweet parser and formatter for Python. Amongst many things, the tasks that can be performed by this module are : reply : The username of the handle to which the tweet is being replied to. users : All the usernames mentioned in the tweet. tags : All the hashtags mentioned in the tweet. urls : All the URLs mentioned in the tw
3 min read
Python calendar module : formatmonth() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. class calendar.TextCalendar(firstweekday=0) can be used to generate plain text
2 min read
Python | Writing to an excel file using openpyxl module
Prerequisite : Reading an excel file using openpyxl Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, user might have to go through thousands of rows and pick out few handful information to make small changes b
3 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
PyMsgBox module in Python
PyMsgBox is simple, cross-platform, purely implemented in Python for message boxes as JavaScript has. It uses Python's built-in Tkinter module for its GUI. Installation This module does not built-in Python. To install it type the below command in the terminal. pip install PyMsgBox There are four functions in PyMsgBox, which follow JavaScript’s mess
2 min read
uniform() method in Python Random module
uniform() is a method specified in the random library in Python 3. Nowadays, in general, day-day tasks, there's always the need to generate random numbers in a range. Normal programming constructs require a method more than just one word to achieve this particular task. In python, there's an inbuilt method, "uniform()" which performs this task with
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg