How can I connect to MySQL in Python 3 on Windows?


I am using ActiveState Python 3 on Windows and wanted to connect to my MySQL database. I heard that mysqldb was the module to use. I can't find mysqldb for Python 3.

Is there a repository available where the binaries exist for mysqldb? How can I connect to MySQL in Python 3 on Windows?

8/18/2019 5:47:27 PM

Accepted Answer

There are currently a few options for using Python 3 with mysql:

  • Officially supported by Oracle
  • Pure python
  • A little slow
  • Not compatible with MySQLdb

  • Pure python
  • Faster than mysql-connector
  • Almost completely compatible with MySQLdb, after calling pymysql.install_as_MySQLdb()

  • fork of pymysql with optional C speedups

  • Django's recommended library.
  • Friendly fork of the original MySQLdb, hopes to merge back some day
  • The fastest implementation, as it is C based.
  • The most compatible with MySQLdb, as it is a fork
  • Debian and Ubuntu use it to provide both python-mysqldb andpython3-mysqldb packages.

benchmarks here:

9/28/2016 7:40:32 PM

You should probably use pymysql - Pure Python MySQL client instead.
It works with Python 3.x, and doesn't have any dependencies.

This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.


import pymysql
conn = pymysql.connect(host='', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow