如何从Docker容器中的python脚本连接到Ubuntu上的本地SQL服务器



我在Ubuntu 20.04中安装了一台SQL Server(安装在VirtualBox中(和一个带有python脚本的Docker容器。我想做的是从这个python脚本连接到主机上运行的SQL服务器。

这是我用来创建图像的Dockerfile:

# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN apt-get update && apt-get install -y gnupg2 curl
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN apt-get update 
&& apt-get -y install gcc 
&& apt-get -y install g++ 
&& apt-get -y install unixodbc unixodbc-dev 
&& apt-get clean 
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m" , "test", "run", "--host=0.0.0.0"]

我知道它看起来很乱,但我在安装pyodbc时遇到了几个问题,看起来所有这些代码都解决了这个问题。

我现在可以成功地创建一个图像,但当启动一个容器时,它会产生以下输出:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/app/test.py", line 10, in <module>
connection = pyodbc.connect(connection_string, autocommit=True)
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
['ODBC Driver 17 for SQL Server']

连接字符串似乎有问题,如下所示:

connection_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:host.docker.internal,1433;UID=SA;PWD=<my_password>'

我已经为此挣扎了一段时间,但仍然无法让它发挥作用。如果有任何帮助或建议,我们将不胜感激!

UPD-

按照Pato的建议,我尝试将连接字符串中的Server选项更改为机器的IP地址。

ip addr show向我返回了以下输出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host 
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:29:e8:b8 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
valid_lft 71149sec preferred_lft 71149sec
inet6 fe80::2517:652e:ac69:8ec9/64 scope link noprefixroute 
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
link/ether 02:42:35:26:ba:86 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:35ff:fe26:ba86/64 scope link 
valid_lft forever preferred_lft forever

因此,我尝试用127.0.0.110.0.2.15172.17.0.1代替host.docker.internal。所有这些都给了我以下错误:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/app/test.py", line 10, in <module>
connection = pyodbc.connect(connection_string, autocommit=True)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server;Server=tcp:172.17.0.1,1433;UID=SA;PWD=<my_password>' : file not found (0) (SQLDriverConnect)")

ifconfig给我回了这个:

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
inet6 fe80::42:35ff:fe26:ba86  prefixlen 64  scopeid 0x20<link>
ether 02:42:35:26:ba:86  txqueuelen 0  (Ethernet)
RX packets 25340  bytes 1240556 (1.2 MB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 39308  bytes 691001962 (691.0 MB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
inet6 fe80::2517:652e:ac69:8ec9  prefixlen 64  scopeid 0x20<link>
ether 08:00:27:29:e8:b8  txqueuelen 1000  (Ethernet)
RX packets 649701  bytes 946934508 (946.9 MB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 55603  bytes 7183567 (7.1 MB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
loop  txqueuelen 1000  (Local Loopback)
RX packets 27675  bytes 49732911 (49.7 MB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 27675  bytes 49732911 (49.7 MB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我正在研究这个连接问题,在这篇文章中出现了找不到的错误文件。我试图重新创建你的设置,但我得到了同样的错误。

一个快速的解决方案是将驱动程序更改为pymssql==2.2.2(在我的docker容器中测试(。

pip3 install pymssql==2.2.2

下面是一个例子:

import pymssql
conn = pymssql.connect('host.docker.internal', 'sa', 'yourPassword', "database")
cursor = conn.cursor()
cursor.execute("""
IF OBJECT_ID('persons', 'U') IS NOT NULL
DROP TABLE persons
CREATE TABLE persons (
id INT NOT NULL,
name VARCHAR(100),
salesrep VARCHAR(100),
PRIMARY KEY(id)
)
""")
cursor.executemany(
"INSERT INTO persons VALUES (%d, %s, %s)",
[(1, 'John Smith', 'John Doe'),
(2, 'Jane Doe', 'Joe Dog'),
(3, 'Mike T.', 'Sarah H.')])
# you must call commit() to persist your data if you don't set autocommit to True
conn.commit()
cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
row = cursor.fetchone()
while row:
print("ID=%d, Name=%s" % (row[0], row[1]))
row = cursor.fetchone()
conn.close()
# Result
'''
ID=1, Name=John Smith
'''

更新

SQL Server Linux版本故障排除

  1. 检查mssql-server.service是否正在运行
patricio@server2:~$ sudo systemctl status mssql-server.service
[sudo] password for patricio:
● mssql-server.service - Microsoft SQL Server Database Engine
Loaded: loaded (/lib/systemd/system/mssql-server.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-11-10 09:56:38 -03; 2 weeks 4 days ago
Docs: https://learn.microsoft.com/en-us/sql/linux
Main PID: 14885 (sqlservr)
Tasks: 164
CGroup: /system.slice/mssql-server.service
├─14885 /opt/mssql/bin/sqlservr
└─14913 /opt/mssql/bin/sqlservr
  1. 检查端口1433或默认端口是否打开
patricio@server2:~$ telnet 127.0.0.1 1433
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
patricio@server2:~$ nmap 127.0.0.1
Starting Nmap 7.60 ( https://nmap.org ) at 2021-11-29 08:41 -03
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000085s latency).
Not shown: 994 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
443/tcp  open  https
1433/tcp open  ms-sql-s
// ...
  1. 如果未安装sqlcmd,请安装它
patricio@server2:~$ sqlcmd -S 127.0.0.1 -U sa -p
Password:
1> SELECT CONVERT(varchar, SERVERPROPERTY('collation'))
2> GO
------------------------------
SQL_Latin1_General_CP1_CI_AS
(1 rows affected)

参考文献:

从Docker容器内部,如何连接到机器的本地主机?

尝试访问host.docker.Connection中的内部结果拒绝

相关内容

  • 没有找到相关文章

最新更新