简体   繁体   English

MySQL-python无法连接到服务器

[英]MySQL-python Cannot connect to server

This is driving me crazy. 这真让我抓狂。

I have Python 2.5 and MySQL-python-1.2.3 from the .exe available here installed on Vista. 我的Python 2.5,并从可用的。exe的MySQL的Python-1.2.3 这里在Vista上安装。

I have been making .php pages over the past few weeks and connected fine in order to test them in my browser. 我过去几周一直在制作.php页面并且连接正常,以便在我的浏览器中测试它们。

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

I have also been using mysql commandline with 我也一直在使用mysql命令行

mysql.exe -uroot -pmypassword just fine.

However, when I try to use MySQLdb with 但是,当我尝试使用MySQLdb时

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. 我已经检查过MySQL在服务中运行。 I checked my MySQL config in my.ini and it is running on port=3306. 我在my.ini中检查了我的MySQL配置,它在port = 3306上运行。 I have even uninstalled and reinstalled MySQL 5.1. 我甚至卸载并重新安装了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? 它一定是我忽略的简单,但有没有人有更多的想法?

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 . 不要使用Windows版本(我所有的相关经验都在Linux上),但是如果你使用127.0.0.1代替localhost ,我愿意打赌它会工作。

Edit: -- a bit of explanation. 编辑: - 一点解释。

Firstly, check the entries in mysql . 首先,检查mysql的条目。 user table, for the host field (although by default, you probably have three entries for root which cover all bases). user表,用于host字段(尽管默认情况下,您可能有三个用于root的条目,涵盖所有基础)。 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. 最有可能的是,您的问题是由于MySQL服务正在侦听127.0.0.1:3306,而不是localhost:3306,而且您的hosts文件或其他路由配置未被python解释器正确调用。

Make sure you have opened the TCP port 3306 in your firewall for connections. 确保已在防火墙中打开TCP端口3306以进行连接。

Also see this question Can't connect to MySQL server on 'localhost' (10061) 另请参阅此问题无法连接到'localhost'上的MySQL服务器(10061)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM