![](/img/trans.png)
[英]Django 4.0.3 and mssql-django 1.1.2 will not connect to SQL Server Express 2019
[英]Can't connect django to exist MS SQL Server using mssql-django
尝试执行 manage.py runserver 时出现错误。 我已经为 sql 服务器安装了 mssql-django 和 odbc 驱动程序 17。 一般来说,我对 django 和 python 非常陌生,但我想使用现有的 ms sql 服务器而不是 Z786D348E95442121DC5B7387E 这是我在setting.py中的代码:
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "eHospital_DKAG_NSTL",
"USER": "sa",
"PASSWORD": "passwod",
"HOST": "172.16.2.20\sqlservertest",
"PORT": "1433",
"OPTIONS": {
"driver": "ODBC Driver 17 for SQL Server",
},
},
}
这是错误消息:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "F:\Python\Django\fptdb\lib\site-packages\mssql\base.py", line 353, in get_new_connection
conn = Database.connect(connstr, **args)
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (258)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "F:\Python\Django\fptdb\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "F:\Python\Django\fptdb\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
self.check_migrations()
File "F:\Python\Django\fptdb\lib\site-packages\django\core\management\base.py", line 576, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
self.build_graph()
File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
if self.has_table():
File "F:\Python\Django\fptdb\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
with self.connection.cursor() as cursor:
File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
return self._cursor()
File "F:\Python\Django\fptdb\lib\site-packages\mssql\base.py", line 246, in _cursor
conn = super()._cursor()
File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
self.ensure_connection()
File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 243, in ensure_connection
with self.wrap_database_errors:
File "F:\Python\Django\fptdb\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "F:\Python\Django\fptdb\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "F:\Python\Django\fptdb\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "F:\Python\Django\fptdb\lib\site-packages\mssql\base.py", line 353, in get_new_connection
conn = Database.connect(connstr, **args)
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (258)')
任何帮助表示赞赏。 谢谢你。
将“PORT”保留为空白或删除“PORT” ,否则如果设置任何端口号都会出错,例如“2244” 、 “9877”甚至“1433” ,这是MSSQL的默认端口号:
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "eHospital_DKAG_NSTL",
"USER": "sa",
"PASSWORD": "passwod",
"HOST": "172.16.2.20\sqlservertest",
"PORT": "", # Keep it blank
# "PORT": "1433", # Or remove it
"OPTIONS": {
"driver": "ODBC Driver 17 for SQL Server",
},
},
}
您可以看到在mssql-django 的示例中, “PORT”保持空白,如下所示:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'mydb',
'USER': 'user@myserver',
'PASSWORD': 'password',
'HOST': 'myserver.database.windows.net',
'PORT': '', # Kept blank
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
# set this to False if you want to turn off pyodbc's connection pooling
DATABASE_CONNECTION_POOLING = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.