Python - MySQL SSL Connections

Viewed 19883

I have a MySQL Server set up to use SSL and I also have the CA Certificate.

When I connect to the server using MySQL Workbench, I do not need the certificate. I can also connect to the server using Python and MySQLdb on a Mac without the CA-certificate.

But when I try to connect using the exact same setup of Python and MySQLdb on a windows machine, I get access denied. It appears that I need the CA. And when I enter the CA, I get the following error

_mysql_exceptions.OperationalError: (2026, 'SSL connection error')

My code to open the connection is below:

db = MySQLdb.connect(host="host.name",    
                 port=3306,
                 user="user",         
                 passwd="secret_password",  
                 db="database", 
                 ssl={'ca': '/path/to/ca/cert'})  

Could anyone point out what the problem is on a windows?

4 Answers

I just got the following to work with Python 2.7 and MySQLdb (1.2.4):

database = MySQLdb.connect(host='hostname', user='username', db='db_name',
    passwd='PASSWORD', ssl={'ca': '/path/to/ca-file'})

This is what you had so there must be something else going on here. I wonder if you have something either incorrect with the your local CA file or possibly the cert on the server? Can you get a copy of the CA file from the server?

I know this is a bit old but I found a way to get this to work. Use pymysql instead of MySQLdb and write the connection as:

import pymysql

conn = pymysql.connect(user = 'user', password = 'passwd'
, database = 'db', host = 'hst', ssl = {'ssl' : {'ca': 'pathtosll/something.pem'}})

The point people miss (including myself) is that ssl needs to be a dictionary containing a key 'ssl' which has another dictionary as a value with a key 'ca'. This should work for you.

import pymysql

conn = pymysql.connect(host= # your host, usually localhost,
user = # your username,
passwd = # your password,
db = #your database name ,
ssl ={'ssl': r'path of your pem file'})
Related