简体   繁体   English

python mysql连接问题

[英]python mysql connection problem

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)")

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. 我猜您的用户不允许使用iP地址连接到MySQL服务器。 What happens if you try a connection usign the MySQL commandline client? 如果尝试使用MySQL命令行客户端进行连接会怎样?

$ mysql --host=192.168.1.1 -u root bullsorbit $ mysql --host = 192.168.1.1 -u root bullsorbit

with localhost you connect via loopback-interface. 与本地主机通过回送接口连接。

with ip-addr you connect - as you connect from extern. 使用ip-addr连接-从extern连接时。

if your server allows only the local (loopback) connection your ip-addr connection fails. 如果您的服务器仅允许本地(环回)连接,则ip-addr连接失败。 (security!) (安全!)

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

Instead of: 代替:

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

try: 尝试:

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。

For the 2003 error, another possibility is that too many connections are being attempted in too short of time. 对于2003错误,另一种可能性是在太短的时间内尝试了太多的连接。 I noticed this same behavior when I had 127.0.0.1 as my connect string. 当我将127.0.0.1作为连接字符串时,我注意到了相同的行为。 First I replaced it with localhost which got me further but still the same 2003 error. 首先,我用localhost替换了它,这使我更进一步,但仍然是2003错误。 Then I saw that if I scaled back the number of calls the error disappeared completely. 然后我看到,如果我减少呼叫数量,错误将完全消失。

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

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