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?
-
This is driving me crazy too ... stackoverflow.com/search?q=cannot+connect+to+mysqlajreal– ajreal2011年02月02日 06:14:12 +00:00Commented Feb 2, 2011 at 6:14
-
mind to double check the port ??? is default port 3306 or 10061?ajreal– ajreal2011年02月02日 06:15:22 +00:00Commented Feb 2, 2011 at 6:15
2 Answers 2
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.
2 Comments
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)