0

我有一个在 Azure 上运行的 Flask 应用程序,并连接到 Azure SQL 数据库,经过以下开发后,我无法再访问数据库。

这是我最近添加的内容:

  • 创建了一个.env文件来保护连接字符串

  • 添加了一个.gitignore

  • 将以下内容添加到我的 main.py 中:

     connection_string = textwrap.dedent(f'''
          Driver={driver};
          Server={os.getenv("SERVER")};
          Database={os.getenv("DB_NAME")};
          Uid={os.getenv("DB_USER")};
          Pwd={os.getenv("PASSWORD")};
          Encrypt=yes;
          TrustServerCertificate=no;
          Connection Timeout=30;
      ''')
    
  • 我还添加了以下内容以在生产中运行:

     if __name__ == '__main__':
         from waitress import serve
         serve(app, host="0.0.0.0", port=8080)
    

它在本地完美运行,但是一旦我在 Azure 上部署它,我就遇到了后台 docker 部署的问题。似乎无法从以下错误(来自 Kudu)中识别连接字符串参数:

Warning
2021-10-15T12:42:51.4960727

[2021-10-15 12:42:51 +0000] [40] [ERROR] Exception in worker process

  Ok
2021-10-15T12:42:51.4961108

Traceback (most recent call last):

  Ok
2021-10-15T12:42:51.4961177

  File "/tmp/8d98fd8a8763182/antenv/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker

  Ok
2021-10-15T12:42:51.4961354

    worker.init_process()

  Ok
2021-10-15T12:42:51.4961403

  File "/tmp/8d98fd8a8763182/antenv/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process

  Ok
2021-10-15T12:42:51.4961451

    self.load_wsgi()

  Ok
2021-10-15T12:42:51.4961495

  File "/tmp/8d98fd8a8763182/antenv/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi

  Ok
2021-10-15T12:42:51.4961542

    self.wsgi = self.app.wsgi()

  Ok
2021-10-15T12:42:51.4961586

  File "/tmp/8d98fd8a8763182/antenv/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi

  Ok
2021-10-15T12:42:51.4961638

    self.callable = self.load()

  Ok
2021-10-15T12:42:51.4961682

  File "/tmp/8d98fd8a8763182/antenv/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load

  Ok
2021-10-15T12:42:51.4961729

    return self.load_wsgiapp()

  Ok
2021-10-15T12:42:51.4961772

  File "/tmp/8d98fd8a8763182/antenv/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp

  Ok
2021-10-15T12:42:51.496182

    return util.import_app(self.app_uri)

  Ok
2021-10-15T12:42:51.4961864

  File "/tmp/8d98fd8a8763182/antenv/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app

  Ok
2021-10-15T12:42:51.496191

    mod = importlib.import_module(module)

  Ok
2021-10-15T12:42:51.4961956

  File "/opt/python/3.8.6/lib/python3.8/importlib/__init__.py", line 127, in import_module

  Ok
2021-10-15T12:42:51.4962002

    return _bootstrap._gcd_import(name[level:], package, level)

  Ok
2021-10-15T12:42:51.496308

  File "", line 1014, in _gcd_import

  Ok
2021-10-15T12:42:51.496315

  File "", line 991, in _find_and_load

  Ok
2021-10-15T12:42:51.4963197

  File "", line 975, in _find_and_load_unlocked

  Ok
2021-10-15T12:42:51.4963244

  File "", line 671, in _load_unlocked

  Ok
2021-10-15T12:42:51.496329

  File "", line 783, in exec_module

  Ok
2021-10-15T12:42:51.4963337

  File "", line 219, in _call_with_frames_removed

  Ok
2021-10-15T12:42:51.4963383

  File "/tmp/8d98fd8a8763182/main.py", line 104, in 

  Ok
2021-10-15T12:42:51.4965707

    con, cur = AZconnect()

  Ok
2021-10-15T12:42:51.496577

  File "/tmp/8d98fd8a8763182/main.py", line 91, in AZconnect

  Ok
2021-10-15T12:42:51.4965817

    con: pyodbc.Connection = pyodbc.connect(connection_string)

  Warning
2021-10-15T12:42:51.4965863

pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

我很确定我在 .env 和 load_dotenv() 上做错了,但我不知道在哪里。

4

1 回答 1

0

我明白我的错误;.gitignore阻止使用该.env文件,因此无法设置环境变量。然后我在 Azure 应用程序设置中设置了这些变量,它工作得很好:

天蓝色应用设置

于 2021-10-15T15:08:04.633 回答