Author Topic: Using Mysql With Python  (Read 12394 times)

jfr

  • Newbie
  • *
  • Posts: 36
    • View Profile
Using Mysql With Python
« on: October 15, 2007, 08:02:45 pm »
I thought I'd try out the Python/PyQt/MySQL combination, but I've hit a snag that I can't handle. Any words of wisdom would be very welcome.

The machine is a C3000 with stock ROM.

I installed the Zaurus Python Image v2.4 from pyqplayer.sourceforge.net: files
python-exe_2.4.0-1_arm.ipk
python24.ex2.gz

Python runs fine, as far as I can tell.

For MySQL, I installed
mysql_3.23.49-8.7_arm.ipk
That works fine using the mysql app: as zaurus or root I can create and populate databases. No passwords set yet.

But from python, I can't connect to MySQL. I've created a test database called 'junk' and here's what happens when I try to access it.

$ python
Python 2.4 (#1, Jan  5 2005, 19:21:20)
[GCC 2.95.1 19990816 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import qt
>>> import MySQLdb
>>> ms = MySQLdb.connect(host='localhost',user='zaurus',db='junk',passwd='')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/home/QtPalmtop/lib/python2.4/site-packages/MySQLdb/__init__.py", line 64, in Connect
    return Connection(*args, **kwargs)
  File "/home/QtPalmtop/lib/python2.4/site-packages/MySQLdb/connections.py", line 109, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
>>>

The MySQL daemon is running (or the mysql app wouldn't have worked). The directory
/var/run/mysqld
is empty.
There are no error reports in dmesg.
Running as root gives the same error.

I'm totally new to Python, and I've no idea what to try next.

Cheers and TIA

John

jfr

  • Newbie
  • *
  • Posts: 36
    • View Profile
Using Mysql With Python
« Reply #1 on: October 16, 2007, 04:13:23 pm »
Apologies for bumping my own post, but after more searching I found the answer and I thought I'd post it in case anyone else should get bitten.

The Python code was looking for the connection socket as /var/run/mysqld/mysql.sock, but the directory was empty.

In /etc/mysql/my.cnf, the socket location was given as /tmp/mysql.sock and indeed that socket file exists. So the solution was to add:
unix_socket='/tmp/mysql.sock'
as an extra parameter to the MySQLdb.connect() call. Magic.

Hope that helps someone.

nilch

  • Hero Member
  • *****
  • Posts: 668
    • View Profile
    • http://
Using Mysql With Python
« Reply #2 on: October 16, 2007, 05:10:19 pm »
Thanks for putting in the solution to your own problem ..
It could help someone tomorrow ... so thanks.
New no more-C1000 / 5000D (sold my 6000 and 750) | Cacko ROM 1.23 on C1000 | 256 MB CF | 2GB PNY SD card | Socket Networker WiFi CF Card | USB Host cable from StreamlineCPUS | Mini Microphone (for voice recording) |

Lance Simms

  • Newbie
  • *
  • Posts: 1
    • View Profile
Using Mysql With Python
« Reply #3 on: January 09, 2008, 05:10:40 pm »
Quote from: nilch
Thanks for putting in the solution to your own problem ..
It could help someone tomorrow ... so thanks.

It did help me with MySQL 5.0 and MySQLdb 1.22 as well.  Thank you.