Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

db=MySQLdb.connect(     host="localhost",     user="br_admin",     passwd="blabla",     db="br_brain" ) 

This code fails with this error:

Traceback (most recent call last):   File "<pyshell#22>", line 5, in <module>   db="brainse_brain" File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__ super(Connection, self).__init__(*args, **kwargs2) OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)") 

How do I resolve this error?

like image 276
Guru Prakash S Avatar asked Jun 20 '12 14:06

Guru Prakash S


2 Answers

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 
like image 88
chirag ghiyad Avatar answered Sep 20 '22 03:09

chirag ghiyad


I had the same trouble too, I'm working on a Windows of 64 bits, and the solution just was changing the host variable value. I had set "localhost" when the correct value have to be "127.0.0.1". However, when I'm working on Windows of 32 bits. I can set "localhost" or "127.0.0.1" in the host variable value and no matter, my django's project runs perfectly.

like image 36
user2112920 Avatar answered Sep 19 '22 03:09

user2112920