在阅读了很多线程后,我没有解决我的问题,我的代码使用在这个网站(freemysqlhosting.net)中创建的免费数据库,但它不能在托管网站上使用我自己的数据库。我的代码('******'隐私):
#!/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()
这里的错误:
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'sql.******.it' ([Errno 101] Network is unreachable)")
从命令行我会使用ping
,然后使用nc
。我的意思是,只需将事情减少到最低限度,检查一下是否有效。因此,首先验证主机是否有效,并且您的主机可以将其解析为IP。接下来检查是否可以在提供的端口上连接到该主机。 Here是python的样本。
如果我必须下注 - 我会说,你错过了端口......只需仔细检查主机提供的所有名称,文档。
你尝试过使用mysql.connector吗?这就是我使用它可能会起作用
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)