Home  >  Article  >  Backend Development  >  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
WBOYforward
2024-02-06 10:42:041120browse

初始化数据库失败,出现错误拨号 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

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete