ECONNREFUSED while trying to connect MySQL server

System Information
  • Strapi Version: 4.6.1
  • Operating System: Mac OS Ventura
  • Database: MySQL 2.18.1
  • Node Version: 16-alpine
  • NPM Version:
  • Yarn Version:

Hello! Hope you doing well :slight_smile:

I’m trying to run Strapi through Docker but i’m facing several issues:

  • First, i kept loosing data. I figured that came from using sqlite as Client so,
    → I created one database.js file by environment
    → I tried to use MySQL as a client

  • Then i face issue: ECONNREFUSED 127.0.0.1:3306 while trying to run the Strapi app
    → My DB container is running fine on port 3306
    → I think that might come from TCP stuffs but I don’t know how enable TCP on a Docker Service

Here my docker-compose.yml:

version: '3'

name: dev_starter_strapi

services:
  strapi-cms:
    container_name: dev.starter.strapi-cms
    build:
      context: ../
      dockerfile: docker/dev.Dockerfile
    image: strapi:latest
    restart: unless-stopped
    env_file: .env
    environment:
      DATABASE_CLIENT: ${DATABASE_CLIENT}
      DATABASE_HOST: ${DATABASE_HOST}
      DATABASE_PORT: ${DATABASE_PORT}
      DATABASE_NAME: ${DATABASE_NAME}
      DATABASE_USERNAME: ${DATABASE_USERNAME}
      DATABASE_PASSWORD: ${DATABASE_PASSWORD}
      JWT_SECRET: ${JWT_SECRET}
      ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
      APP_KEYS: ${APP_KEYS}
      NODE_ENV: ${NODE_ENV}
    volumes:
      - ../config:/opt/app/config
      - ../src:/opt/app/src
      - ../package.json:/opt/package.json
      - ../yarn.lock:/opt/yarn.lock
      - ../.env:/opt/app/.env
      - ../public/uploads:/opt/app/public/uploads
    ports:
      - '1337:1337'
    networks:
      - strapi
    depends_on:
      - strapi-db

  strapi-db:
    container_name: dev.starter.strapi-db
    platform: linux/amd64 #for platform error on Apple M1 chips
    restart: unless-stopped
    env_file: .env
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_USER: ${DATABASE_USERNAME}
      MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_DATABASE: ${DATABASE_NAME}
    volumes:
      - ../data:/var/lib/mysql
    ports:
      - '3306:3306'
    networks:
      - strapi

volumes:
  strapi-data:

networks:
  strapi:
    name: Strapi
    driver: bridge

Here my config/env/development/database.js:

const path = require("path");

module.exports = ({ env }) => ({
  connection: {
    client: env("DATABASE_CLIENT"),
    connection: {
      filename: path.join(
        __dirname,
        "..",
        env("DATABASE_FILENAME", ".tmp/data.db")
      ),
      port: env("DATABASE_PORT"),
      database: env("DATABASE_NAME"),
      user: env("DATABASE_USER"),
      password: env("DATABASE_PASSWORD"),
    },
  },
});

Please let me know if you need more informations :wink:

Thank you for reading this, take care!

Hello and welcome to the Strapi Community :cake: @CorentinTrt
The reason you get that error is that the you will have two separate containers
If you are using docker-compose for local development the DATABASE_HOST would be the name of the container strapi-db or dev.starter.strapi-db 127.0.0.1 is part of the other container and hence why it’s Connection refused.

You also need to change your database.js to use this.

I made a tool that you might have used called dockerize npx @strapi-community/dockerize just make sure you backup all your config and dockerfile.

But when you run it select MySQL and it should help with most of it.

Hope this helps.

Yep, i didn’t provide my .env file but DATABASE_HOST=strapi-db and still, i got this ECONNREFUSED error.

In the meantime, i switch to Postgres, adapting the database.js file and now it works just fine.

I should have use your tool but i wanted to learn more about database dockerization :wink:

However i still dont understand why it’s not working with mySQL…