Django使用mssql(sqlserver)数据库配置问题解决

您所在的位置:网站首页 django连接数据库出错 Django使用mssql(sqlserver)数据库配置问题解决

Django使用mssql(sqlserver)数据库配置问题解决

2023-10-15 21:45| 来源: 网络整理| 查看: 265

操作系统:MAC 11 big sur Django版本:3.1.4 想用django操作远程的mssql数据库,之前使用php的Laravel框架连接过远程mssql一切正常,只需要安装驱动就可以了。Navicat也可以顺利连接上远程数据库,证明远程连接没问题。 搜索了网上的各种文章,一一尝试最后都失败了。 后来搜到了github上一个项目 https://github.com/essolutions/django-mssql-backend/ 下面的readme文件写的十分详细 跟着步骤做:

安装pyodbc和Django Install pyodbc and Django 这里可能存在的坑:后面最终一切安装完毕后,使用数据库时,之前由于安装其他包尝试时,django做了降级,导致了一些问题,后来升级回最新的django版本后,一切正常。 django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend. Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3'

出现这个提示后,我升级了django版本,就恢复了正常

安装django-mssql-backend Install django-mssql-backend

数据库配置,在settings.py文件中

DATABASES = { 'default': { 'ENGINE': 'sql_server.pyodbc',#引擎名称,必须是这个 'NAME': 'MyDB',#数据库名称,要连接的数据库的名字 'USER': 'user',#用户名,我的sa用户名登录失败,新建用户名可以用 'PASSWORD': 'password',#密码 'HOST': '127.0.0.1',#主机地址,我的是远程地址填写了域名'mydomain.com' 'PORT': '1433', #数据库使用端口,mssql一般默认是1433 'OPTIONS': { 'driver':'ODBC Driver 11 for SQL Server' #使用的驱动,此处有坑,后文介绍 }, }, }

标准配置(Standard Django settings) The following entries in a database-level settings dictionary in DATABASES control the behavior of the backend: ENGINE String. It must be “sql_server.pyodbc”. NAME String. Database name. Required. HOST String. SQL Server instance in “server\instance” format. PORT String. Server instance port. An empty string means the default port. USER String. Database user name in “user” format. If not given then MS Integrated Security will be used. PASSWORD String. Database user password. AUTOCOMMIT Boolean. Set this to False if you want to disable Django’s transaction management and implement your own. and the following entries are also available in the TEST dictionary for any given database-level settings dictionary: NAME String. The name of database to use when running the test suite. If the default value (None) is used, the test database will use the name “test_” + NAME. COLLATION String. The collation order to use when creating the test database. If the default value (None) is used, the test database is assigned the default collation of the instance of SQL Server. DEPENDENCIES String. The creation-order dependencies of the database. See the official Django documentation for more details. MIRROR String. The alias of the database that this database should mirror during testing. Default value is None. See the official Django documentation for more details.

各种可用设置项 OPTIONS Dictionary. Current available keys are: driver——【这条很重要】 String. ODBC Driver to use (“ODBC Driver 13 for SQL Server”, “SQL Server Native Client 11.0”, “FreeTDS” etc). Default is “ODBC Driver 13 for SQL Server”. 虽然文档提供的值只有三个,但是后面我安装过17后,修改成ODBC Driver 17 for SQL Server我是可以正常使用的。 isolation_level String. Sets transaction isolation level for each database session. Valid values for this entry are READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SNAPSHOT, and SERIALIZABLE. Default is None which means no isolation levei is set to a database session and SQL Server default will be used. dsn String. A named DSN can be used instead of HOST. host_is_server Boolean. Only relevant if using the FreeTDS ODBC driver under Unix/Linux. By default, when using the FreeTDS ODBC driver the value specified in the HOST setting is used in a SERVERNAME ODBC connection string component instead of being used in a SERVER component; this means that this value should be the name of a dataserver definition present in the freetds.conf FreeTDS configuration file instead of a hostname or an IP address. But if this option is present and it’s value is True, this special behavior is turned off. See http://www.freetds.org/userguide/dsnless.htm for more information. unicode_results Boolean. If it is set to True, pyodbc’s unicode_results feature is activated and strings returned from pyodbc are always Unicode. Default value is False. extra_params String. Additional parameters for the ODBC connection. The format is “param=value;param=value”. collation String. Name of the collation to use when performing text field lookups against the database. Default is None; this means no collation specifier is added to your lookup SQL (the default collation of your database will be used). For Chinese language you can set it to “Chinese_PRC_CI_AS”. connection_timeout Integer. Sets the timeout in seconds for the database connection process. Default value is 0 which disables the timeout. connection_retries Integer. Sets the times to retry the database connection process. Default value is 5. connection_retry_backoff_time Integer. Sets the back off time in seconds for reries of the database connection process. Default value is 5. query_timeout Integer. Sets the timeout in seconds for the database query. Default value is 0 which disables the timeout.

我在测试过程中,一直提示我

pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server Native Client 11.0' : file not found (0) (SQLDriverConnect)")

于是我去安装了mac系统的odbc驱动 安装网址:https://docs.microsoft.com/zh-tw/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15

该网页提示我: Microsoft ODBC 17 若要在 macOS 上安裝 Microsoft ODBC Driver 17 for SQL Server,請執行下列命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release brew update HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools

安装后,让我更新Xcode,更新的同时我测试了数据库连接,已经可以通过新创建的用户连接上远程mssql数据库了。

需要将对应的驱动进行更改

DATABASES = { 'default': { 'ENGINE': 'sql_server.pyodbc',#引擎名称,必须是这个 'NAME': 'MyDB',#数据库名称,要连接的数据库的名字 'USER': 'user',#用户名,我的sa用户名登录失败,新建用户名可以用 'PASSWORD': 'password',#密码 'HOST': '127.0.0.1',#主机地址,我的是远程地址填写了域名'mydomain.com' 'PORT': '1433', #数据库使用端口,mssql一般默认是1433 'OPTIONS': { 'driver':'ODBC Driver 17 for SQL Server' #使用的驱动,此处有坑,后文介绍 }, }, }

期间又提示了登录失败的错误,不过证实是用户对应数据库等登录信息设置的问题,修改远端mssql服务器后一切正常

pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无法打开登录所请求的数据库 "tplustest"。登录失败。 (4060) (SQLDriverConnect)')

最后还有sa用户登录的问题一直解决不了,pycharm配置数据库使用sa用户就会失败,原因还没有找到。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3