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?

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
check the second part of my answer -- it will be to do with the routing
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)

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.