- Install MySQL Connector Python on Windows, MacOs, Linux, Unix and Ubuntu
- Table of contents
- Prerequisites before installing MySQL Connector Python
- Python MySQL Connector Versions
- Pip Command to install MySQL Connector python
- Verifying MySQL Connector Python installation
- Download and Install MySQL Connector Python on Windows
- Install MySQL Connector Python on Windows using a Source Code Distribution:-
- Verifying MySQL Connector/Python installation on windows
- Download and Install MySQL Connector Python on Linux
- Verifying MySQL Connector Python installation on Linux
- Download and Install MySQL Connector Python on MacOs
- Install MySQL Connector Python on Ubuntu
- Next Steps:
- About Vishal
- Related Tutorial Topics:
- Python Exercises and Quizzes
- mysqlclient
- Support
- Install
- Windows
- macOS (Homebrew)
- Linux
- Customize build (POSIX)
- Documentation
Install MySQL Connector Python on Windows, MacOs, Linux, Unix and Ubuntu
In this lesson, You will learn how to Install MySQL Connector Python on Windows, macOS, Linux, Unix, and Ubuntu using pip and vis source code. To connect to a MySQL server from Python, you need a database driver (module). MySQL Connector Python is the official Oracle-supported driver to connect MySQL through Python.
Further Reading:
Table of contents
Prerequisites before installing MySQL Connector Python
- You need root or administrator privileges to perform the installation process.
- Python must be installed on your machine.
Note: – MySQL Connector Python requires Python to be in the system’s PATH. Installation fails if it doesn’t find Python.
- Python is generally located in a directory included in the default PATH setting on Unix and Unix-like systems.
- On Windows, If Python doesn’t exist in the system’s PATH, please manually add the directory containing python.exe yourself.
This article applies to: –
Platform(s): 64-bit Windows, Windows 10, Windows 7, Windows 8, Windows Vista, Windows XP, Linux, Ubuntu Linux, Debian Linux, SUSE Linux, Red Hat Linux, Fedora, MacOs.
Python version(s): Python 2 and 3
MySQL Version(s): Greater than 4.1
Ways to install MySQL Connector Python
There are multiple ways to install Oracle’s MySQL Connector Python on your machine. The following are a few ways.
- Install MySQL Connector Python using the pip command
- Install MySQL connector python via source code (via ZIP or TAR file)
- Use Built Distribution A package created in the native packaging format intended for a given platform. For example, RPM packages for Linux or MSI installer for windows.
Python MySQL Connector Versions
Please refer to the following table of MySQL connector Python versions. You need to install a module that is compatible with your Python version.
MySQL Connector Python | MySQL Versions | Python Versions |
---|---|---|
8.0 | 8.0, 5.7, 5.6, 5.5 | 3.6, 3.5, 3.4, 2.7 |
2.2 | 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 |
Great! Now you can choose the version as per your need.
Pip Command to install MySQL Connector python
It is always accessible and straightforward to install any module using pip in Python. MySQL Connector Python is available on pypi.org, so you can install it using the pip command.
pip install mysql-connector-python
If you are facing any problem while installing, please mention the module’s version and then try to install it again. Refer to the above table to install the correct version.
pip install mysql-connector-python==8.0.11
If you are facing pip install fails error with connection error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:598) . You can solve this error.
You can ignore SSL errors by setting pypi.org and files.pythonhosted.org as trusted hosts. Please try following the pip command to install MySQL Connector Python.
python -m pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org mysql-connector-python
Verifying MySQL Connector Python installation
You should get the following messages after running pip command: –
- Collecting mysql-connector-python
- Downloading packages.
- Requirement already satisfied: setup tools in D:python\python37-32\lib\site-packages.
- Installing collected packages: mysql-connector-python
- Successfully installed mysql-connector-python-8.0.13
Verify MySQL Connector Python installation by Connecting to MySQL Using MySQL Connector Python.
If you are unable to install using pip you can install using the following approaches.
Download and Install MySQL Connector Python on Windows
There are two ways to install MySQL Connector Python on windows.
- Install using Source Code Distribution ( Platform Independent and Architecture Independent ZIP Archive)
- Install using Built Distribution i.e., MSI installer
Install MySQL Connector Python on Windows using a Source Code Distribution:-
Follow below instruction to download Platform Independent ZIP. Go to download MySQL Connector Python for windows from here
- Abobe URL automatically opens the latest version of MySQL Connector Python.
- If you want to use the older version which is compatible with your python version, then select “Looking for previous GA versions” option which you can find at the right side.
- If you want to check which version of MySQL Connector Python is compatible with your python version, refer to the above table.
I am downloading 2.1.7 because I am using Python 3.5. Select Platform independent from the drop-down list
Click on the “download” button to download the ZIP file on your machine. After clicking download you get the below screen, click on No Thanks, start the download option.
Note: If you want to download the latest version, i.e. 8.0.1 then select “Looking for the latest GA versions” option which you can find at the right side.
After the download is complete, please follow the below steps to install: –
- Unpack or extract the Zip archive in the intended installation directory (for example, C:\mysql-connector\) using 7Zip or another tool that can read .zip files.
- Start a console window and change the location to the folder where you unpacked the Zip archive:
You should get the following screen after this command.
Verifying MySQL Connector/Python installation on windows
To verify MySQL connection Python is installed and to make sure that it is working correctly and you can connect to the MySQL database server without any issues. To verify the installation use the following steps:
- On Windows, the default MySQL Connector Python installation location is C:\Python.Version\Lib\sitepackages\ . Here Python.version is the Python version you used to install the connector.
- Type importing MySQL connector using import mysql.connector . If it is executed successfully mean installation completed successfully.
- Also, you can check that MySQL Connector Python installation is working and able to connect to MySQL Server by Connecting to MySQL Using MySQL Connector Python.
Download and Install MySQL Connector Python on Linux
There are two ways to install MySQL Connector Python on For Unix and Unix-like systems such as Linux, Solaris, macOS, and FreeBSD.
- Install using Source Code Distribution ( Platform Independent (Architecture Independent), TAR File)
- You can install using Built Distribution for Example RPM file.
Install MySQL Connector Python on Linux using Source Code Distribution. Follow the below instructions to download MySQL connector python Platform Independent TAR (tar.gz) file.
- It opens the latest version of MySQL connector python. Choose the Previous GA version from the right side if you want to install a version other than 8.0.1. you can refer to the above table to check which version is compatible with your python version.
- Select Platform independent TAR from the “Select Operating System” drop-down list. I am downloading 2.1.7 because I am using Python 3.5
Choose the TAR archive file and click on the download button. You should get the following screen, click on the start of my download.
After the download is complete, please follow the below steps to install: –
- Untar the downloaded tar.gz file. Use below command to untar.
shell>tar xzf mysql-connector-python-VERSION.tar.gz
shell> cd mysql-connector-python-VERSION
Verifying MySQL Connector Python installation on Linux
To verify the installation, use the following steps:
- On Unix-like systems, the default Connector/Python installation location is /prefix/python.VERSION/site-packages/ where prefix is the location where Python installed, and VERSION is the Python version.
- Type import mysql.connector and execute the program. If it is executed successfully mean installation completed successfully.
- Also, you can check that MySQL Connector Python installation is working and able to connect to MySQL Server by Connecting to MySQL Using MySQL Connector Python.
Download and Install MySQL Connector Python on MacOs
You can Installing MySQL Connector Python on macOS Using a Disk Image.
- Go to download MySQL Connector python for macOS from here
- Refer the above table to check which version is compatible with your python version
- Download the mysql-connector-python-8.0.11-macos10.13.dmg file. it is an architecture Independent DMG file.
- .Install the downloaded MySQL Connector Python by opening it and double-clicking the resulting .pkg file.
Verifying MySQL Connector Python installation on macOS
Check that MySQL Connector Python installation is working and able to connect to MySQL Server by Connecting to MySQL Using MySQL Connector Python.
Install MySQL Connector Python on Ubuntu
Use the following command to install MySQL connector Python on Ubuntu.
sudo apt-get install mysql-connector-python
After this run the following command.
pip install mysql-connector-python
You can replace pip with pip3 if the command fails in Python3. If the above approach doesn’t work, you can still install it on Ubuntu using the source code.
- Go to download MySQL Connector python for Ubuntu from here.
- Select the Operating system Ubuntu Linux from the drop-down.
- Select the OS version as an architecture-independent. I am selecting Ubuntu-Linux 16.04 (architecture-independent).
- You should get two entries fro DEB Package python for MySQL Connector Python (For python 2 and Python 3).
sudo dpkg -i /path_to_downloaded_deb_file
Next Steps:
To practice what you learned in this article, Please solve a Python Database Exercise project to Practice and master the Python Database operations.
Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.
About Vishal
I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter
Related Tutorial Topics:
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
- 15+ Topic-specific Exercises and Quizzes
- Each Exercise contains 10 questions
- Each Quiz contains 12-15 MCQ
mysqlclient
This project is a fork of MySQLdb1. This project adds Python 3 support and fixed many bugs.
Support
Do Not use Github Issue Tracker to ask help. OSS Maintainer is not free tech support
When your question looks relating to Python rather than MySQL:
Or when you have question about MySQL:
Install
Windows
Building mysqlclient on Windows is very hard. But there are some binary wheels you can install easily.
If binary wheels do not exist for your version of Python, it may be possible to build from source, but if this does not work, do not come asking for support. To build from source, download the MariaDB C Connector and install it. It must be installed in the default location (usually «C:\Program Files\MariaDB\MariaDB Connector C» or «C:\Program Files (x86)\MariaDB\MariaDB Connector C» for 32-bit). If you build the connector yourself or install it in a different location, set the environment variable MYSQLCLIENT_CONNECTOR before installing. Once you have the connector installed and an appropriate version of Visual Studio for your version of Python:
macOS (Homebrew)
Install MySQL and mysqlclient:
# Assume you are activating Python 3 venv $ brew install mysql pkg-config $ pip install mysqlclient
If you don’t want to install MySQL server, you can use mysql-client instead:
# Assume you are activating Python 3 venv $ brew install mysql-client pkg-config $ export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig" $ pip install mysqlclient
Linux
Note that this is a basic step. I can not support complete step for build for all environment. If you can see some error, you should fix it by yourself, or ask for support in some user forum. Don’t file a issue on the issue tracker.
You may need to install the Python 3 and MySQL development headers and libraries like so:
- $ sudo apt-get install python3-dev default-libmysqlclient-dev build-essential # Debian / Ubuntu
- % sudo yum install python3-devel mysql-devel # Red Hat / CentOS
Then you can install mysqlclient via pip now:
Customize build (POSIX)
mysqlclient uses pkg-config —clfags —ldflags mysqlclient by default for finding compiler/linker flags.
You can use MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS environment variables to customize compiler/linker options.
$ export MYSQLCLIENT_CFLAGS=`pkg-config mysqlclient --cflags` $ export MYSQLCLIENT_LDFLAGS=`pkg-config mysqlclient --libs` $ pip install mysqlclient
Documentation
Documentation is hosted on Read The Docs