Getting Started with MySQL Connector/Python

Summary: In this tutorial, you will learn about MySQL Connector/Python which allows you to interact with MySQL databases from Python.

Introduction to MySQL Connector/Python

To interact with the MySQL database from Python, you need a database driver. MySQL Connector/Python is a standardized database driver provided by MySQL.

Python MySQL

MySQL Connector/Python supports almost all features provided by MySQL. It allows you to convert the parameter’s value between Python and MySQL data types such as Python datetime and MySQL DATETIME.

MySQL Connector/Python is designed specifically for MySQL. It supports all MySQL extensions such as LIMIT clause.

MySQL Connector/Python allows you to compress the data stream between Python and MySQL database server using protocol compression. It supports connections using TCP/IP sockets and secure TCP/IP connections using SSL.

MySQL Connector/Python is an API implemented using pure Python. It means that you don’t need to install any MySQL client library or any Python modules except the standard library.

MySQL Connector/Python versions

You will need to install the correct versions of Python, MySQL, and Connect/Python. The following table illustrates the compatible versions of Connect/Python, MySQL, and Python:

Connector/Python VersionMySQL Server VersionsPython VersionsConnector Status
8.x Innovation8.1, 8.0, 5.7, 5.63.12 (8.2.0), 3.11, 3.10, 3.9, 3.8General Availability
8.08.0, 5.7, 5.6, 5.53.11, 3.10, 3.9, 3.8, 3.7, (3.6 before 8.0.29), (2.7 and 3.5 before 8.0.24)General Availability
2.2 (continues as 8.0)5.7, 5.6, 5.53.5, 3.4, 2.7Developer Milestone, No releases
2.15.7, 5.6, 5.53.5, 3.4, 2.7, 2.6General Availability
2.05.7, 5.6, 5.53.5, 3.4, 2.7, 2.6GA, final release on 2016-10-26
1.25.7, 5.6, 5.5 (5.1, 5.0, 4.1)3.4, 3.3, 3.2, 3.1, 2.7, 2.6GA, final release on 2014-08-22

Installing MySQL Connector/Python

Prerequisites

Before installing MySQL Connector/Python, you need to have:

  • The Root or Administrator privileges to perform the installation.
  • The Python 3.x installed on your system

Note that the MySQL Connector/Python installation requires Python to be in the system’s PATH or it will fail.

Installation

The MySQL Python Connector is available on pypi.org, therefore, you can install it using the pip command.

First, open Command Prompt on Windows or Terminal on Unix-like systems.

Second, create a new directory such as pub to store the project files:

mkdir pub

Third, go inside the pub directory:

cd pub

Fourth, create a new virtual environment:

python -m venv venv

Fifth, activate the new virtual environment:

venv\scripts\activate

Sixth, install the mysql-connector-python package:

pip install mysql-connector-python

Seven, run the Python CLI:

python

Eight, import the mysql connector:

import mysql.connectorCode language: JavaScript (javascript)

Finally, display the help of the mysql.connector package:

help(mysql.connector)Code language: CSS (css)

If you see the following output, meaning that you have installed the mysql-connector-python package successfully:

Help on package mysql.connector in mysql:

NAME
    mysql.connector - MySQL Connector/Python - MySQL driver written in Python.
...

Finally, press Ctrl+C twice to exit Python.

Summary

  • Use MySQL Connector/Python to interact with the MySQL databases.
Was this tutorial helpful?