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.
CodePudding user response:
The error
socket.gaierror: [Errno -2] Name does not resolve
would indicate problems with the name resolution (for myhostname
).
CodePudding user response:
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