Python and MySQL : Setup without root privilege and without installing rpms

This article deals with How to setup Python-Mysql connectivity using MySQLdb libaray without root privilege and without installing any rpms.
but extracting binary files from rpm to any custom location.
This may be helpful if you are not having root privilege or couldn’t get RPMs installed because of some other reasons.

Important Points

There are important points which we need to remember for better understanding.
1. RPM is a compressed zip file which contains binary files which will be copied to corresponding directories by rpm utility during installation
2. We can uncompress (inflate) RPM file using any zip utility like 7zip.
3. Python versions need to be very specific. don’t expect 2.6 libaries to work with 2.4 python.
4. RedHat, OEL, CentOS rpms are generally compatible.
5. We can add new location from which python should import libarary.
6. Locations from which library files are loaded can be manipualated by modifying LD_LIBRARY_PATH
7. if we manually copy binary files from rpm, we may have to create symbolic links manually.

Example and Demo

Now let me show you how to do it with an example if we fire up python interpreter, we can find out what is the version of python.
in my case it is 2.4. so in my case i should use only Python 2.4 version libraries.

Next is to find out whether it is 32bit system or 64bit system
cat /etc/issue
in my case it is OEL 5.7 64bit

We can make a note of the version of Glibc

/lib/libc-2.5.so
mine is version 2.5

Step 1.

Get MySQLdb libraries for Python 2.4 and 54 bit.

I could not get a binary file from project site.
But got one binary rpm : MySQL-python-1.2.3-0.1.c1.el5.x86_64.rpm
unpacked the Libary files to newly created “pyMySQL” folder

[[email protected] /home/oracle/pyMySQL/lib64/python2.4/site-packages]r
$ ls
MySQLdb _mysql_exceptions.py _mysql_exceptions.pyc _mysql_exceptions.pyo MySQL_python-1.2.3c1-py2.4.egg-info _mysql.so

Step 2.

Get MySQL library files which are compatible with MySQLdb libraries.

If you try to use MySQLdb in your program by importing it you should get an error message with required libaries.
Downloaded : mysqlclient15-5.0.92-3.ius.el5.x86_64.rpm
unpacked and extracted all library files to a newly created “libmysql” directory in /home/oracle/

[[email protected] /home/oracle/libmysql/mysql]
$ ls -l
total 2968
lrwxrwxrwx 1 oracle oinstall 26 Feb 25 23:46 libmysqlclient_r.so.15 -> libmysqlclient_r.so.15.0.0
-rw-r--r-- 1 oracle oinstall 1518552 Sep 7 2012 libmysqlclient_r.so.15.0.0
lrwxrwxrwx 1 oracle oinstall 24 Feb 25 23:46 libmysqlclient.so.15 -> libmysqlclient.so.15.0.0
-rw-r--r-- 1 oracle oinstall 1511088 Sep 7 2012 libmysqlclient.so.15.0.0
as you can see above, soft links are created to actual files.

Step 3.
Get libssl libraries.

If you try to import MySQLdb you will get errors saying that libssl files are missing.
Downloaded : openssl-0.9.8e-12.el5_5.7.x86_64.rpm
unpacked the libraries into a “libssl” directory.
create soft links also to actual files.

[[email protected] /home/oracle/libssl]
$ ls -l
total 1656
-rw-r--r-- 1 oracle oinstall 1363952 Dec 14 2010 libcrypto.so.0.9.8e
lrwxrwxrwx 1 oracle oinstall 19 Feb 25 11:25 libcrypto.so.10 -> libcrypto.so.0.9.8e
lrwxrwxrwx 1 oracle oinstall 19 Feb 25 23:35 libcrypto.so.6 -> libcrypto.so.0.9.8e
-rw-r--r-- 1 oracle oinstall 19 Dec 14 2010 libcrypto.so.6.bk
lrwxrwxrwx 1 oracle oinstall 30 Feb 25 11:30 libpython2.6.so.1.0 -> /usr/lib64/libpython2.4.so.1.0
-rw-r--r-- 1 oracle oinstall 312344 Dec 14 2010 libssl.so.0.9.8e
lrwxrwxrwx 1 oracle oinstall 16 Feb 25 11:27 libssl.so.10 -> libssl.so.0.9.8e
lrwxrwxrwx 1 oracle oinstall 16 Feb 25 23:34 libssl.so.6 -> libssl.so.0.9.8e
-rw-r--r-- 1 oracle oinstall 16 Dec 14 2010 libssl.so.6.bk

Now we are ready to star the scripting.
But before that we need to set LD_LIBRARY_PATH to locations where library files are available.
export LD_LIBRARY_PATH=/u01/app/oracle/product/11.2.0.2/db_1/lib:/home/oracle/libssl:/home/oracle/libmysql/mysql
since python libaries are in different location we should tell python about that during the execution.
we can use sys.path.append to specify where the libary location is.

python
$ python
Python 2.4.3 (#1, Feb 24 2012, 13:04:26)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/oracle/pyMySQL/lib64/python2.4/site-packages')
>>> import _mysql
>>> db=_mysql.connect(host="ausdlltheta01.us.dell.com",port=3308,user="infuser",passwd="inf123",db="inf")
>>> db.query("""select FIELD_NAME from OPB_WIDGET_FIELD""")
>>> r=db.store_result()
r.fetch_row()
>>> r.fetch_row()
(("'DW_LD_GRP_VAL'",),)
>>>

Read More Post

STAY CONNECTED

Most Popular

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know
17 Dec

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know

If you’re heading to Singapore for a few weeks or many months, maybe for work or study or just to

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding