0

After reading many thread here i didn't resolved my problem, my code works with a free database created in this website (freemysqlhosting.net), but it doesn't work with my own database on a hosting webiste. My code ('******' for privacy):

#!/usr/bin/python3

import pymysql

db = pymysql.connect("sql.******.it", "******", "******", "******")

cursor = db.cursor()

cursor.execute("SELECT * FROM users")

results = cursor.fetchall()
print(results)

db.close()

Here the error:

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'sql.******.it' ([Errno 101] Network is unreachable)")

2 Answers2

2

From command line I would use ping, and then nc. I mean, just cut things to minimum, an check if that works. So first validate if host is valid, and your host can resolve it to IP. Next check if you can connect to that host, on provided port. Here is sample in python.

If I have to bet - I would say, you are missing port... Just double check all names, docs provided by the hosting.

Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
0

Have you tried using mysql.connector? That's what I use maybe it will work

import mysql.connector

mydb = mysql.connector.connect(
   host="localhost",
   user="yourusername",
   passwd="yourpassword"
   )

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
    print(x)
NatanMish
  • 11
  • 4
  • I tried with `import MySQLdb`, supported by my hosting service, but nothing, no response when i execute a query, and i got this `Can't connect to MySQL server on 'sql.******.it:3306' (110 Connection timed out)` With `mysql.connector` same problem. –  Mar 30 '19 at 10:20