7

This is driving me crazy.

I have Python 2.5 and MySQL-python-1.2.3 from the .exe available here installed on Vista.

I have been making .php pages over the past few weeks and connected fine in order to test them in my browser.

$dbcnx=@mysql_connect("localhost", "root", "mypassword")

I have also been using mysql commandline with

mysql.exe -uroot -pmypassword just fine.

However, when I try to use MySQLdb with

conn = MySQLdb.connect(host = "localhost", user = "root", passwd = "mypassword") 

I get

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

I have checked that MySQL is running in services. I checked my MySQL config in my.ini and it is running on port=3306. I have even uninstalled and reinstalled MySQL 5.1. I read several pages of connection problems and answers on Google, but haven't yielded anything. It must be something simple I am overlooking, but does anyone have any more ideas?

joaquin
  • 82,968
  • 29
  • 138
  • 152
Amy
  • 85
  • 2
  • 6

2 Answers2

15

Don't use the windows version (all my relevant experience is on Linux), but I'd be willing to bet it will work if you use 127.0.0.1 in place of localhost.

Edit: -- a bit of explanation.

Firstly, check the entries in mysql.user table, for the host field (although by default, you probably have three entries for root which cover all bases). Most likely, your problem is caused by the fact that the MySQL service is listening on 127.0.0.1:3306, not localhost:3306, and your hosts file or other routing config is not being invoked properly by the python interpreter.

simon
  • 15,344
  • 5
  • 45
  • 67
  • WAH. I could have sworn I read of places where people said to change *to* using "localhost" from an address, so I never tried changing it to 127.0.0.1. However, this seems to have worked! (At least now it's returning an error with my MySQL syntax itself which I'll have to figure out.) I'm not exactly sure why it worked though, because looking at the mysql.user table, selecting host just gives back localhost. Thank you so very much! I should have asked earlier and saved myself 3 hours. – Amy Feb 02 '11 at 06:45
  • check the second part of my answer -- it will be to do with the routing – simon Feb 02 '11 at 06:54
0

Make sure you have opened the TCP port 3306 in your firewall for connections.

Also see this question Can't connect to MySQL server on 'localhost' (10061)

Community
  • 1
  • 1
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Ah, I should have said I even went as far as disabling my firewall completely in an attempt to get this to work as well. Thank you for the suggestion though. – Amy Feb 02 '11 at 06:48