WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: MySQLDB Python Extension  (Read 4127 times)

Offline KHarvey

  • Full Member
  • ***
  • Posts: 102
MySQLDB Python Extension
« on: December 29, 2011, 12:01:51 PM »
The MySQLDB Python extension located at http://sourceforge.net/projects/mysql-python is used for connecting Python scripts to MySQL.

I have been playing with this for a couple of hours trying to get it installed and working.
So far the dependencies I have found are:
MySQL.tcz
Python-2.6.tcz
compiletc.tcz  (May be only required for installation)
python-dev.tcz (May be only required for installation)
mysql-dev.tcz (May be only required for installation)
Pythonsetuptools-2.7.tcz (May be only required for installation)

Before the install I found that I had to change the setup_posix.py (line 26) to be:
mysql_config.path = "/usr/local/mysql/bin/mysql_config"

I still don't have it working, but even if I get it working, I probably won't have persistence with it, so it would be really nice if it could be created as an extension.

Offline KHarvey

  • Full Member
  • ***
  • Posts: 102
Re: MySQLDB Python Extension
« Reply #1 on: January 11, 2012, 05:35:14 PM »
I just started playing with this again, and I think I made a little bit more progress but I am stuck.

I was going to try and submit this as an extension myself, but I got lost trying to follow the build instructions.  The extension creation wiki says to use make, but this already appears to be partly built, and I just run python scripts to install.

The dependencies I listed above appear to be correct.

Here are the steps that I followed:
Download the tar.gz from site listed above
tar xvf MySQL-python-1.2.3.tar.gz
edit setup_posix.py
mysql_config.path = "/usr/local/mysql/bin/mysql_config"

It is at this point that I have hit a cross roads.  If I run the python setup.py build then it will build fine, but when I do the install I am unable to import MySQLDB as I get a
Quote
ImportError: libmysqlclient_r.so.14: cannot open shared object file: No such file or directory

After some searching I found the solution to this problem to be to edit the site.cfg and change static = True
But then when I do the python setup.py build I receive a gcc error
Quote
gcc: error: /tmp/tcloop/mysql/usr/local/mysql/lib/libmysqlclient_r.a:  No such file or directory
When I compared the gcc strings that were being used I found the error.  It is trying to use /tmp/tcloop/mysql/usr/local/mysql/lib/libmysqlclient_r.a but that file does not exist.  The file does exist in /tmp/tcloop/mysql-dev/usr/local/mysql/lib/libmysqlclient_r.a
And this is where I am stuck at.  I have been digging through the python setup files trying to find if I can change the location it looks for the libmysqlclient_r.a but so far I haven't been able to find it.  Then I tried copying the files from the mysql-dev to the mysql in /tmp/tcloop and I learned that they are read-only.

I am going to continue to dig through the python setup files and see if I can hunt down what to change.  But any help or suggestions would be greatly appreciated.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11590
Re: MySQLDB Python Extension
« Reply #2 on: January 11, 2012, 08:51:27 PM »
Hi KHarvey
I think you should set static back to False, it will make for a smaller build.
If you are building under TC3.x, the library is under  usr/local/lib/mysql/libmysqlclient.so.16
If you are building under TC4.x, the library is under  usr/local/mysql/lib/libmysqlclient.so.18
Note that  lib  and  mysql  in the path are swapped between the two versions.
« Last Edit: January 11, 2012, 08:56:27 PM by Rich »

Offline KHarvey

  • Full Member
  • ***
  • Posts: 102
Re: MySQLDB Python Extension
« Reply #3 on: January 13, 2012, 05:21:45 PM »
Thanks for the reply Rich.

I finally was able to load the MySQLdb module with the libmysqlclient, but now I have a new issue.

First off these are the steps that I did to get it to work:
Code: [Select]
Download MySQLdb
tar xvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
edit setup_posix.py
  mysql_config.path = "/usr/local/mysql/bin/mysql_config"
mkdir ~/Downloads/MySQL-python-1.2.3/build
mkdir ~/Downloads/MySQL-python-1.2.3/build/lib.linux-i686-2.7
mkdir ~/Downloads/MySQL-python-1.2.3/build/lib.linux-i686-2.7/MySQLdb
mkdir ~/Downloads/MySQL-python-1.2.3/build/lib.linux-i686-2.7/MySQLdb/constants
mkdir ~/Downloads/MySQL-python-1.2.3/build/temp.linux-i686-2.7
cp ~/Downloads/MySQL-python-1.2.3/MySQLdb/*.py ~/Downloads/MySQL-python-1.2.3/build/lib.linux-i686-2.7/MySQLdb/
cp ~/Downloads/MySQL-python-1.2.3/MySQLdb/constants/*.py ~/Downloads/MySQL-python-1.2.3/build/lib.linux-i686-2.7/MySQLdb/constants/
gcc -pthread -fno-strict-aliasing -march=i486 -mtune=i686 -O3 -pipe -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -D__version__=1.2.3 -I/usr/local/mysql/include -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-i686-2.7/_mysql.o -Os -pipe -g
gcc -pthread -shared build/temp.linux-i686-2.7/_mysql.o -L/tmp/tcloop/mysql-dev/usr/local/mysql/lib -L/usr/local/lib -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -lpython2.7 -o build/lib.linux-i686-2.7/_mysql.so

Now I originally tried the /usr/local/mysql/lib but it still failed when I did an ldd of _mysql.so.  But when I changed it to /tmp/tcloop/mysql-dev/usr/local/mysql/lib the ldd finds the libmysql

Code: [Select]
linux-gate.so.1 =>  (0xb773e000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb7465000)
libz.so.1 => /usr/lib/libz.so.1 (0xb7454000)
libm.so.6 => /lib/libm.so.6 (0xb7431000)
librt.so.1 => /lib/librt.so.1 (0xb7429000)
libdl.so.2 => /lib/libdl.so.2 (0xb7424000)
libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0xb7114000)
libc.so.6 => /lib/libc.so.6 (0xb7009000)
/lib/ld-linux.so.2 (0xb773f000)
libutil.so.1 => /lib/libutil.so.1 (0xb7005000)

But now when I go into Python and do an import of either MySQLdb or _mysql it get a "segmentation fault".  At this point I am really stumped, I don't have a clue where to go from here.
Not sure if I should bring this up in another forum or not.  Also I am running TC 4.2

If I get this to work I think that I vaguely understand (I doubt it though) how to turn this into an extensions.
Anyone have any clues of what I can try next?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11590
Re: MySQLDB Python Extension
« Reply #4 on: January 14, 2012, 01:14:04 AM »
Hi KHarvey
Quote
Now I originally tried the /usr/local/mysql/lib but it still failed when I did an ldd of _mysql.so.  But when I changed it to /tmp/tcloop/mysql-dev/usr/local/mysql/lib the ldd finds the libmysql
Well, that suggests a couple of things:
The links from  /usr/local/mysql/lib  to  /tmp/tcloop/mysql-dev/usr/local/mysql/lib  might not exist or
may point to the wrong location.
You might still be doing a  static  build since the mysql-dev extension contains the static libraries
while the shared libraries are in mysql.
While probably not the source of any problems, your first gcc command has 3 optimization flags (-o),
2 pipe flags, and 2 debugging flags (-g) listed. Regardless, I would straighten that out, use -oS for
the optimization flag.
Quote
But now when I go into Python and do an import of either MySQLdb or _mysql it get a "segmentation fault".  At this point I am really stumped, I don't have a clue where to go from here.
I'm not into fads like Python, and can't help you there, though I would ask, are you sure the
segmentation fault is being caused by  _mysql, and not by a bad pointer from Python (does Python
use pointers)?
Maybe someone more experienced will see this thread and offer some advice.