简体   繁体   English

尝试连接到 mysql 服务器时连接被拒绝

[英]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.

相关问题 连接拒绝尝试连接到服务器 - Connection refused trying to connect to server 无法连接到MySQL服务器(连接被拒绝),尝试在Django和MySQL之间安装连接 - Can't connect to MySQL server(Connection refused) , trying to install connection between Django and MySQL 尝试连接到蓝牙服务器时连接被拒绝 - Connection refused when trying to connect to bluetooth server 为什么我在尝试将我的 python 客户端连接到服务器时不断收到 [Erno 111] 连接被拒绝? - Why do i keep getting [Erno 111] connection refused while trying to connect my python client to server? 尝试使用python连接到TCP服务器时拒绝连接 - Getting connection refused when trying to connect to a tcp server with python Errno 61:尝试连接到python服务器时出现连接拒绝错误 - Errno 61: Connection refused error when trying to connect to python server 无法连接到“数据库”上的 MySQL 服务器([Errno 111] 连接被拒绝 - Can't connect to MySQL server on 'database' ([Errno 111] Connection refused Docker MYSQL [2003] 无法连接到 MySQL 服务器(111 连接被拒绝) - Docker MYSQL [2003] Can't connect to MySQL server (111 Connection refused) Riak-尝试使用Python客户端库连接到Riak群集时拒绝连接 - Riak - Connection Refused while trying to connect to Riak cluster using Python client library mysql.connector.errors.InterfaceError:2003:无法连接到“127.0.0.1:3306”上的 MySQL 服务器(111 连接被拒绝) - mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM