我正在学习 Python 并尝试使用教程来连接到远程数据库。 我遇到的问题是我不确定用什么替换 localhost,我尝试过域名、IP 地址等,但不断收到以下错误。
OperationalError: (2003, "Can't connect to MySQL server on 'remotehost name' (timed out)")
# Open database connection
db = pymysql.connect("localhost","username","password","dbname" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Drop table if it already exist using execute() method.
cursor.execute("DROP TABLE IF EXISTS rsstracker")
# Create table as per requirement
sql = """CREATE TABLE rsstracker (
article_title varchar(255),
article_url varchar(1000),
article_summary varchar(1000)
summary )"""
cursor.execute(sql)
# disconnect from server
db.close()
最佳答案
您应该将 localhost
替换为服务器的 IP 地址或主机名。如果该服务器位于您的 LAN 中,您将需要其内部地址。如果它在您的网络之外,您将需要它的外部地址。
无论哪种方式,您都需要确保您使用的端口得到良好转发,并且不会被一路上的路由器/防火墙(包括远程服务器操作系统上的/由远程服务器的操作系统阻止/过滤)。
这个问题可能更适合superuser exchange .
关于python - PyMySQL 连接到远程数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45917661/