python mysql 连接问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1164033/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:37:11  来源:igfitidea点击:

python mysql connection problem

pythonmysql

提问by Blauohr

This code works fine:

这段代码工作正常:

import  MySQLdb

db = MySQLdb.connect("localhost", "root", "","bullsorbit")
cursor = db.cursor()

cursor.execute("Select * from  table  where  conditions'")
numrows = int(cursor.rowcount)

print 'Total number of Pages :  %d ' % numrows

but if I give my IP address

但如果我提供我的 IP 地址

db = MySQLdb.connect("192.168.*.*", "root", "","bullsorbit")

it will give this error

它会给出这个错误

super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'ip address' (111)")

回答by

Code 2003 is a standard MySQL client error:

代码 2003 是标准的 MySQL 客户端错误

Error: 2003 (CR_CONN_HOST_ERROR) Message: Can't connect to MySQL server on '%s' (%d)

错误:2003 (CR_CONN_HOST_ERROR) 消息:无法连接到“%s”(%d) 上的 MySQL 服务器

I'd guess that your user is not allowed to connect to the MySQL server using an iP-address. What happens if you try a connection usign the MySQL commandline client?

我猜您的用户不允许使用 iP 地址连接到 MySQL 服务器。如果您尝试使用 MySQL 命令行客户端进行连接会发生什么?

$ mysql --host=192.168.1.1 -u root bullsorbit

$ mysql --host=192.168.1.1 -u root Bullsorbit

回答by Blauohr

with localhost you connect via loopback-interface.

使用 localhost 通过环回接口连接。

with ip-addr you connect - as you connect from extern.

使用 ip-addr 连接 - 当您从外部连接时。

if your server allows only the local (loopback) connection your ip-addr connection fails. (security!)

如果您的服务器只允许本地(环回)连接,您的 ip-addr 连接就会失败。(安全!)

look at your mysql config, maybe only local-connections are allowed.

看看你的 mysql 配置,也许只允许本地连接。

is the skip-networking switch off (in the mysql-conf) ?

跳过网络关闭(在 mysql-conf 中)?

#skip-networking

回答by unmounted

Instead of:

代替:

db = MySQLdb.connect("192.168..", "root", "","bullsorbit")

db = MySQLdb.connect("192.168..", "root", "","bullsorbit")

try:

尝试:

db = MySQLdb.connect(user="root", host="192.168..", db="bullsorbit")

db = MySQLdb.connect(user="root", host="192.168..", db="bullsorbit")

Password will default to empty string and try the usual identity methods. Host will also default to localhost on default port.

密码将默认为空字符串并尝试通常的身份方法。主机也将默认为默认端口上的 localhost。

回答by demongolem

For the 2003error, another possibility is that too many connections are being attempted in too short of time. I noticed this same behavior when I had 127.0.0.1as my connect string. First I replaced it with localhostwhich got me further but still the same 2003error. Then I saw that if I scaled back the number of calls the error disappeared completely.

对于该2003错误,另一种可能是在太短的时间内尝试了过多的连接。当我127.0.0.1作为连接字符串时,我注意到了同样的行为。首先,我用它替换了它,localhost这让我走得更远,但仍然是同样的2003错误。然后我看到,如果我减少调用次数,错误就会完全消失。