MySQL-drivers (Python)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Conclusie: PyMySQL bevalt.

Vereisten

Python 3

Als het niet werkt in Python 3, dan hoef ik het niet. Dat geldt voor alle overige eisen, ook bv. voor SQLAlchemy.

Geschikt voor SQLAlchemy

Ik wil SQLAlchemy gebruiken als ORM (jan. 2019). Deze voorbeeldcode laat zien welke drivers in ieder geval ondersteund worden (ik kan me trouwens goed voorstellen dat het driver-onafhankelijk is):

# MySQLdb
engine = create_engine('mysql://scott:tiger@localhost/foo')

# mysql-python
engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')

# MySQL-connector-python
engine = create_engine('mysql+mysqlconnector://scott:tiger@localhost/foo')

# OurSQL
engine = create_engine('mysql+oursql://scott:tiger@localhost/foo')

Geschikt voor dashboarding

Eind 2020 ben ik me aan het oriënteren op de mogelijkheden om zelf in Python een dashboard te maken. Dat behelst connecties met een dwh en met meerdere webwinkels.

Betrouwbaar

Uit de evaluaties hieronder blijkt, dat connectors lang niet altijd even betrouwbaar zijn. Heel onaantrekkelijk.

Actief ondersteund

Dit is een broertje van betrouwbaar: Belangrijk dat de connector actief ondersteund wordt door ontwikkelaars.

Snel

Snelheid is zeker belangrijk.

Open-source

Ik vind het prettig als het open-source-code betreft.

Geen C?

Ik heb de indruk dat je een paar beperkingen hebt, als een connector in C geschreven is, itt. Python. Aan de andere kant, het lijkt me dat een implementatie in C een stuk sneller is.

Long list

Er bestaan diverse MySQL-drivers. Welke is goed genoeg? Waarom dit geen non-issue is: Installatie wil maar niet goed lukken (begin 2019). Vandaar dat het de moeite waard is, om effe wat langer bij deze vraag stil te staan.

MySQLdb

  • Veel-gebruikt, maar schijnt complicaties te geven tijdens installatie ivm. externe afhankelijkheden [1]
  • Schijnt dood te zijn (bron uit 2016)
  • https://pypi.org/project/MySQL-python
  • Er is nog geen P3-versie beschikbaar (dec. 2020) - Lijkt inderdaad nogal dood te zijn.

MySQL-Connector-Python

  • Veelbelovend, 100% Python (dus bv. geen C-bibliotheken), en afkomstig van Oracle zelf
  • Python 3.x wordt ondersteund
  • "MySQL-connector-Python is a pure Python MySQL driver, and is now released under the auspices of the MySQL project itself, as owned by Oracle. MySQL-connector-Python supports both Python 3 as well as eventlet monkeypatching, and is well maintained. It is endorsed by Oracle as the official Oracle-supported driver for MySQL, so to that extent, it is in most ways Openstack's first choice in driver. However, Oracle refuses to publish MySQL-connector-Python on Pypi, which is critical to the Openstack infrastructure. Repeated attempts to communicate with Oracle in order to resolve this issue have not made any progress. Therefore, for this one unfortunate reason, MySQL-connector-Python will not have a place in the Openstack ecosystem unless this issue is resolved" [2]
  • "The problem with using Oracle's Connector/Python is that it has subtle bugs and other integration issues. It's easy to install, but nearly impossible to get to work for all the real-world use cases I've tried it for. Hence why I always recommend MySQLdb" [3] - Let op: Dit komt uit 2015 en heeft vermoedelijk betrekking op Python 2
  • https://dev.mysql.com/downloads/connector/python/ MySQL Connector/Python

MySQL-Python

mysqlclient

PyMySQL

  • Geschikt voor Python 3.x
  • Dit zou de opvolger zijn van MySQLdb
  • "PyMySQL is a pure Python MySQL driver, first written as a rough port of the MySQL-Python driver. PyMySQL meets all of Openstack's criterion for a driver: it is fully open source, hosted on Github, released on Pypi, is actively maintained, is written in pure Python so is eventlet-monkeypatch compatible, and is fully Python 3 compatible. As this document is named "PyMySQL Evaluation", it should be apparent that this is the driver Openstack is currently leaning towards; because it is the only one that meets all criteria fully, it is already most likely the "winner". However, it does have some minor code quality issues which hopefully can be addressed in some way; the section below titled "PyMySQL Code Review" will summarize the current state of the code. " [7]
  • https://github.com/PyMySQL/PyMySQL

Nadeel: De huidige versie (dec. 2020) is 0.10.1 - Da's niet erg geruststellend.

OurSQL

Storm

https://storm.canonical.com

Shortlist

Een aantal drivers die hiervoor genoemd worden, vallen gelijk al af. Dit is wat er overblijft. Deze selectie was in jan. 2019 en dec. 2020 dezelfde (in de tussentijd is dit artikel amper uitgebreid met nieuwe informatie - Dat speelt vast een rol):

MySQL-Connector-Python

Voldoet aan de belangrijkste eisen. Wat betrouwbaarheid betreft: De opmerking hierboven over 'subtiele fouten', komt uit 2015 en de schrijver suggereert een connector die niet bestaat voor Python 3.

mysqlclient

Scoort goed én is open-source. Ik heb er alleen niet veel info over.

PyMySQL

Sterk op veel aspecten, behalve versienummer.

Selectie

Wanneer heb ik wat gekozen? En waarom?

MySQL-Connector-Python (okt. 2018)

Er is een interessante discussie over wat de beste driver is. Die discussie lijkt zich toe te spitsen op Mysqlclient vs. MySQL-Connector-Python [10], [11] - Ik kies deze laatste.

PyMySQL (dec. 2020)

Op gevoel:

  1. Keuze is gevallen op PyMySQL
  2. Tweede keuze: MySQL-Connector-Python
  3. Derde keuze: mysqlclient - Ik vind de naam vooral heel stom, want identiek aan de gebruikelijke command line-client voor MySQL.

Installatie

MySQL-Connector (okt. 2018 - Python 2.x)

  • Downloaden naar buroblad
  • Dubbelklikken
  • Installeren
  • Klaar.

mysqlclient (jan. 2019 - 3.x - Niet gelukt)

[12]:

sudo apt install python3-dev   # OK
sudo apt install default-libmysqlclient-dev   # Package kon niet gevonden worden

pip install mysqlclient   # Pakket niet gevonden
cd /home/strompf/.local/bin & sudo ./pip install mysqlclient   # Pakket niet gevonden
sudo apt install mysqlclient   # Niet gevonden

MySQL-Connector-Python (jan. 2019 - 3.x)

[13]:

Download: Ah, de eerste versie is waarschijnlijk voor Python 3 → Gedwonload naar buroblad
Installatie: Gewoon dubbelklikken - Inderdaad Python 3 → Klaar
Bestanden

MySQL-Connector-Python (okt. 2019 - 3.x)

  • Python: Versie 3.7.4 geïnstalleerd, naast versie 3.5.2
  • OS: Linux Mint 18.3 Sylvia - Gebaseerd op Ubuntu 16.04.

[14]:

Linux Mint is gebaseerd op Ubuntu 16.04. Dus dit zijn de vier relevante downloads. Ik kies de een-na-bovenste: Python3 + 64bit
Ondertussen had ik Python 3.7.4 geïnstalleerd naast Python 3.5, en blijkbaar is dat afdoende (geen foutmelding)
Gelukt!

PyMySQL (dec. 2020)

Soms is het heel simpel:

pip3 install PyMySQL

Connectie tot stand brengen

MySQL-Connector-Python (okt. 2019 - 3.x)

Gelukt:

#! /usr/bin/python3
#
# Just get MySQL-Connector-Python to work
#
#####################################################################################
# Load libraries
#####################################################################################
#
import mysql.connector   # Lukt!


#####################################################################################
# Connect with MySQL-db using MySQL-Connector-Python
#####################################################################################
#
# * Dit staat los van sqlalchemy
# * Lukt helaas niet zonder wachtwoord
#
cnx = mysql.connector.connect(user="gebruiker", password="password", database="example_com")
  • Maakt niet uit of de shebang python3 of python3.7 aangeeft

PyMySQL (dec. 2020)

Fluitje van een cent, dankzij dit voorbeeld. Ik gebruikte geen shebang:

import pymysql.cursors

connection = pymysql.connect(
	host='localhost',
	user='supervrouw',
	password='wachtwoord')

try:
	with connection.cursor() as cursor:
		sql="show databases"
		cursor.execute(sql)
		result=cursor.fetchall()
		print(result)
finally:
	connection.close()

Zie ook

Bronnen