Before you install MySQL Connector/Python make sure you have MySQL and Python installed on your system. In case you don’t have these installed already visit the following links for further instructions.
MySQL Connector/Python Version
In this tutorial, we will be using Connector/Python version 8.0 which works with MySQL (8.0, 5.7, 5.6, 5.5) and Python (3.6, 3.5, 3.4, 2.7). If you want to work with older versions of MySQL or Python then checkout the version matrix available at this link.
Creating Virtual Environment
Create a new directory named
learn-mysql-connector using the
mkdir command as follows:
$ mkdir learn-mysql-connector
We will use this directory to store the examples in this tutorial. All the examples used in this tutorial are available for download at this link.
Next, change your current working directory to
learn-mysql-connector and create a new virtual environment using the
$ cd learn-mysql-connector
$ virtualenv env
Note: If you are new to virtualenv visit Virtualenv Guide to learn more.
If you are on Linux or Mac OS, execute the following command to activate the virtual environment.
$ source env/bin/activate
To deactivate the virtual environment type:
$ source env/bin/deactivate
Windows users can use the following command to activate and deactivate the virtual environment:
We are now ready to install MySQL Connector/Python.
Installing MySQL Connector/Python
The easiest way to install Connector/Python on most platforms is via the
$ pip install mysql-connector-python
Another way to install MySQL Connector/Python is to download it directly from the official site. The following screen shows the list of packages available for Ubuntu Linux.
Once the package is installed start the Python interpreter and import
mysql.connector module as follows:
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector as m
If you get the version as indicated in the preceding snippet then you have a working Connector/Python installation.
The C Extension
Initially, MySQL Connector/Python was written in pure Python. In version 2.1, the C extension was introduced, which allows better performance when dealing with large result sets as compared to pure Python implementation.
If you have installed Connector/Python using pip on Linux then it is highly likely that the C extension is installed with the package. If this is not the case, you can download the Connector/Python package which comes with C Extension from the download page.
The packages which have “cext” in their name come with C extension, while the rest of them are pure Python implementation.
We can also check whether you have C extension installed on not by importing the
_mysql_connector module rather than the
>>> import _mysql_connector
The import statement succeeds because I am on Linux and
pip has installed the C extension.
However, If you are on Windows, you will get an error like this:
>>> import _mysql_connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named '_mysql_connector'
Unfortunately, at the time of writing Windows packages don’t come with the C extension.
Most of the examples in this tutorial uses pure Python implementation. So you can follow along without having to install the C extension. However, once you moved to the production environment you should definitely consider using the C extension.
Installing the World Database
All the examples in this tutorial will query the world sample database. To download the database visit https://dev.mysql.com/doc/index-other.html.
Once the download finishes, extract the archive and you will get a single file called
To create the database from
world.sql file execute the following:
$ mysql -u root -p < /path/to/world.sql
You should now have world sample database installed. In the next lesson, we will learn how to connect to the database using Connector/Python.