[英]Connection refused while trying to connect to mysql server
I'm trying to connect my flask project to a mysql server using peewee.我正在尝试使用 peewee 将我的烧瓶项目连接到 mysql 服务器。 When I declare the database using the information I have on my server:
当我使用服务器上的信息声明数据库时:
app = Flask(__name__)
mydb = MySQLDatabase(os.getenv("MYSQL_DATABASE"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
host=os.getenv("MYSQL_HOST"),
port=3306
)
print(mydb)
everything works fine, I run the flask project from the vps terminal and I get the <peewee.MySQLDatabase object at x> line, meaning that the connection was succesful, or at least that's my understanding.一切正常,我从 vps 终端运行烧瓶项目,我得到 <peewee.MySQLDatabase object at x> 行,这意味着连接成功,或者至少这是我的理解。 But when I try to connect from the python file (mydb.connect()) to add some tables everything stops working and I get the error message:
但是当我尝试从 python 文件 (mydb.connect()) 连接以添加一些表时,一切都停止工作,我收到错误消息:
peewee.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
The mysql server is running on my local computer and the project is located on the vps but that shouldn't be a problem, especially because I can connect to it before changing the python file. mysql 服务器在我的本地计算机上运行,并且项目位于 vps 上,但这应该不是问题,特别是因为我可以在更改 python 文件之前连接到它。
Python Python
import os
from flask import Flask
from peewee import *
app = Flask(__name__)
mydb = MySQLDatabase(os.getenv("MYSQL_DATABASE"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
host=os.getenv("MYSQL_HOST"),
port=3306
)
print(mydb)
mydb.connect() #When I add this line everything stops working
Why is it that with only that line my code doesn't work?为什么只有那一行我的代码不起作用?
I'm very skeptical that you will be able to connect from the flask app running on some remote server, to the mysql running on your laptop.我非常怀疑您是否能够从某个远程服务器上运行的烧瓶应用程序连接到笔记本电脑上运行的 mysql。 How is traffic routed?
流量如何路由?
Also you're wrong about this:你也错了:
print(mydb) # This does not mean you can connect to the database.
mydb.connect() # This is when an actual connection is created.
Try running both on the same computer.尝试在同一台计算机上运行两者。 If you absolutely must run flask on your VPS and mysql on your workstation, you will need to tunnel the traffic somehow or use a vpn.
如果您绝对必须在您的 VPS 上运行 flask 并在您的工作站上运行 mysql,您将需要以某种方式传输流量或使用 vpn。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.