简体   繁体   English

如何从 dockerized python 应用程序连接到本地 sql server 数据库

[英]How to connect to local sql server database from dockerized python app

I am trying to connect to a long-running SQL server database on my local machine from a dockerized python container, but I keep getting the error below.我正在尝试从 dockerized python 容器连接到本地计算机上长期运行的 SQL Server 数据库,但我不断收到以下错误。 I will add all relevant code as well.我也会添加所有相关代码。 I can confirm,m the username and password are both correct(I have verified 10+ times and set up a new account on the server and database with full rights as well).我可以确认,我的用户名和密码都是正确的(我已经验证了 10 多次,并在服务器和数据库上设置了一个具有完全权限的新帐户)。

I have scoured stack overflow and articles for the last day, but none have given me anything different, so I am hoping someone here will have some info that will point me in the right direction.我在最后一天搜索了堆栈溢出和文章,但没有人给我任何不同的东西,所以我希望这里有人能提供一些信息,为我指明正确的方向。 Please let me know if you need any more info.如果您需要更多信息,请告诉我。

pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Login failed for user 'myusername'. (18456) (SQLDriverConnect)")

Here is my dockerfile这是我的码头文件

FROM python:3.7

RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini

RUN apt-get update \
 && apt-get install unixodbc -y \
 && apt-get install unixodbc-dev -y \
 && apt-get install freetds-dev -y \
 && apt-get install freetds-bin -y \
 && apt-get install tdsodbc -y \
 && apt-get install --reinstall build-essential -y

RUN pip install --trusted-host pypi.python.org pyodbc==4.0.26 

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY src/. .

CMD ["python", "main.py"]

And here is main.py这是 main.py

import pyodbc

try:
  sql_connection = pyodbc.connect('Driver={FreeTDS};'
                                  'Server=host.docker.internal,1433;'
                                  'Database=mydatabasename;' 
                                  'UID=myusername;'
                                  'PWD=mypassword')
  sql_cursor = sql_connection.cursor()
  sql_cursor.execute("Select @@version")

  print(sql_cursor.fetchone())
finally:
  sql_cursor.close()
  sql_connection.close()

i would start with first trying to connect to this DB from inside the running docker container (via docker exec) using SQL client (psql, mysql-client whatever the DB you have) and connfirm you can do it.我首先尝试使用 SQL 客户端(psql、mysql-client 无论你拥有什么数据库)从正在运行的 docker 容器(通过 docker exec)内部连接到这个数据库,并确认你可以做到。 If not obviously smth on DB level doesn't allow the connection possibly only 127.0.0.1 is allowed in the DB settings如果不是很明显,数据库级别不允许连接,则数据库设置中可能只允许 127.0.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从(未经过安装的)Python应用程序连接到dockerized PostgreSQL - How to connect to a dockerized PostgreSQL from (an undockerized) Python app 将 Python 与 SQL Server 数据库连接起来 - Connect Python with SQL Server Database 在 Python 单元测试中连接到本地 dockerized Perforce 服务器 - Connecting to local dockerized Perforce server in Python unittests 如何使用 Python 和 Windows 身份验证从 WSL2 连接到非本地 MS SQL 服务器? - How to connect to non-local MS SQL Server from WSL2 using Python and Windows Authentication? 如何从 ZC5FD214CDD0D2B3B4272E 中的 python 容器脚本连接到 Ubuntu 上的本地 SQL 服务器 - How do I connect to the local SQL server on Ubuntu from a python script in Docker container 如何从 Python Flask 连接到 SQL Server? - How to connect to SQL Server from Python Flask? 如何从Dockerized Python Web应用程序提供静态文件? - How to serve static files from a Dockerized Python web app? 从 Python 连接到 SQL 服务器 - Connect to SQL Server from Python Python:如何连接到服务器数据库? - Python: How to connect to a server database? 如何连接到 Google Cloud SQL,其中 python 的 pymysql 作为 Kubernetes 集群中运行的 dockerized 容器的一部分运行? - How to connect to Google Cloud SQL with python's pymysql running as part of a dockerized container running in a Kubernetes cluster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM