This post is written with Python 3.4 in consideration, and may, or may not apply to Python 2.x
Numerous blogs / forums suggest umteen non-working solutions to get the MySQL Connector/Python to work. All I got out of those, is a wasted day, and this small little comment by ACyclic on StackOverFlow.
A prerequisite for MySQL Connector/Python to work seamlessly on linux machines, is to have the packages (but not limited to) SSL and ZLIB. Installing the connector itself does not throw errors, but importing mysql.connector does. It would ideally error out with exceptions like, “ImportError: No module named HTTPSConnection” or “ImportError: No module named zlib”. Now, only one of these errors will come up at a time, and you would need to recompile Python for subsequent attempts.
Experimenting with Python command line parameters, such as –with-ssl, –with-zlib, etc., will not work since Python does not depend on any such parameters, but instead, is intelligent enough to automatically install packages if support exist i.e., automatically installs SSL modules if the SSL packages are found on the machine. This check exists in the setup.py file.
The Solution is to have the below packages installed before compiling Python.
- openssl-dev / openssl-devel
- zlib-dev / zlib-devel
To verify package dependencies are met, search the setup.py file for the missing module, and the subsequent code will likely tell you how Python determines if a package is installed.
Here’s a small piece of code to help you test the connector installation.
import mysql.connector cnx = mysql.connector.connect(user='username', password='password', host='localhost', database='dbname') cursor = cnx.cursor() select = "SELECT * FROM someTable" cursor.execute(select) for row in cursor: print(row) cnx.close()