使用数据库连接将代码部署到 Azure Web 时,Flask 应用程序无法呈现,但在本地服务器上运行良好

Flask app fails to render when deploying code to Azure Web with a database connection, but works fine from local server

我有一个带有 Flask 模板的 Azure Web 应用程序。我希望它连接到 SQL 数据库。我做了一个。我安装了pymssql。出于测试目的,我添加到根文件夹中的 views.py:

import pymssql
conn = pymssql.connect(server='mydb.database.windows.net', user='mydbnameadmin@mydb', password='secret', database='mydb')

我首先在我的 kubuntu 机器上进行了本地测试。为了确保我能连接上,我输入了错误的密码,服务器抛出了一个错误。我输入正确,错误消失了。凉爽的。 Azure 门户上的 SQL 仪表板也报告了成功的连接。然后我将我的更改(包括更新 requirements.txt)推送到我的 github 存储库,它被吸入我的 Web 应用程序。当我尝试 运行 网络应用程序时,在索引页面上:

The page cannot be displayed because an internal server error has occurred.

打开并检查详细日志后,我明白了:

HTTP Error 500.0 - Internal Server Error
The page cannot be displayed because an internal server error has occurred.
Most likely causes:

IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.
IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.
IIS was not able to process configuration for the Web site or application.
The authenticated user does not have permission to use this DLL.
The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.

IIS。它在这里做什么?我认为 MS 很流行,并在 linux 盒子 0_0

上托管了这项服务

我去掉第二行

conn = pymssql.connect(server='mydb.database.windows.net', user='mydbnameadmin@mydb', password='secret', database='mydb')

错误消失了。如果我 运行 本地服务器仍然使用 Azure SQL 服务器,我没有任何问题。

这似乎与我遇到的问题相同: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e20bb2a3-bdbe-4e49-8b92-b36fe50577da/having-trouble-deploying-flask-app-into-azure-with-azure-database?forum=windowsazurewebsitespreview&prof=required

但他们说他们是否真的让它与 SQL 数据库一起工作...

人们怎么看? 谢谢

我的requirements.txt

alembic==0.7.7
azure==0.11.1
Flask==0.10.1
Flask-Migrate==1.5.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.0
futures==3.0.3
itsdangerous==0.24
Jinja2==2.8
Mako==1.0.1
MarkupSafe==0.23
pymssql==2.1.1
python-dateutil==2.4.2
six==1.9.0
SQLAlchemy==1.0.8
Werkzeug==0.10.4
wheel==0.24.0

据我了解,Azure 网站默认托管在 Windows Server 2012 VM 上,这不会安装 FreeTDS 而且我们也没有安装它的权限。而 pymssql 是基于 FreeTDS.

所以我们可以使用 pyodbc 连接 Azure SQL 作为解决方法来轻松处理它。

我能够使用 pymssql 2.1 库将我的 Python 代码成功连接到 Azure 上的 SQL 数据库。

我不知道这是否是导致问题的原因,但我确实在我的连接字符串中包含了几个额外的参数...也许这会有所帮助。我明确指定了驱动程序和 Encrypt 参数,因为始终为 SQL Azure 启用 SSL(我相信)

myConnection = pyodbc.connect('Driver={SQL Server};'
'Server=tcp:1234567.database.windows.net,1433;'
'Database=MyAzureDatabase;'
'Uid=geekgirl@123456;Pwd=abcdef;'
'Encrypt=yes')

如果这不起作用,也许可以尝试在 connect 语句周围添加一些错误处理以尝试获取更具体的错误消息。

双手合十!

我遇到了这个问题,但我最终意识到这是因为我在Visual Studio中使用了64位版本的Python,但是Azure只支持32位版本。

我看到了:

DLL load failed: %1 is not a valid Win32 application.

一旦我切换到安装和使用 32 位版本,我就可以毫无问题地使用 Flask 和 pyodbc。