How to use PlanetScale with Python

Gerard Keating
Jul 21, 2022

--

Planetscale seems to be a pretty good database system but I was having problems connecting to it from Python, specifically from within a docker container that uses the python base image.

The error I was getting was:

MySQLdb.NotSupportedError: MySQL client library does not support ssl_mode specification

After much head scratching the fix is to use mysql-connector-python.

Then your code to connect will be something like:

def get_conn():
return mysql.connector.connect(
host=os.getenv("HOST"),
user=os.getenv("USERNAME"),
passwd=os.getenv("PASSWORD"),
db=os.getenv("DATABASE"),
ssl_verify_identity=True,
ssl_ca='/etc/ssl/certs/ca-certificates.crt',
)

ssl_ca path may differ on some systems but that one working on the docker python:3.8 image.

I hope this has helped some people out there.

Happy coding.

--

--

Gerard Keating

A professional Software Developer since 2007, comic book fan, husband and father http://vfxger.com