I have a small script that connects using ssh and opens a tunnel on port 3306. It works in the ipython interpreter if i type it in manually. However when I try to run the script from the interpreter or command line I get the error message "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (61)". tmp/mysql.sock does exist.

the code is below:
import os
import MySQLdb

command = '/usr/bin/ssh name@somesshserver.com -i mykey -p 1022 -L 3306:localhost:3306 -N'
pipe=os.popen(command,'r')

conn=MySQLdb.connect(host='aname',user='me',passwd='yes', db='probably', port=3306)
curs=conn.cursor()

** from the Ipython interpreter i can do a fetchall or any other mysql commands and it works fine, but if i'm running the script it stops after the 'conn=...'

I'm guessing a permissions issue although i've tried running the script with a sudo and it still fails.

This is on Mac OS 10.5.8
python 2.5.1

Was able to get this working by adding a sleep command after the ssh connect and before the mysql query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.