An error occurs when using NodeJS to connect to MySQL in Docker
P粉722521204
P粉722521204 2024-04-06 12:07:19
0
1
513

I created a NodeJS backend server connected to MySQL. Using Docker, I created an image to run my nodeJS and package.json files (which includes MySQL) using the following commands. Here is my Dockerfile:

FROM node

WORKDIR /app

COPY package.json .

RUN npm install

COPY . /app

EXPOSE 3000

CMD ["node", "app.js"]

I have another file to create a connection to MySQL using NodeJS:

const mysql = require("mysql");

const con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "ilovestackoverflow",
  database: "db830",
  port: "3306"
});

con.connect(function (err, rows) {
  if (err) throw err;
  console.log("Database is connected!");
});

module.exports = con;

I tried to run Dockers (docker run -p 3000:3000 help:help), but got the following error (don't know how to solve it, please help!!):

Server is listening on Port: 3000
/app/config/database.js:12
  if (err) throw err;
           ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16)
    --------------------
    at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
    at Object.<anonymous> (/app/config/database.js:11:5)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1022:19)
    at require (node:internal/modules/cjs/helpers:102:18) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

P粉722521204
P粉722521204

reply all(1)
P粉787806024

In a multi-container environment, there are multiple networks in the docker container, and each container has its own network namespace. When you need to access other Docker containers, networking in Docker is done through service names. In the docker-compose settings, you will reference the name of the service. When using regular docker containers, it's a bit more difficult. you need to

  1. Create Docker network

docker network creates awesomeNetwork

  1. Add the first container to the network

docker network connectionnodebackend AwesomeNetwork

  1. Add the second container to the network

docker network connection db AwesomeNetwork

You can now reference another container within the container network using the name db/nodebackend respectively.


In a development/local docker-compose environment, I highly recommend using docker-compose and writing your compose manifest. Then you don't need to create the network as it will be created for you every time. The setup looks like this

# docker-compose.yml
version: '3.8'
services:
  nodeBackend:
    build: .
    context: ./Path
    dockerfile: Dockerfile
    ports:
     - "3000:3000"
  db:
    image: mysql
    ports:
      - 3306:3306
    volumes:
      - :/var/lib/mysql      # select a path to persist your data
    environment:
      - MYSQL_ROOT_PASSWORD=
      - MYSQL_PASSWORD=
      - MYSQL_USER=
      - MYSQL_DATABASE=

Now you can connect to db in your node application using the hostname db

const con = mysql.createConnection({
  host: "db",
  user: "root",
  password: "ilovestackoverflow",
  database: "db830",
  port: "3306"
});

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template