Home > Backend Development > Golang > Failed to initialize the database, error dialing tcp 127.0.0.1:3306: connect: connection refused problem

Failed to initialize the database, error dialing tcp 127.0.0.1:3306: connect: connection refused problem

WBOY
Release: 2024-02-06 10:42:04
forward
1162 people have browsed it

初始化数据库失败,出现错误拨号 tcp 127.0.0.1:3306: connect: 连接被拒绝 问题

Question content

I am learning docker, let’s start now But I have a problem when I run it with this docker docker run --rm -p 8080:8080/tcp --env-file .env my-project:latest Here is some of my .env code. I'm using docker desktop on Windows, am I unable to run docker on localhost in Windows?

DB_HOST=127.0.0.1
DB_USERNAME=root
DB_NAME=mydbs
DB_PASS=root123
AUTH_GEN_URL=https://api.learning.mydbs.id
Copy after login

Does anyone know? Any answers would be greatly appreciated

Thank you


Correct answer


The problem is that when you start the container, it tries to connect to 127.0.0.1:3306 inside the container instead of the host, so you get an error because the connection was refused because there is nothing on port 3306 running the localhost in the container.

For Windows and Mac, this can be easily fixed by using host.docker.internal instead of 127.0.0.1. This ensures that services running within the container properly connect to the MySQL instance running on the host machine.

For Linux, it's even simpler, since all you have to do is pass the --network="host" option to the docker run command

The above is the detailed content of Failed to initialize the database, error dialing tcp 127.0.0.1:3306: connect: connection refused problem. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template