Categories
Tech

The MySQL/Python Connector Saga



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.

python

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.

  1. mod_ssl
  2. openssl
  3. openssl-dev / openssl-devel
  4. zlib
  5. 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()

Plink: Auto Accept HostKey's
The fun in compiling Apache

By Immanuel Noel

A techie at heart. Works with Adobe at Bangalore, India. Currently do DevOps. Been a part of the ColdFusion Engineering, Flash Runtime Engineering, Flash Builder Engineering teams in the past. Was a Flash Platform Evangelist, evangelizing the Adobe Flex platform. Spoke at numerous ColdFusion / Flash and Flex tech conferences. This blog is a collection of some of my strides with technology.

More on me on the home page

Leave a Reply

Your email address will not be published. Required fields are marked *