28

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?

2
  • 3
    Can you connect to the server on localhost outside of python?
    – Wooble
    Jun 20, 2012 at 14:39
  • NO.The problem is.. Im not able to figure out the host name from my cpanel. In my phpmyadmin in variables i have the hostname as "XXXXXXX"... but when I try its effectless..... Jun 21, 2012 at 7:11

7 Answers 7

41

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
3
  • 8
    Actually the problem in my case was that I was using "localhost" instead of "127.0.0.1". Anyway, the simple test you provided was a very welcome help.
    – Kaniabi
    Nov 27, 2012 at 18:45
  • In my case, I accidently supplied the username as the password. Check that all your credentials are correct (instead of assuming they are). Mar 16, 2015 at 13:48
  • Documentation for mysqldb says it defaults to 3306 however at least as of 1.2.3rc1 under python 2.6 I can't connect remotely unless explicitly setting port to 3306.
    – cs_alumnus
    Oct 10, 2015 at 20:13
6

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.

0
3

This will work fine :

    db = MySQLdb.connect(host="127.0.0.1",user="db_username",passwd="db_password",db="db_name") 

or

   db=  MySQLdb.connect("127.0.0.1","db_username","db_password","db_name")
2

In Windows 32, if you set host as 127.0.01 it gives the down error:

OperationalError: (2005, "Unknown MySQL server host '127.0.01' (0)")

But if you set host as 127.0.0.1 then you get no error.

1
  • 2
    That's because 127.0.01 isn't a valid IP address.
    – Nick Burns
    Feb 24, 2016 at 20:47
2

If you are using windows you should specify the IP to "127.0.0.1", using "localhost" will give you that error 2003. On Ubuntu I had no problem.

2
  • couldn't get when i give 127.0.01. But got by giving host as 127.0.0.1 Feb 24, 2016 at 5:12
  • 1
    Of course, that's a typo. IP address consists of 4 (1-byte) integers. Feb 24, 2016 at 20:40
0

1.Go to MySQL workbench
2.On the task bar above, click on server->and then click on Startup/Shutdown
3.On the screen, click on Start the server and you will get logs of server starting and running

0
 - DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.mysql',
           'NAME': 'dataflair',
           'USER':'root',
           'PASSWORD':'',
           'HOST':'127.0.0.1',
           'PORT':'3306',
           'OPTIONS':{
       'init_command':"SET sql_mode=STRICT_TRANS_TABLES" }
1
  • 1
    Welcome to StackOverflow. While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Jun 21, 2021 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.