How to install MySQL Connector/Python

Currently, MySQL Connector/Python is only available through Launchpad. Here’s a small how-to for installing it using the Bazaar bzr client tool. All you need is a machine with Python installed (v2.3 or higher, but not v3.x), and.. well, that’s it!

$ bzr checkout lp:~mysql/myconnpy/main myconnpy
$ cd myconnpy
$ sudo python setup.py install

Please check it out. It’s not feature complete yet, and probably can use some code optimizations here and there. I’m looking forward to bug reports! Also, only works with MySQL 4.1 and above.

Here is a little script that shows how it works, save it in file test_myconn.py:

import mysql.connector

if __name__ == "__main__":
  db = mysql.connector.

Connect(host="localhost",
    user="root",password="",database="test")
  cursor = db.cursor()
  cursor.execute("SHOW ENGINES")

  for row in cursor.fetchall():
    print row

  cursor.close()
  db.close()

Execute it like this and you should see the available storage engines:

$ python test_myconn.py
[u'InnoDB', u'YES', u'Supports transactions, row-level locking, and foreign keys', u'YES', u'YES', u'YES']
[u'MRG_MYISAM', u'YES', u'Collection of identical MyISAM tables', u'NO', u'NO', u'NO']
[u'BLACKHOLE', u'YES', u'/dev/null storage engine (anything you write to it disappears)', u'NO', u'NO', u'NO']
[u'CSV', u'YES', u'CSV storage engine', u'NO', u'NO', u'NO']
[u'MEMORY', u'YES', u'Hash based, stored in memory, useful for temporary tables', u'NO', u'NO', u'NO']
[u'FEDERATED', u'NO', u'Federated MySQL storage engine', None, None, None]
[u'ARCHIVE', u'YES', u'Archive storage engine', u'NO', u'NO', u'NO']
[u'MyISAM', u'DEFAULT', u'Default engine as of MySQL 3.23 with great performance', u'NO', u'NO', u'NO']

Comments

rgz
What makes it better to the standard MySQLdb
Geert JM Vanderkelen
I'm not going to call it better than MySQLdb, because Connector/Python is not yet complete.
However, you don't have to compile anything, but it might make it slower again.

Give it a spin :-)
rgz
So it's a pure python implementation of the mysql (partial) API?

Sorry for the late reply.
Geert JM Vanderkelen
Yup, MySQL Connector/Python is currently all written in Python. I'm planning linking against libmysqlclient as well, but the default would be to install a Python module which doesn't need compiling or anything else MySQL related installed.

I hope it will be fully compatible with libmysqlclient, it should work already for general stuff.
Iken
May just be my system acting insane but:
I get an error (Unknown MySQL server host) when executing your example code.
It appears the 'import mysql.connector' statement might have some kind of overlap with MySQLdb?

The error is the same one i've been receiving when trying to use MySQLdb across the network.


Traceback:
Traceback (most recent call last):
File “connector.py”, line 1, in
import mysql.connector
File “/home/joes/python/mysql.py”, line 9, in
db = MySQLdb.connect(host=“mailscan.wilson.local”, user=“mailadd”, passwd=“mailadd#1”, db=“mailscanner”)
File “build/bdist.linux-i686/egg/MySQLdb/init.py”, line 81, in Connect

File “build/bdist.linux-i686/egg/MySQLdb/connections.py”, line 188, in init
Geert JM Vanderkelen
@Iken: you best go to Launchpad and ask a question there (Blogs ain't for support): https://launchpad.net/myconnpy
BTW, MySQLdb.connect != mysql.connector.connect