简体   繁体   中英

Docker NodeJS with MySQL connect ECONNREFUSED 127.0.0.1:8000

I have 2 container which is mysql and nodejs. However, the connection between my nodejs and mysql doesnt seem to be connected. The error is

connect ECONNREFUSED 127.0.0.1:8000 

My server.js file

 //Connection with database var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", port: "8000", user: "root", password: "root", database: "justcall" }); con.connect(function(err){ if(err){ console.log('Database connection error: ' + err.message); }else{ console.log('Database connection successful'); } });

Dockerfile for my nodejs

 FROM node:12 RUN useradd -ms /bin/bash admin WORKDIR /app COPY . /app RUN chown -R admin:admin /app RUN chmod 755 /app USER admin CMD npm start EXPOSE 4443
I build the image using

docker run -p 4443:4443 nodejs:signalingserver2

I run the container using

docker run -p 4443:4443 nodejs:signalingserver2

My mysql docker-compose is

 version: '3' services: mysql: image: mysql:8.0 container_name: mysql-server-80 # command: --default-authentication-plugin=msql_native_password # working dir: /application volumes: - .:/application restart: always ports: - '3306:3306' expose: - '3306' environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=mydb - MYSQL_USER=root - MYSQL_PASSWORD=root phpmyadmin: depends_on: - mysql image: phpmyadmin/phpmyadmin container_name: phpmyadmin restart: always ports: - "8080:80" links: - mysql environment: PMA_HOSTS: mysql php: container_name: php image: php:php_img build: context: ./ volumes: - /root/app/xampp/justcall:/var/www/html ports: - 8000:80 depends_on: - mysql restart: always

The problem is everytime i run my nodejs server, it has an error which is ECONNREFUSED

You are trying to connect with php-myadmin, not with MySQL.

Try to change your connection to

//Connection with database
var mysql = require('mysql');
var con = mysql.createConnection({
  host: "mysql",
  port: "3306",
  user: "root",
  password: "root",
  database: "mydb"
});

Also, add the nodejs container to docker-compose or add legacy linking between nodejs container and MySQL container. when you use localhost it refers to the localhost of node container, so you need to use docker-networking to connect with DB container from nodejs container. check service to service communication .

docker run -it --link mysql -p 4443:4443 nodejs:signalingserver2

also, uncomment this command: --default-authentication-plugin=msql_native_password , mysql 8 does not support plain authutication.

With docker-compose it can be

version: '3'
services:

  mysql:
    image: mysql:8.0
    command: --default-authentication-plugin=msql_native_password
    ports:
      - '3306:3306'
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
  nodejs:
    image: nodejs:signalingserver2
    ports:
      - '4443:4443'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM