title: Docker Compose getting error ECONNREFUSED 127.0.0.1:3306 with MySQL and NodeJS
body: <p>I am trying to setup some containers for my NestJS + TypeORM + MySQL environment by using Docker Compose in a Windows 10 host, but I am getting an ECONNREFUSED error:</p>
  <pre><code>connect ECONNREFUSED 127.0.0.1:3306 +2ms
  backend_1  | Error: connect ECONNREFUSED 127.0.0.1:3306
  backend_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
  backend_1  |     --------------------
  backend_1  |     at Protocol._enqueue (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
  backend_1  |     at Protocol.handshake (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)        
  backend_1  |     at PoolConnection.connect (/usr/src/app/node_modules/mysql/lib/Connection.js:116:18)
  backend_1  |     at Pool.getConnection (/usr/src/app/node_modules/mysql/lib/Pool.js:48:16)
  backend_1  |     at /usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:793:18
  backend_1  |     at new Promise (&lt;anonymous&gt;)
  backend_1  |     at MysqlDriver.createPool (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:790:16)
  backend_1  |     at MysqlDriver.&lt;anonymous&gt; (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:278:51)
  backend_1  |     at step (/usr/src/app/node_modules/typeorm/node_modules/tslib/tslib.js:141:27)
  backend_1  |     at Object.next (/usr/src/app/node_modules/typeorm/node_modules/tslib/tslib.js:122:57)
  </code></pre>
  <p>I have created the following <code>Dockerfile</code> to configure the NestJS API container:</p>
  <pre><code>FROM node:12-alpine
  WORKDIR /usr/src/app
  
  COPY package.json .
  RUN npm install
  
  EXPOSE 3000
  
  #CMD [&quot;npm&quot;, &quot;start&quot;]
  
  CMD /wait-for-it.sh db:3306 -- npm start
  
  COPY . .
  </code></pre>
  <p>And then I reference this from Docker Compose with the following <code>docker-compose.yml</code>:</p>
  <pre><code>version: &quot;3.8&quot;
  
  networks:
    app-tier:
      driver: bridge
  
  services:
    db:
      image: mysql
      command: --default-authentication-plugin=mysql_native_password
      restart: always
      expose:
        - &quot;3306&quot;
      ports:
        - &quot;3306:3306&quot;    
      networks:
        - app-tier      
      environment:
        MYSQL_DATABASE: school
        MYSQL_ALLOW_EMPTY_PASSWORD: ok
        MYSQL_ROOT_PASSWORD: root
        MYSQL_USER: dbuser
        MYSQL_PASSWORD: dbuser
        MYSQL_ROOT_HOST: '%'
    backend:
      depends_on:
        - db
      build: .
      ports:
        - &quot;3000:3000&quot;
      networks:
        - app-tier      
  </code></pre>
  <p>Finally, I set the TypeORM configuration to match with the Docker Compose file:</p>
  <pre><code>export const DB_CONFIG: TypeOrmModuleOptions = {
      type: 'mysql',
      host: 'db',
      port: 3306,
      username: 'dbuser',
      password: 'dbuser',
      database: 'school',
      entities: [], // We specify the entities in the App Module.
      synchronize: true,
  };
  </code></pre>
  <p>I am kind of new to Docker Compose, but I have tried many things like changing the output port to 3307, setting an explicit network... and the port 3306 is free in my host OS when I run it. Any help?</p>
  <h3>Edit 1</h3>
  <p>I have included <code>MYSQL_ROOT_HOST</code> and <code>wait-for-it.sh</code> as suggested, but still no results.</p>