#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    42
    Rep Power
    12

    Exclamation How to tell MySQLdb where mysql.sock is?


    Outisde of python I can connect to mysql from php, perl and I have phpMyAdmin running.

    my socket is located in /var/lib/mysql/mysql.sock

    when I first installed mysql the interactive mysql client would look for the socket at /tmp/mysql.sock
    I can't remember how i fixed it, but i think it was an environment variable

    enough background - my current problem is that python is also looking in /tmp/mysql.sock when I run

    >>> import MySQLdb;
    >>> db_conn = MySQLdb.connect('localhost','matt');

    i also pass the password to connect.. but none of that matters becuse I get this error:
    ----------------------
    _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
    ------------------------

    How can i inform python and MySQLdb of the mysql installation directories??

    I discovered unix_socket and called connect(...,'unix_socket='/var/lib/mysql/mysql.sock');

    is there a better way than this??
    I'm worried that I'll get other connection problems until the mysql path's are set.

    thanks in advance.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi Balzi, too be perfectly honest i have no idea, i've never had a problem connecting to MySQL/PostgreSQL/SQLite..

    Anyway I got this example from the Python Cookbook, it uses the port option aswell as host and i figured it might give you some ideas?

    Code:
    import MySQLdb
    
    # Create a connection object, then use it to create a cursor
    Con = MySQLdb.connect(host="127.0.0.1", port=3306, 
        user="joe", passwd="egf42", db="tst")
    Cursor = Con.cursor()
    
    # Execute an SQL string
    sql = "SELECT * FROM Users"
    Cursor.execute(sql)
    
    # Fetch all results from the cursor into a sequence and close the connection
    Results = Cursor.fetchall()
    Con.close()
    Good luck,
    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    42
    Rep Power
    12
    Thanks Mark,

    I've got the basics down by combing devshed and from the Python tutorials at python.org and here.

    it seems weird that python can't find the socket handle without an explicit pointer but php and perl can..

    anyway, I only have to specify it on opening the connection.. if I get no other issues I'll probably just write it off as a no-solution oddity.

    thanks again for hte speedy reply.
    nice to have helpful moderators.

    Matt
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Very welcome Balzi, glad i could help! I have to agree with you, it's very strange that python couldn't find the connection.. especially when perl and PHP can, Mmmm. Ah well if it works then fine, prob some totaly obscure system problem

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Hi, I'm Calvin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    LosAngeles, SanDiego, Houston
    Posts
    50
    Rep Power
    12
    maybe mentioning this problem to the python newsgroup would be fruitful... if indeed they can't come up with a solution, at least this problem will be noted for future versions of python or something.

    however, i have NO idea how to get in contact with those guys, but i'm sure thorough rummaging through python.org will give some leads...
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Good idea CV

    Now i cant tell you fro sure if http://www.dbforums.com/f97/ and googles comp.lang.python group are one and the same but i have my suspisions about it .. In any case give it a go if your interested.

    Mark
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    42
    Rep Power
    12
    my only reservation with posting to comp.lang.python is that no-one seems to share my problem..

    I can't tell if it occurs for other people, but then i am using Python 2.3.. so maybe it's a bug, maybe it's not.
    but, I am still hestitant to jump into the arena with the "big guys" and tell them that something they did is stuffed. Frogive my Frenench.

    course, they might not see it that way.. and I could just wear the abuse for newbieness and stupid-question-asker and general wrong place to ask flames.

    Matt
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hey i know what you mean, i had this error where i tried to send an email and it crashed the windows kernal (hense the leaning *nix). I also had python outputing a line "worker thread.." when using the socket module or one of the modules whoch use it i.e. urllib.

    Anyway I reported them on the Python bug bored and low and behold, it was just me lol.

    I dont think it's an error with Python 2.3 or the MySQLdb module, so many use them every day, the error would be all over the place, not to mention fixed , it's just one of those lil things.. you should feel special

    Have fun,
    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    8
    Rep Power
    0

    Lightbulb Solution


    I know this is an old post but I ran into the same problem and did a google search on the error string. My hope is that people running into the same problem will find this in similar manner and now will also have a fix. This page was last in a list of only four results.

    For complete documention read the file connections.py that comes with MySQLdb you can find it in the tarball of the package http://sourceforge.net/project/showf...ease_id=102893 or in your python dir: mine is /usr/lib/python2.2/site-packages/MySQLdb/ but yours will vary.

    Here is how you specify the socket to use.

    import MySQLdb
    con = MySQLdb.Connection(host='your_host', user='your_user', passwd='your_passwd', unix_socket='path_to_mysql.sock')

    You'll have to figure out the rest yourself. On my platform MySQLdb was trying to connect to /var/lib/mysql/mysql.sock but my socket is in /tmp/mysql.sock. If you can't find your socket and you're on a linux box try find / -name mysql.sock

    Best of luck
    God Bless
    -matthew
  18. #10
  19. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Why is it that whenever I have really obscure problems with things, the only right answer I find is in really old devshed threads?

    Devshed rocks

    Yeah, thread necromancy sucks, but I've spent HOURS on this already, and I googled it, and the right answer was here all along.
  20. #11
  21. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    when you search google use

    your question site:forums.devshed.com

IMN logo majestic logo threadwatch logo seochat tools logo