Home > Uncategorized > How to install MySQL Connector/Python

How to install MySQL Connector/Python

September 24th, 2009 Leave a comment Go to comments

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
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:

shell> 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']
Share
Tags: , ,
  1. rgz
    September 25th, 2009 at 22:15 | #1

    What makes it better to the standard MySQLdb

  2. Geert JM Vanderkelen
    September 26th, 2009 at 11:55 | #2

    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 :-)

  3. rgz
    September 28th, 2009 at 16:52 | #3

    So it's a pure python implementation of the mysql (partial) API?

    Sorry for the late reply.

  4. Geert JM Vanderkelen
    September 28th, 2009 at 20:19 | #4

    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.

  5. Iken
    January 19th, 2010 at 16:08 | #5

    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__

  6. Geert JM Vanderkelen
    January 20th, 2010 at 08:22 | #6

    @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

  1. No trackbacks yet.