skip to Main Content

I’m running a go app locally (MacOS Sonoma) that connects to a local mysql server. This works fine when I build the app and run it from the command line. When I create a container and run it – all locally – it can not connect. The exact error:

dial tcp 127.0.0.1:3306: connect: connection refused

The connection string I use for both cases is:

user:pw@tcp(127.0.0.1:3306)/raw_data?parseTime=true

When I run docker I use:

docker run --net=host <latest image id>

My sql.cnf looks like this:

[mysqld]
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0

[client-server]
bind-address = 0.0.0.0

I tried restarting the server, but there was no change. The users permission on the server looks like this:

GRANT ALL ON *.* TO `user`@`%`

I’ve also tried running the container like this:

docker run -p 3306:3306 <image id>

2

Answers


  1. The issue is that 127.0.0.1 is the loopback inside your docker container not the one on your host. You need to specify the IP address of the host machine when you are running the code in a container.

    Login or Signup to reply.
  2. Update your connection string to:

    user:pw@tcp(host.docker.internal:3306)/raw_data?parseTime=true
    

    this way you are now refering to the host machine instead of refering to the loopback interface of the container.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search