python:&quort; mysql.connector.errors.interfaceerror:2003&quot当我的端口是一个数字时

发布于 2025-02-02 00:09:20 字数 3466 浏览 4 评论 0原文

我正在尝试在高山容器中使用MySQL与Python一起使用,并且在笔记本电脑上可以正常工作的简单脚本看到以下错误。它的说法是不需要字符串的数字(查看其他帖子此数字是端口):

bash-5.1# python3 test.py 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 550, in open_connection
    socket.SOL_TCP)
  File "/usr/local/lib/python3.7/socket.py", line 752, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name does not resolve

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    database='defaultdb'
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/__init__.py", line 273, in connect
    return MySQLConnection(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 116, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 1052, in connect
    self._open_connection()
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 494, in _open_connection
    self._socket.open_connection()
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 566, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '%-.100s:%u' (%s) (Warning: %u format: a number is required, not str)

bash-5.1# pip3 list
Package                Version
---------------------- -----------
certifi                2022.5.18.1
charset-normalizer     2.0.12
idna                   3.3
mysql                  0.0.3
mysql-connector-python 8.0.29
mysqlclient            2.1.0
pip                    22.0.4
protobuf               4.21.0
requests               2.27.1
setuptools             57.5.0
tabulate               0.8.9
urllib3                1.26.9
wheel                  0.37.1

这是脚本。如您所见,我绝对在这里使用一个数字:

bash-5.1# cat test.py 
import mysql.connector

mydb = mysql.connector.connect(
  host="mydbname-do-user-12345678-0.b.db.ondigitalocean.com",
  user="myadmin",
  password="mypassword",
  port=int(12374),
  database='defaultdb'
)

print(mydb)

这是我的Dockerfile:

# syntax=docker/dockerfile:1

# Alpine is chosen for its small footprint
# compared to Ubuntu

FROM python:3.7-alpine3.16
COPY --from=golang:1.14-alpine /usr/local/go/ /usr/local/go/

ENV PATH="/usr/local/go/bin:${PATH}"

RUN apk -v --update add \
      util-linux \
      jq \
      curl \
      python3 \
      py-pip \
      groff \
      bash \
      go \
      mysql \
      gcc musl-dev mariadb-connector-c-dev \
      && \
      pip3 install --upgrade requests tabulate mysql mysql-connector-python && \
      apk -v --purge del && \
      rm /var/cache/apk/*

WORKDIR /app
RUN mkdir /app/data/
COPY ./*.py /app/
RUN chmod +x /app/*
COPY ./data/ /app/data/

我的dockerfile是在我的笔记本电脑上工作正常。我需要添加到我的容器中才能正常工作?我想念一些Python包装吗?即使我尝试将笔记本电脑上所有相同的精确软件包安装到容器上,我仍然会看到错误。

更新:我的主机名称似乎存在问题,我添加了一个类似于我自己的DB服务器。

进一步研究它似乎就像 this 。看来python:3.7-alpine3.16容器在主机名中存在一些DNS问题,但是IP地址正常工作。

I am trying to use mysql with python in an alpine container and I am seeing the following error for a simple script that works fine on my laptop. Its saying a number not a string is required (looking at other posts this number is the port):

bash-5.1# python3 test.py 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 550, in open_connection
    socket.SOL_TCP)
  File "/usr/local/lib/python3.7/socket.py", line 752, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name does not resolve

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    database='defaultdb'
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/__init__.py", line 273, in connect
    return MySQLConnection(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 116, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 1052, in connect
    self._open_connection()
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 494, in _open_connection
    self._socket.open_connection()
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 566, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '%-.100s:%u' (%s) (Warning: %u format: a number is required, not str)

bash-5.1# pip3 list
Package                Version
---------------------- -----------
certifi                2022.5.18.1
charset-normalizer     2.0.12
idna                   3.3
mysql                  0.0.3
mysql-connector-python 8.0.29
mysqlclient            2.1.0
pip                    22.0.4
protobuf               4.21.0
requests               2.27.1
setuptools             57.5.0
tabulate               0.8.9
urllib3                1.26.9
wheel                  0.37.1

Here's the script. As you can see I am absolutely using a number here:

bash-5.1# cat test.py 
import mysql.connector

mydb = mysql.connector.connect(
  host="mydbname-do-user-12345678-0.b.db.ondigitalocean.com",
  user="myadmin",
  password="mypassword",
  port=int(12374),
  database='defaultdb'
)

print(mydb)

Here is my dockerfile:

# syntax=docker/dockerfile:1

# Alpine is chosen for its small footprint
# compared to Ubuntu

FROM python:3.7-alpine3.16
COPY --from=golang:1.14-alpine /usr/local/go/ /usr/local/go/

ENV PATH="/usr/local/go/bin:${PATH}"

RUN apk -v --update add \
      util-linux \
      jq \
      curl \
      python3 \
      py-pip \
      groff \
      bash \
      go \
      mysql \
      gcc musl-dev mariadb-connector-c-dev \
      && \
      pip3 install --upgrade requests tabulate mysql mysql-connector-python && \
      apk -v --purge del && \
      rm /var/cache/apk/*

WORKDIR /app
RUN mkdir /app/data/
COPY ./*.py /app/
RUN chmod +x /app/*
COPY ./data/ /app/data/

What stumps me is that it works fine on my laptop. What do I need to add into my container to get this working? Am I missing some python packages? Even when I attempt to install all the same exact packages from my laptop onto the container, I still see the error.

Update: It seems there an issue with my host name, I have added one more like my own which is a db server from digital ocean.

Looking into it more seems to be like this. It seems that the python:3.7-alpine3.16 container has some dns issues when it comes to hostnames, but IP addresses work fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2025-02-09 00:09:20

该错误

socket.gaierror: [Errno -2] Name does not resolve

将指示名称分辨率的问题(对于myHostName)。

The error

socket.gaierror: [Errno -2] Name does not resolve

would indicate problems with the name resolution (for myhostname).

祁梦 2025-02-09 00:09:20

我通过使用nslookup找到IP地址,然后使用它而不是主机名来解决此问题。

bash-5.1#  nslookup "mydbname-do-user-12345678-0.b.db.ondigitalocean.com"
Server:   192.168.0.1
Address:  192.168.0.1:53

Non-authoritative answer:
Name: "mydbname-do-user-12345678-0.b.db.ondigitalocean.com"
Address: 1.2.3.4

** server can't find "mydbname-do-user-12345678-0.b.db.ondigitalocean.com": NXDOMAIN

I fixed this by finding the ip address with nslookup and then using that instead of the hostname.

bash-5.1#  nslookup "mydbname-do-user-12345678-0.b.db.ondigitalocean.com"
Server:   192.168.0.1
Address:  192.168.0.1:53

Non-authoritative answer:
Name: "mydbname-do-user-12345678-0.b.db.ondigitalocean.com"
Address: 1.2.3.4

** server can't find "mydbname-do-user-12345678-0.b.db.ondigitalocean.com": NXDOMAIN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文