'mysql error:2003: Can't connect to MySQL server on '%-.100s:%u' (docker)

In a docker container, I'm trying to build a database with an sql file and fill that database using python scripts. They work locally but I need them in a docker container.

When trying to build the container, I have issues with connecting to the mysql container and the python script exits. I get this error message mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '%-.100s:%u' (%s) (Warning: %u format: a real number is required, not str)

this is the mysql part of the docker compose:

mysql:
        image: mariadb:latest
        environment:
            MYSQL_ROOT_PASSWORD: 'nipt@qc'
            MYSQL_USER: 'MolBio'
            MYSQL_PASSWORD: 'nipt@qc'
            MYSQL_DATABASE: 'nipt'
        volumes:
          - mysqldata:/var/lib/mysql
        ports:
            - 3307:3306

And this is how I try to connect to it in the .py:

cnx=mysql.connector.connect(user="MolBio",
    password="nipt@qc",
    host = "localhost:3307",
    )


Solution 1:[1]

This error occurs because you are combining the hostname and port in in the call to connect:

>>> import mysql.connector as mc
>>> conn = mc.connect(
               user='root', password='', database='test', host='localhost:3306'
)
Traceback (most recent call last):
  File "/home/user/virtual-envs/so310/lib/python3.10/site-packages/mysql/connector/network.py", line 547, in open_connection
    addrinfos = socket.getaddrinfo(self.server_host,
  File "/usr/local/lib/python3.10/socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/virtual-envs/so310/lib/python3.10/site-packages/mysql/connector/__init__.py", line 273, in connect
    return MySQLConnection(*args, **kwargs)
  File "/home/user/virtual-envs/so310/lib/python3.10/site-packages/mysql/connector/connection.py", line 114, in __init__
    self.connect(**kwargs)
  File "/home/user/virtual-envs/so310/lib/python3.10/site-packages/mysql/connector/abstracts.py", line 1009, in connect
    self._open_connection()
  File "/home/user/virtual-envs/so310/lib/python3.10/site-packages/mysql/connector/connection.py", line 418, in _open_connection
    self._socket.open_connection()
  File "/home/user/virtual-envs/so310/lib/python3.10/site-packages/mysql/connector/network.py", line 565, in open_connection
    raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '%-.100s:%u' (%s) (Warning: %u format: a real number is required, not str)

Instead, pass the port as an integer, using the port keyword argument.

>>> conn = mc.connect(user='root', password='', database='test', host='localhost', port=3306)
>>> print(conn)
<mysql.connector.connection.MySQLConnection object at 0x7f7baea96830>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1