Search code examples
mysqldockernode-red

Node RED on Docker writing on MYSQL database


I am running Node-RED on Docker, and I am trying to write data to MySQL on my localhost.

As host, I am using localhost (see picture)

Node-Red Host

I receive the error: connection refused.

Which Host address should I use here? I don't quite understand the communication between the container and the local host.


Solution

  • localhost always points to the TCP/IP stack that the program opening the connection is bound to.

    Every Docker container has it's own TCP/IP stack so unless the database is running in the same container as Node-RED localhost will not be the correct hostname for the database.

    You have 3 choices

    1. If you are using docker-compose then you can use the container name as the hostname and it will connect to the right container.
    2. You can use the docker inspect [container-instance-name] to find the IP address assigned to the database container and use that
    3. If you have mapped the container port to your host machine (with the -p option) then you can use the IP address of the host which normally defaults to 172.17.0.1

    Edit:

    To connect to the host machine then you can use host.docker.internal on Docker for Windows and Docker for Mac (unfortunately the issue to fix this on Linux has been open for over 3 years).

    You should also be able to use the IP address from point 3 above.