Question

I am having some issues connecting to my hosted mysql database using python. My code is as follows:

import MySQLdb

db = MySQLdb.connect(host="xxxxxx.db.1and1.com", user="xxxxxxx",passwd="xxxxxxx", db="xxxxxxx")

I get the following error:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    db = MySQLdb.connect(host="db518597727.db.1and1.com", user="dbo518597727", passwd="OilyOily123", db="db518597727")
  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 193, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'xxxxxxx.db.1and1.com' (10060)")

My sql database is hosted by 1and1. I am sure I have the right credentials, as when I read from this database on a php website, using the same credentials (I have triple checked) it works fine.

I have tried this both on a Raspberry Pi (where I intend to use it) and on my Windows 8.1 PC, so I am pretty sure that it is not the computer that is the problem.

Any help would be much appreciated.
Many thanks.

Was it helpful?

Solution

In MySQL credentials are per client, so we distinguish a couple of client types:
- localhost means the same machine as MySQL server connecting via Unix Socket
- 127.0.0.1 means the same machine as MySQL server connecting via TCP/IP
- A.B.C.D where letters are replaced by numbers in range 0-255 which means IP address of client machine connecting via TCP/IP
- * is a wildcard which means that any client can connect with given credentials via TCP/IP

Every entry in MySQL users table consists of client specification (described above), username, password and some other columns (not relevant here).

So the problem you are facing is that PHP script on 1and1 server can connect to the database hosted on 1and1 since the hosting company sets up their database server to accept connections from their own servers with valid credentials.

But the same credentials are considered invalid for connections coming from client machines unknown to 1and1.

What you can do is to ask 1and1 to give your specific IP access rights to your database.
What you can't to is to overcome this problem on your own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top