MySQLdb in Python: “Can't connect to MySQL server on 'localhost'”

后端 未结 6 953
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 10:15

I have installed MySQLdb for Python and I am able to import MySQLdb. Now I try to connect to the MySQL Community Server on my local machine, using this code:



        
6条回答
  •  遥遥无期
    2020-12-08 10:57

    Make sure to provide the proper host and port:

    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'yourdbname',                      
        'USER': 'root',                      
        'PASSWORD': 'your password',         
        'HOST': '127.0.0.1',                 
        'PORT': '3306',                      
    },
    

    This is my settings.py file config for my django app.

    Same for you please take host "127.0.0.1" and port "3306".

    This might solve your problem. And for python idle I've tested like...

    >>> import MySQLdb
    >>> Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="yoruname", passwd="yourpwd", db="test")
    >>> Cursor = Con.cursor()
    >>> sql = "SELECT * FROM test.testing"
    >>> Cursor.execute(sql)
    2L
    

提交回复
热议问题