MySQL

pageicon Monday Jan 05, 2009

MySQL-Python Driver Installation and Setup

Introduction

This document describes the steps required on Solaris for writing MySQL Scripts with Python DB-API.

Python Version

Make sure you have version 2.4.3 or higher.

# which python

/usr/bin/python

# python

Python 2.4.4 (#1, Nov 19 2008, 01:54:58) [C] on sunos5

Type "help", "copyright", "credits" or "license" for more information.

>>>

Download MySQL-python driver

Go to this site

http://sourceforge.net/projects/mysql-python

Click on download link and download mysq-python 1.2.2.

Installation

#pwd

/export/home/mysql

# gzip -d MySQL-python-1.2.2.tar.gz

# tar xvf MySQL-python-1.2.2

# cd MySQL-python-1.2.2

/export/home/mysql/MySQL-python-1.2.2


Building

#pwd
/export/home/mysql/MySQL-python-1.2.2
# python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.solaris-2.11-sun4u-2.4/MySQLdb
running build_ext
building '_mysql' extension
/usr/lib/python2.4/pycc -OPT:Olimit=0 -DNDEBUG -Dversion_info=(1,2,2,'final',0) -D__version__=1.2.2 -I/usr/local/mysql/include/mysql -I/usr/include/python2.4 -c _mysql.c -o build/temp.solaris-2.11-sun4u-2.4/_mysql.o -DHAVE_RWLOCK_T -DUNIV_SOLARIS
cc: Warning: illegal option -OPT:Olimit=0
/usr/lib/python2.4/pycc -G build/temp.solaris-2.11-sun4u-2.4/_mysql.o -L/usr/local/mysql/lib/mysql -lmysqlclient_r -lz -lpthread -lthread -lgen -lsocket -lnsl -lm -lpthread -lthread -o build/lib.solaris-2.11-sun4u-2.4/_mysql.so

Installing

sudo python setup.py install
r
unning install
running bdist_egg
running egg_info
writing MySQL_python.egg-info/PKG-INFO
writing top-level names to MySQL_python.egg-info/top_level.txt
writing dependency_links to MySQL_python.egg-info/dependency_links.txt
deleting MySQL_python.egg-info/native_libs.txt
reading manifest file 'MySQL_python.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'MySQL_python.egg-info/SOURCES.txt'
installing library code to build/bdist.solaris-2.11-sun4u/egg
running install_lib
running build_py
copying MySQLdb/release.py -> build/lib.solaris-2.11-sun4u-2.4/MySQLdb
running build_ext
creating build/bdist.solaris-2.11-sun4u
.....
copying build/lib.solaris-2.11-sun4u-2.4/MySQLdb/__init__.py -> build/bdist.solaris-2.11-
creating dist..........

creating 'dist/MySQL_python-1.2.2-py2.4-solaris-2.11-sun4u.egg' and adding 'build/bdist.solaris-2.11-sun4u/egg' to it
removing 'build/bdist.solaris-2.11-sun4u/egg' (and everything under it)
Processing MySQL_python-1.2.2-py2.4-solaris-2.11-sun4u.egg
Copying MySQL_python-1.2.2-py2.4-solaris-2.11-sun4u.egg to /usr/lib/python2.4/site-packages
Adding MySQL-python 1.2.2 to easy-install.pth file

Installed /usr/lib/python2.4/site-packages/MySQL_python-1.2.2-py2.4-solaris-2.11-sun4u.egg
Processing dependencies for MySQL-python==1.2.2
Finished processing dependencies for MySQL-python==1.2.2


Testing the MySQL-Python setup

To test if MySQLdb module is installed, issue the import command by running Python in interactive mode from the command line prompt. Also, make sure to set LD_LIBRARY_PATH to contain MySQL Libs otherwise you might see import errors.


# export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql
# python

Python 2.4.4 (#1, Nov 19 2008, 01:54:58) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>

Create Simple Python DB-API script to get Mysql version


Create a file server_version.py and add the following contents.


#server_version.py - retrieve and display database server version

import MySQLdb
conn = MySQLdb.connect (host = "localhost",
                           user = "testuser",
                           passwd = "testpass",
                           db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()


Running the DB-API script


# python server_version.py
/usr/lib/python2.4/site-packages/MySQL_python-1.2.2-py2.4-solaris-2.11-sun4u.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib/python2.4/site-packages/MySQL_python-1.2.2-py2.4-solaris-2.11-sun4u.egg/_mysql.pyc, but /export/home/mysql/MySQL-python-1.2.2 is being added to sys.path
server version: 5.1.30


References

Supported Drivers, Docs, Discussion forms:

http://www.sun.com/software/products/mysql/drivers.jsp

Articles for installing drivers:

http://www.kitebird.com/articles/


Comments:

well, on solaris 10, you can avoid all the hassle of building these open source components by simply sun web stack

http://www.sun.com/webstack

Posted by Sriram Natarajan on January 09, 2009 at 06:48 PM PST #

I tried webstack and the MySql module does not seem to be installed.

/opt/webstack/python/bin/python

Python 2.5.2 (r252:60911, Dec 10 2008, 19:10:51) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: No module named _mysql

Posted by Michael Mellinger on January 19, 2009 at 08:24 AM PST #

In fact, I get this error message. I'm guess my PATH, or LD_LIBRARY_PATH is set up wrong? I want MySql 5.x libraries.

/opt/webstack/python/lib/python2.5/pycc -I/opt/webstack/wslibs/include -xarch=386 -xchip=pentium -I/opt/webstack/wslibs/lib/libffi-3.0.5/include -D__i386__ -xspace -xO4 -Xa -xildoff -xnorunpath -m32 -DNDEBUG -Dversion_info=(1,2,2,'final',0) -D__version__=1.2.2 -I/opt/mysql/mysql/include -I/opt/webstack/python/include/python2.5 -c _mysql.c -o build/temp.solaris-2.10-i86pc-2.5/_mysql.o -g -mt -fsimple=1 -ftrap=%%none -nofstore -xbuiltin=%%all -xlibmil -xlibmopt -xtarget=generic -DBIG_TABLES -DHAVE_RWLOCK_T
cc: illegal option -ftrap=%%none
error: command '/opt/webstack/python/lib/python2.5/pycc' failed with exit status 1

Posted by Michael Mellinger on January 19, 2009 at 08:30 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today

Feeds

Search this blog

Links

Weblog menu

Today's referrers

Today's Page Hits: 1