Menu

Trouble connecting, but only through python

Help
RafeS
2007-12-25
2012-09-19
  • RafeS

    RafeS - 2007-12-25

    Hello,

    This is my first post, so bear with me... (how do I post clean code blocks, for example?)

    I'm fairly new to python, and extremely new to MySQL. I am starting with the book "MySQL Cookbook". I have a server running. I have a basic database and am able to interact with it just fine. When I attempted the most basic script however, things fall apart for me.

    Using...
    Python 2.5.1
    ՄեՍԽԼ 5.1.22 rc-win32 (on Windows XP)

    Simple example (python shell)...
    >>> Import MySQLdb
    >>> db = MySQLdb.connect(db='cookbook', host='localhost', user='USERNAME', passwd='PASSWORD')
    Traceback (most recent call last):
    File "<console>", line 0, in <module>
    File "C:\Python25\Lib\site-packages\MySQLdb__init.py", line 74, in Connect
    return Connection(args, *kwargs)
    File "C:\Python25\Lib\site-packages\MySQLdb\connections.py", line 170, in
    init
    super(Connection, self).
    init__(args, *kwargs2)
    OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
    db = MySQLdb.connect()

    More complex example (trying to wrap in to an object)...

    Import

    import sys, logging
    import MySQLdb

    logging.basicConfig()
    _oLog = logging.getLogger()

    CLASS

    class TestSQL(object):

    # Variables
    _oConn = None;
    
    # Methods
    def Connect(self, sDb, sHost, sUser, sPass):
        _oLog.debug(&quot;Connecting to server...&quot;)
    
        try:
            _oConn = MySQLdb.connect( db     = sDb,
                                      host   = sHost,
                                      user   = sUser,
                                      passwd = sPass )
        except MySQLdb.Error, err:
            sMsg = &quot;Cannnot connect to server.&quot;
            _oLog.exception( str(err.args[0]) + &quot;, &quot; + err.args[1] )
            sys.exit(1)
            raise MySQLdb.Error
    
        _oConn.close()
        _oLog.debug(&quot;Disconnected from server.&quot;)
    

    if name == "main":
    oSQL = TestSQL()
    oSQL.Connect( "cookbook", "localhost", "USERNAME", "PASSWORD" )

    Result:

    ERROR:root:2003, Can't connect to MySQL server on 'localhost' (10061)
    Traceback (most recent call last):
    File "D:\Work\IFW\Dev\Global\lib\cookbookConn.py", line 25, in Connect
    passwd = sPass )
    File "C:\Python25\Lib\site-packages\MySQLdb__init.py", line 74, in Connect
    return Connection(args, *kwargs)
    File "C:\Python25\Lib\site-packages\MySQLdb\connections.py", line 170, in
    init
    super(Connection, self).
    init__(args, *kwargs2)
    OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

     
    • RafeS

      RafeS - 2007-12-25

      Sorry about the formatting. Help would be appreciated there. I would be happy to re-post.

      Also,
      ՄեՍԽԼ 5.1.22 rc-win32 (on Windows XP)

      Should have been:
      MySQL 5.1.22 rc-win32 (on Windows XP)

      • Rafe
       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.