如何从pythonanywhere烧瓶WebApp(通过pandas& sqlalchemy)访问外部MySQL数据库:接收sqlalchemy.exc.exc.operationalerror

发布于 2025-02-11 00:35:46 字数 6587 浏览 3 评论 0原文

我正在尝试将烧瓶WebApp转换为在Pythonanywhere上运行,而不是我当前托管并完美运行的Raspberry Pi。

WebApp的核心功能之一是使用Pandas查询外部托管的MySQL数据库(未托管在Pythonanywhere上)。

以前,我已经使用以下方法进行了此操作,没有任何麻烦:

import pandas as pd
URI=f"mysql://{user}@{host}:{port}/{schema}"
my_dataframe=pd.read_sql_query(sql="select * from users",con=URI)

使用pythonanywhere托管的WebApp进行尝试,导致502-备用错误,错误日志的错误日志:(

File "./webapp.py", line 240, in ages
    message_, success=scripts.ages.main()
  File "./scripts/ages.py", line 22, in main
    my_dataframe=pd.read_sql_query(sql="select * from users",con=URI)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/pandas/io/sql.py", line 383, in read_sql_query
    chunksize=chunksize,
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/pandas/io/sql.py", line 1295, in read_query
    result = self.execute(*args)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/pandas/io/sql.py", line 1162, in execute
    *args, **kwargs
  File "<string>", line 2, in execute
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/deprecations.py", line 401, in warned
    return fn(*args, **kwargs)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3145, in execute
    connection = self.connect(close_with_result=True)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3204, in connect
    return self._connection_cls(self, close_with_result=close_with_result)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 96, in __init__
    else engine.raw_connection()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3283, in raw_connection
    return self._wrap_pool_connect(self.pool.connect, _connection)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3254, in _wrap_pool_connect
    e, dialect, self
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2101, in _handle_dbapi_exception_noconnection
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3250, in _wrap_pool_connect
    return fn()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 310, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 868, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 476, in checkout
    rec = pool._do_get()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
    self._dec_overflow()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
    with_traceback=exc_tb,
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
    return self._create_connection()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 256, in _create_connection
    return _ConnectionRecord(self)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 371, in __init__
    self.__connect()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 666, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
    with_traceback=exc_tb,
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/create.py", line 590, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 597, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/MySQLdb/__init__.py", line 130, in Connect
    return Connection(*args, **kwargs)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/MySQLdb/connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2003, "Can't connect to MySQL server on '34.105.08.12:8080' (111)")
(Background on this error at: https://sqlalche.me/e/14/e3q8)

我更改了IP)

我已经去看了在链接网站 https://sqlalche.me/e/e/14/e3q8 't看到任何有用的东西。

我还尝试过谷歌搜索如何在Pythonanywhere中使用Sqlalchemy。这主要导致有关连接到Pythonanywhere托管的数据库的问题,这对我没有帮助。

我遇到了这个链接:

https://help.pythonanywhere.com/pages/usingsqlalchemywithmysql/

我实际上不知道它是指使用SQLalchemy连接到pythonanywhere托管MySQL数据库或外部数据库,但无论如何我还是尝试实现此功能。我怀疑这会有所作为,但是URI位于PANDAS SQL查询的单独文件中。

#credentials.py
from sqlalchemy import create_engine
URI_string=f"mysql://{user}@{host}:{port}/{schema}"
URI = create_engine(URI_string, pool_recycle=280)
import pandas as pd
import credentials
my_dataframe=pd.read_sql_query(sql="select * from users",con=credentials.URI)

通过此更改在PI上进行测试时,WebApp仍然有效,但是在PyanyWhere进行测试并接收相同的错误日志输出时,结果没有差异。

如果有人有任何想法,请告诉我!

I am attempting to convert a Flask webapp to run on PythonAnywhere instead of my Raspberry Pi on which it is currently hosted and functions perfectly.

One of the core features of the webapp is using Pandas to query an externally hosted MySQL database (NOT hosted on PythonAnywhere).

Previously I have done this using the following method, with no troubles:

import pandas as pd
URI=f"mysql://{user}@{host}:{port}/{schema}"
my_dataframe=pd.read_sql_query(sql="select * from users",con=URI)

Attempting this with the webapp hosted on PythonAnywhere results in a 502-backend error, with an error log of:

File "./webapp.py", line 240, in ages
    message_, success=scripts.ages.main()
  File "./scripts/ages.py", line 22, in main
    my_dataframe=pd.read_sql_query(sql="select * from users",con=URI)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/pandas/io/sql.py", line 383, in read_sql_query
    chunksize=chunksize,
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/pandas/io/sql.py", line 1295, in read_query
    result = self.execute(*args)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/pandas/io/sql.py", line 1162, in execute
    *args, **kwargs
  File "<string>", line 2, in execute
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/deprecations.py", line 401, in warned
    return fn(*args, **kwargs)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3145, in execute
    connection = self.connect(close_with_result=True)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3204, in connect
    return self._connection_cls(self, close_with_result=close_with_result)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 96, in __init__
    else engine.raw_connection()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3283, in raw_connection
    return self._wrap_pool_connect(self.pool.connect, _connection)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3254, in _wrap_pool_connect
    e, dialect, self
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2101, in _handle_dbapi_exception_noconnection
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 3250, in _wrap_pool_connect
    return fn()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 310, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 868, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 476, in checkout
    rec = pool._do_get()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
    self._dec_overflow()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
    with_traceback=exc_tb,
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
    return self._create_connection()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 256, in _create_connection
    return _ConnectionRecord(self)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 371, in __init__
    self.__connect()
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 666, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
    with_traceback=exc_tb,
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/create.py", line 590, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 597, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/MySQLdb/__init__.py", line 130, in Connect
    return Connection(*args, **kwargs)
  File "/home/mywebapp/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/MySQLdb/connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2003, "Can't connect to MySQL server on '34.105.08.12:8080' (111)")
(Background on this error at: https://sqlalche.me/e/14/e3q8)

(I've changed the IP)

I've take a look at the linked site https://sqlalche.me/e/14/e3q8, but didn't see anything useful on there.

I've also tried googling how to use sqlalchemy within PythonAnywhere. This mostly resulted in questions about connecting to databases hosted by PythonAnywhere, which doesn't help me.

I came across this link:

https://help.pythonanywhere.com/pages/UsingSQLAlchemywithMySQL/

I don't actually know if it's referring to using SQLAlchemy to connect to a PythonAnywhere hosted MySQL database, or an external one, but I tried implementing this anyway. I doubt it makes a difference, but the URI is in a separate file to the Pandas sql query.

#credentials.py
from sqlalchemy import create_engine
URI_string=f"mysql://{user}@{host}:{port}/{schema}"
URI = create_engine(URI_string, pool_recycle=280)
import pandas as pd
import credentials
my_dataframe=pd.read_sql_query(sql="select * from users",con=credentials.URI)

The webapp is still functional when testing on the Pi with this change, however it made no difference in the outcome when testing on PyAnywhere and the same error log output is received.

If anyone has any ideas please let me know!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2025-02-18 00:35:46

如果您使用的是免费帐户,则只能使用http(s)从pythonanywhere连接到批准的网站列表。因此,外部数据库连接将无法从免费帐户中使用。

If you're using a free account, you will only be able to connect out of PythonAnywhere using http(s) to a list of approved sites. So external database connections will not work from a free account.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文