How to fix 'ECONNREFUSED' error in my nodejs app using docker? - mysql

I am using Docker container for my node app which is a REST API Server. It use multiple backend (currently mongoDB and mysql). My mongoclient is connecting to the port defined in my docker-compose.yml but my mysql is not configuring to it, throwing ECONNREFUSED error.
Also i like to add In my localhost I have both mongodb and mysql installed on my system.
Below is the code of my Dockerfile and docker-compose.yml
FROM node:8
RUN mkdir -p /home/sharad/eapp
WORKDIR /home/sharad/eapp
COPY package.json /home/sharad/eapp/
RUN npm install
COPY . /home/sharad/eapp
EXPOSE 8016
CMD [ "npm","start" ]
version: '3.1'
services:
server:
image: sharadm20/eyantra-app
restart: always
ports:
- 8016:8016
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
links:
- mongo
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
ME_CONFIG_MONGODB_ENABLE_ADMIN: 'true'
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
adminer:
image: adminer
restart: always
ports:
- 8082:8080
And below is the error which I am encountering:
Unable to connect to the mysql database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 }

It worked by changing the Hostname in mysql config to the name similar to docker image name( in my case mysql is called db) so the hostname+port would look like db:3306.
Thanks to Marc Sances for pointing it out.

Related

connect ECONNREFUSED 127.0.0.1:3306 in adonis 5 docker mysql [duplicate]

This question already has an answer here:
Node.js connect to MySQL Docker container ECONNREFUSED
(1 answer)
Closed last year.
I'm using Adonis 5 and Mysql database , after following some tutorials i added this code to connect mysql. but it shows me "connect ECONNREFUSED 127.0.0.1:3306" error. Here is my code below:
My Dockerfile:
FROM node:14
RUN mkdir -p /home/node/app/node_modules
WORKDIR /home/node/app
COPY package*.json ./
# RUN apk add --no-cache git
COPY . /home/node/app/
RUN chown -R node:node /home/node
RUN npm install
USER node
EXPOSE 3333
ENTRYPOINT ["node","ace","serve","--watch"]
my docker-compose.yml file:
# For more information: https://github.com/julien-r44/adonis-sail
version: '3'
services:
mysql:
image: 'mysql/mysql-server:8.0'
container_name: mysql
expose:
- "3307"
ports:
- '${MYSQL_PORT:-3307}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${MYSQL_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${MYSQL_DB_NAME:?err}'
MYSQL_USER: '${MYSQL_USER:?err}'
MYSQL_PASSWORD: '${MYSQL_PASSWORD?:err}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
phpmyadmin:
image: 'phpmyadmin/phpmyadmin'
links:
- 'mysql:mysql'
ports:
- 8080:80
environment:
MYSQL_USERNAME: "${MYSQL_USER}"
MYSQL_ROOT_PASSWORD: "${MYSQL_PASSWORD}"
PMA_HOST: mysql
networks:
- sail
app:
links:
- mysql
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/home/node/app
- /home/node/app/node_modules
ports:
- 3333:3333
depends_on:
- mysql
- phpmyadmin
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: "local"
My .env file:
HOST=0.0.0.0
PORT=3333
APP_URL=http://${HOST}:${PORT}
NODE_ENV=development
APP_KEY=wH94pOFV47NO0dJtE_S6-TDRgcjgZXGM
SESSION_DRIVER=cookie
CACHE_VIEWS=false
DB_CONNECTION=mysql
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=adonis
MYSQL_PASSWORD=adonis
MYSQL_DB_NAME=social
Error:
it shows this error
What is my mistake?
Change from MYSQL_HOST=localhost to MYSQL_HOST=mysql
When your app is running inside a docker container, localhost points no longer to your laptop (or server) but to the container itself.
As each service is running in separated containers when one application is trying to access the database service you cannot use localhost. As localhost points to that container, and the database is not installed there.
You should use the container name instead localhost when specificying connection urls.
You can also check Docker Compose network documentation for detailed explanations.

Can't connect to MySQL container from other container

I'm trying to connect my FASTAPI app container to a MySQL database container using the docker-compose file. In the Docker documentation it says that docker creates a default network for both containers. However, I would like to use a pre-existing network that I've created(app-net).
This is my docker-compose file:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local
this is the output I get when i run docker inspect mysql -f "{{json .NetworkSettings.Networks }}":
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["mysql","5e998f9fb646"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null}}
However, when I run each container individually using CMD with the --network app-net the output is different:
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["46157e588c87"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"6a6922a9a6ea8f9d113447decbbb927cb93ddffd3b9563ee882fa2e44970cde5","Gateway":"172.20.0.1","IPAddress":"172.20.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:14:00:02","DriverOpts":null}}
In my app code in order to connect the mysql server, I specified the container name as the hostname since they are supposed to share the same network. But, as I mentioned it seems both containers can't talk to each other.
I'm pretty sure that is the reason I can't connect the database through my app and get that error when I run:
docker-compose -f docker-compose.yml up
I get this error:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2005, "Unknown MySQL server host 'mysql' (0)")
What am I missing?
If the error you're getting is specifically "unknown host" or something similar, this can happen if your application container starts before the database container exists. You can work around this situation by telling Compose about the dependency:
version: '3.8'
services:
mysql: { ... }
app:
depends_on:
- mysql
This has two key effects. If you try to start only the application container docker-compose up app, it will also start the mysql container, following the depends_on: chain. When Compose does start containers, it at least creates the mysql container before creating the app container.
Note that this doesn't guarantee the database will actually be running by the time your application starts up. If you do encounter this, you will get a different error like "connection refused". You can work around this by embedding a script in your image that waits for the database to be available, or by using a version 2 Compose file with health-check support; see Docker Compose wait for container X before starting Y for more details on this specific problem.
You could add the key networks to both containers, this way:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
networks:
- app-net
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
- app-net
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local

connecting to a mysql container: what's the equivalent of localhost inside a docker compose project?

I have this docker-compose.yml file, which has one service (log_app) trying to write to a mysql database (mydb).
version: '3.4'
services:
mydb:
container_name: mysql_data
restart: always
image: mysql:8.0.22
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PW}
MYSQL_DATABASE: ib
MYSQL_PASSWORD: ${MYSQL_PW}
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./mysql-data:/var/lib/mysql
tws:
build: .
container_name: ib_logger_app
volumes:
- ./ib/IBController.ini:/root/IBController/IBController.ini
- ./ib/jts.ini:/root/Jts/jts.ini
environment:
TRADING_MODE: ${TWS_TRADING_MODE}
TWSUSERID: ${TWS_USER_ID}
TWSPASSWORD: ${TWS_PASSWORD}
FIXUSERID:
FIXPASSWORD:
XVFB_ARGS: -ac -screen 0 1024x768x16 +extension RANDR
restart: always
ports:
- 5901:5900
depends_on:
- mydb
log_app:
build: log_app/ib_client
environment:
- IB_GATEWAY_URLNAME=tws
- IB_GATEWAY_URLPORT=4004
- MKT_DATA_TYPE=4
restart: on-failure
depends_on:
- tws
- mydb
volumes:
mysql-data:
When I run this on a bare metal server, everything works fine, but in that situation, I connect to localhost on port 3306. This doesn't seem to work here, though. I get
Can't connect to MySQL server on 'localhost' (99)
I've been reading that 3306 is still the right port, but localhost and 127.0.0.1 are wrong now. Neither works for me.
I've also seen some answers on this site that recommend changing localhost to host.docker.internal, but that gives me this:
Can't connect to MySQL server on 'host.docker.internal' (111)
The host name will be the container name as defined in your docker-compose file.
In your example, you should use mydb:3306 instead of localhost:3306
EDIT:
one way to ensure that is to exec into the container that you want to connect from and run:
telnet mydb 3306
and see if it works (but t might require additional installation of telnet).

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = ms_api_shop

I am using Lumen with Docker to create simple API for authentication. After installing LumenPassport, I cannot migrate the database. I can easily connect to the MySQL db with Dbeaver.
I have already created one Lumen Docker project for the same purpose, it is the second. The first one worked without a problem. Moreover, I have checked the MySQL databases, ms_api_shop was there
Errors:
Here is my docker-compose
services:
nginx:
build:
context: .
dockerfile: docker/Nginx.Dockerfile
image: nginx
ports:
- 8092:80
depends_on:
- fpm
volumes:
- ./:/var/www/lumen-docker
links:
- mysql
fpm:
build:
context: .
dockerfile: docker/fpm.Dockerfile
volumes:
- ./:/var/www/lumen-docker
depends_on:
- mysql
links:
- mysql
mysql:
image: mysql:5.7
ports:
- 33006:3306
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_DATABASE=ms_api_shop
- MYSQL_ROOT_USER=
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
And env:
DB_HOST=mysql
DB_PORT=33006
DB_DATABASE=ms_api_shop
DB_USERNAME=
DB_PASSWORD=
In your docker-file, you are binding the 33006 container port to the 3306 of the host port. In case you want to access the MySQL, you should use 3306 not 33006 as you did in your .env
I have a Laravel app running in Docker and a part of my docker config looks like:
But I personally feel that they should be within a docker network, look at the last two lines of the code in my docker-compose.yml below:
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql:/var/lib/mysql
networks:
- laravel
According to my knowledge, docker containers can communicate with each other when they are on the same network. You seem to have not connected these two docker in docker-compose yet. To get network information from a container, you can use the below command.
$ docker inspect --format='{{json .NetworkSettings.Networks}}' <docker_container_name>
In case you need to connect these two containers, follow these steps:
First, you create a network
$ docker network create my-net
Second, you connect container to the network.
$ docker network connect my-net <docker_container_name>
Don't forget to connect these two containers to the network.

Prisma 2 docker container can't connect MySQL Database container

I'm developing Backend server with prisma 2, MySQL, Docker-compose, GraphQL-Yoga server
My OS is Windows 10, docker compose use debian-openssl-1.1.x
problem is prisma 2 container can't connect MySQL container
docker-compose.yml
version: "3.7"
services:
mysql:
image: mysql:8.0.19
container_name: mysql
ports:
- 3306:3306
restart: always
environment:
MYSQL_DATABASE: $${MYSQL_DATABASE}
MYSQL_ROOT_PASSWORD: $${MYSQL_ROOT_PASSWORD}
volumes:
- /var/lib/mysql
prisma:
links:
- mysql
depends_on:
- mysql
container_name: prisma
ports:
- "5555:5555"
build:
context: back/prisma
dockerfile: Dockerfile
environment:
MYSQL_URL: $${MYSQL_URL}
MYSQL_ROOT_PASSWORD: $${MYSQL_ROOT_PASSWORD}
volumes:
- /app/prisma
backend:
links:
- mysql
depends_on:
- mysql
container_name: backend
ports:
- "4000:4000"
build:
context: back
dockerfile: Dockerfile
environment:
MYSQL_URL: $${MYSQL_URL}
FRONTEND_URL: $${FRONTEND_URL}
volumes:
- ./back:/app
- ./back/node_modules:/app/node_modules
- ./back/prisma:/app/prisma
frontend:
container_name: frontend
ports:
- "3000:3000"
build:
context: front
dockerfile: Dockerfile
environment:
BACKEND_URL: $${BACKEND_URL}
volumes:
- ./front:/app
- ./front/node_modules:/app/node_modules
.env
MYSQL_URL="mysql://root:prisma#mysql:3306/prisma"
BACKEND_URL=http://localhost:4000
FRONTEND_URL=http://localhost:3000
MYSQL_DATABASE:"prisma"
MYSQL_ROOT_PASSWORD:"prisma"
-schema.prisma
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
.env in prisma folder
DATABASE_URL="mysql://root:prisma#mysql:3306/prisma"
when i run docker-compose (docker-compose up), prisma container (prisma 2 studio) show this logs
Error in Prisma Client request:
Error:
Invalid `prisma.user.count()` invocation:
Authentication failed against database server at `mysql`, the provided database credentials for `root` are not valid.
Please make sure to provide valid database credentials for the database server at `mysql`.
at PrismaClientFetcher.request (/root/.cache/prisma/studio/app/runtime/index.js:1:52273)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
enter image description here
I think this error occurred because MySQL container has a password different from MYSQL_ROOT_PASSWORD which I wrote in the doctor-compose.yml.
But, i think there seems to be no problem with the setup, why is this error happening?
And if i did not set up MYSQL_ROOT_PASSWORD, what is the default password value generated by MySQL container?
I searched for days to find the answer to this problem, but I could not get the answer. please give me some advices. thank you