7

I'm trying to use MySQL Connection/Python to connect to my database.

Here's the output I'm getting:

Traceback (most recent call last):
  File "bh2000.py", line 33, in <module>
    cnx = mysql.connector.connect(**config)
  File "/Library/Python/2.7/site-packages/mysql/connector/__init__.py", line 155, in connect
    return MySQLConnection(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 123, in __init__
    self.connect(**kwargs)
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 430, in connect
    self._open_connection()
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 393, in _open_connection
    self._socket.open_connection()
  File "/Library/Python/2.7/site-packages/mysql/connector/network.py", line 375, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host xxx.db.1and1.com:3306' (8 nodename nor servname provided, or not known)

Here's the code I'm using:

import mysql.connector

cnx = mysql.connector.connect(user='xxx', password='xxx',
                              host='xxx.db.1and1.com',
                              database='xxx')
cnx.close()

Where am I going wrong?

Sebastian
  • 3,548
  • 18
  • 60
  • 95
  • Sounds like a network error from the errors you've posted. What does "host .db.1and1.com" give you when run? – Nahkki Jun 06 '14 at 14:41
  • Thanks for that. When I run in the browser? I get 'this webpage is not available', but I currently have loads of projects running with the same db no problem. – Sebastian Jun 06 '14 at 14:43
  • What happens when you run it in your terminal/command prompt? It sounds like is that, for whatever reason, the computer you are running this python code on cannot locate your host. – Nahkki Jun 06 '14 at 14:46
  • If I run the code above in terminal? Same error as above. – Sebastian Jun 06 '14 at 14:49
  • More specifically. If you run(without quotes): "host .db.1ana1.com" in your terminal. I'm reasonably sure that the problem is not currently in your code(though there may be additional issues there) but rather in you connection to that db from the computer you are running the code from. – Nahkki Jun 06 '14 at 18:58
  • I'm having this exact same problem connecting to a database on the same machine as my py code. Tried over localhost and 127.0.0.1 neither works, but mysql command line client connects fine with the same credentials. – Eric Uldall Feb 09 '17 at 01:43

5 Answers5

4
  • Did you specify the correct port?
  • Is your MySQL server running?
  • Is a firewall blocking access?
  • Try removing anonymous user account from your MySQL server?

Default port if not specified is 3306. Otherwise there is nothing wrong with your code. The problem is with your MySQL server or the connection is being blocked by your firewall or the server firewall. Make sure port 3306 is open and not blocked.

db = mysql.connector.connect(user='xxx', password='xxx', host='xxx.db.1and1.com', port=3306)
panofish
  • 7,578
  • 13
  • 55
  • 96
  • 1
    In my case, it was the firewall blocking the access. I had to add an iptables rule for localhost on port 3307 (my default mysql port). – henrique romao Oct 10 '18 at 12:58
3

I believe the answer is because the hosting service you use, 1and1.com, uses a firewall that blocks all outside access to their databases. You have to run the code from a url that they host or it will not work.

I use the same server and had to figure this out the hard way myself. Below is a copy of what they have on one of their pages:

Access to the database via your website/presence only Please always establish the connection to your database via your website/presence. For security reasons, it is not possible to access the database directly, for example via your local computer (external ODBC connection).

To protect your data, your MySQL database is located on a dedicated database server that is protected by a firewall.

rdt0086
  • 138
  • 6
1

Add a port argument in the command.

cnx = mysql.connector.connect(user='myuser', 
        password='mypassword', host='localhost', port='3306', database='mydb')
george mano
  • 5,948
  • 6
  • 33
  • 43
-2

I got the same error. Once crosscheck the details you have entered. The host must be in the format 127.0.0.1. There should not be any / after the url.

aravindkanna
  • 663
  • 1
  • 7
  • 25
-5

Use localhost instead of IP, if you are working on same server.

myConnection = mysql.connector.connect(user='uname', password='pass', host='localhost', port='3306', database='databasename')
amarnath
  • 785
  • 3
  • 19
  • 23