rajaneesh
rajaneesh

Reputation:

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

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

Upvotes: 1

Views: 10661

Answers (4)

demongolem
demongolem

Reputation: 9708

For the 2003 error, 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.1 as my connect string. First I replaced it with localhost which got me further but still the same 2003 error. Then I saw that if I scaled back the number of calls the error disappeared completely.

Upvotes: 1

lutz
lutz

Reputation:

Code 2003 is a standard MySQL client error:

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

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?

$ mysql --host=192.168.1.1 -u root bullsorbit

Upvotes: 5

Blauohr
Blauohr

Reputation: 6003

with localhost you connect via loopback-interface.

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

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

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

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

#skip-networking

Upvotes: 3

unmounted
unmounted

Reputation: 34388

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.

Upvotes: 0

Related Questions