我想用 Python 从其他网络连接 MySQL 数据库。
例如:网络 A:服务器,网络 B:PC
<块引用>我已经尝试过这个 link 但我收到一条错误消息: 2003: Can't connect to MySQL server on 'xxx.xxx.xxx.xxx:3306' (10060 Ein Verbindungsversuch ist fehlgeschlagen, da die Gegenstelle nach einer bestimmten Zeitspanne nicht richtig reagiert hat, oder die hergestellte Verbindung war fehlerhaft, da der verbundene Host nicht reagiert hat)
(它的德语)
IP是正确的,但它不起作用。服务器的 XAMPP 端口是 3306,所以这不是问题,服务器正在运行 mysql 模块和 apache 模块。我已经用TCP和UDP打开了poerts 3306和80
在本地主机上运行完美
这就是我的代码:
import mysql.connector
from mysql.connector import errorcode
# Obtain connection string information from the portal
config = {
'host':'xxx.xxx.xxx.xxx',
'user':'coinchaser',
'password':'Coinchaser2021',
'database':'coinchaser'
}
# Construct connection string
try:
conn = mysql.connector.connect(**config)
print("Connection established")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with the user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cursor = conn.cursor()
# Insert some data into table
cursor.execute("INSERT INTO leaderboard (crdate, playername, points, playedTime) VALUES (%s,%s,%s,%s)", ("16.03.", "test1", 400, "1:45.65"))
# Cleanup
conn.commit()
cursor.close()
conn.close()
print("Done.")