Here's how I set up unixODBC for mySQL to work with the pyodbc module
The ubuntu package to install, is naturally
libmyodbc
and I think you need these too, to compile pyodbc:
libmysqlclient-dev
libmysqld-dev
pyodbc-2.1.6.zip is best sourced from
http://code.google.com/p/pyodbc/
where some documentation and advice can also be found.
Once unzipped, installation includes a compile step
% python setup.py build
% python setup.py install
And the module will import!
You'll need to know the socket, for the connection string. The socket can be found in the config file
/etc/mysql/my.cnf, on ubuntu:
socket = /var/run/mysqld/mysqld.sock
Or to see it just run
% mysqladmin -u root -p version
as mentioned in
http://code.google.com/p/pyodbc/wiki/ConnectionStrings
But the connection string suggested there won't work. And the documentation at
http://www.unixodbc.org/ is spotty. You'll get this error until you create the odbc config files:
IM002:1:0:[unixODBC][Driver Manager]Data source name not found, and no default driver specified
For system DSNs you'll need these files.
_____________________
/etc/odbc.ini
[myDSN]
Description = myodbc_driver
Driver = myodbc_driver
Server =
Database =
Port =
Socket =
Option =
Stmt =
___________________
/etc/odbcinst.ini
[myodbc_driver]
Description =
Driver = /usr/lib/odbc/libmyodbc.so
Driver64 = /usr/lib
Setup = /usr/lib/odbc/libodbcmyS.so
Setup64 = /usr/lib
UsageCount = 1
CPTimeout =
CPReuse =
(Doing this with the conventional ODBCConfig graphical tool offers nothing more than a form-based editor, with little guidance for what to do.)
Finally you should be able to use this connection string:
cn = pyodbc.connect('DRIVER={myodbc_driver};DATABASE=my _db;UID=my_user;PWD=my_pw;SOCKET=/var/run/mysqld/mysqld.sock')
Arguably by filling out the DSN details in odbc.ini the connection string need only refer to the DSN by name, but I haven't tried it.
Bookmarks