Python 中的 MySQLdb:“无法连接到 'localhost' 上的 MySQL 服务器”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11121819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQLdb in Python: "Can't connect to MySQL server on 'localhost'"
提问by Guru Prakash S
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:
我已经为 Python 安装了 MySQLdb,并且可以导入 MySQLdb。现在,我尝试使用以下代码连接到本地计算机上的 MySQL 社区服务器:
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?
如何解决此错误?
回答by chirag ghiyad
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.
这是我的 django 应用程序的 settings.py 文件配置。
Same for you please take host "127.0.0.1" and port "3306".
同样,请使用主机“127.0.0.1”和端口“3306”。
This might solve your problem. And for python idle I've tested like...
这可能会解决您的问题。对于 python idle 我已经测试过......
>>> 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
回答by user2112920
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.
我也遇到了同样的问题,我在 64 位的 Windows 上工作,而解决方案只是更改主机变量值。当正确的值必须是“127.0.0.1”时,我设置了“localhost”。但是,当我在 32 位的 Windows 上工作时。我可以在宿主变量值中设置“localhost”或“127.0.0.1”,无论如何,我的 django 项目运行完美。
回答by dinesh.kumar
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")
回答by Saher Ahwal
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.
如果您使用的是 Windows,您应该将 IP 指定为“127.0.0.1”,使用“localhost”会给您错误 2003。在 Ubuntu 上我没有问题。
回答by pius
In Windows 32, if you set host as 127.0.01
it gives the down error:
在 Windows 32 中,如果您将主机设置为127.0.01
它会给出停机错误:
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.
但是如果你将主机设置为127.0.0.1
那么你就不会出错。
回答by Sandeep Amarnath
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
1.进入MySQL工作台
2.在上面的任务栏上,点击服务器->然后点击启动/关闭
3.在屏幕上,点击启动服务器,你会得到服务器启动和运行的日志