简体   繁体   English

无法连接到数据库:错误的getaddrinfo ENOTFOUND db db db:3306

[英]Can't connect to a database: Error getaddrinfo ENOTFOUND db db:3306

File Structure: 档案结构:

APP
├─ API
│  └─ Dockerfile.yml
├─ db
│  └─ storage
└─ docker-compose.yml

Where I don't have Database yet: 我还没有数据库的地方:

在此处输入图片说明

Dockerfile.yml: Dockerfile.yml:

# Base node image
FROM node:8.2.1

# ON DOCKER CONTAINER 
WORKDIR /API

# Install nodemon to monitor changes to the app.
RUN npm install -g nodemon

COPY package.json /API/package.json
RUN npm install && npm ls
RUN mv /API/node_modules /node_modules

# Stick current directory in the container's app directory
COPY . /API

# Change the user to 'node'
USER node

# Run the command
CMD ["node", "index.js"]

docker-compose.yml: 泊坞窗,compose.yml:

version: "3"

services:
  web:
    build: ./API
    command: nodemon --inspect=0.0.0.0:5858
    volumes:
      - ./API:/API
    ports:
      - "3000:3000"
      - "5858:5858"
    networks:
      - webnet
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ./db/storage:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - webnet
networks:
  webnet:

index.js: index.js:

const express = require('express');
const mysql = require('mysql');
const app = express();

var connection = mysql.createConnection({
   host     : 'db',
   user     : 'root',
   password : 'example',
   database : 'OriginalScenario'
 });

app.listen(3000, function () {
  console.log('Server is up and running. Listening on 127.0.0.1:3000!')
  connection.connect();
})

When I run docker-compose up --build from the command line, I get this: 当我从命令行运行docker-compose up --build ,我得到以下信息:

Successfully built 29aa0ae16e2e
Successfully tagged APP_web:latest
Starting APP_web_1 ...
Starting APP_db_1 ...
Starting APP_web_1
Starting APP_web_1 ... done
Attaching to APP_db_1, APP_web_1
db_1   | Initializing database
db_1   | 2017-08-16T21:17:53.855569Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see document
ation for more details).
db_1   | 2017-08-16T21:17:53.860792Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
db_1   | 2017-08-16T21:17:53.860935Z 0 [ERROR] Aborting
db_1   |
web_1  | [nodemon] 1.11.0
web_1  | [nodemon] to restart at any time, enter `rs`
web_1  | [nodemon] watching: *.*
web_1  | [nodemon] starting `node --inspect=0.0.0.0:5858 routes.js`
APP_db_1 exited with code 1
web_1  | Debugger listening on ws://0.0.0.0:5858/96f3e80c-55af-4a4b-a9ea-217203ffbebc
web_1  | For help see https://nodejs.org/en/docs/inspector
web_1  | Server is up and running. Listening on 127.0.0.1:3000!
web_1  | events.js:182
web_1  |       throw er; // Unhandled 'error' event
web_1  |       ^
web_1  |
web_1  | Error: getaddrinfo ENOTFOUND db db:3306
web_1  |     at errnoException (dns.js:50:10)
web_1  |     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
web_1  |     --------------------
web_1  |     at Protocol._enqueue (/API/node_modules/mysql/lib/protocol/Protocol.js:145:48)
web_1  |     at Protocol.handshake (/API/node_modules/mysql/lib/protocol/Protocol.js:52:23)
web_1  |     at Connection.connect (/API/node_modules/mysql/lib/Connection.js:130:18)
web_1  |     at Server.<anonymous> (/API/routes.js:29:14)
web_1  |     at Object.onceWrapper (events.js:314:30)
web_1  |     at emitNone (events.js:105:13)
web_1  |     at Server.emit (events.js:207:7)
web_1  |     at emitListeningNT (net.js:1346:10)
web_1  |     at _combinedTickCallback (internal/process/next_tick.js:135:11)
web_1  |     at process._tickCallback (internal/process/next_tick.js:180:9)
web_1  | [nodemon] app crashed - waiting for file changes before starting...

if I remove connection.connect() I don't get any errors, so the problem is that I can't connect. 如果删除connection.connect() ,则不会出现任何错误,所以问题是我无法连接。 Any help is appreciated as this has been taking all my time to debug and thanks 感谢您的帮助,因为这花费了我所有的时间进行调试,谢谢

EDIT: 编辑:

I had a simple OriginalScenario folder under APP->db->storage with couple empty tables, but I had the same issue; 我在APP->db->storage下有一个简单的OriginalScenario文件夹,里面有几个空表,但是我遇到了同样的问题。 so I moved it thinking docker-compose up might create it for me. 因此,我认为docker-compose up可能会为我创建它。

Two things. 两件事情。 Make sure you APP->db->storage is empty while you are starting. 启动时,请确保APP->db->storage为空。 Next add a MYSQL_DATABASE=OriginalScenario to your db service. 接下来,向您的数据库服务添加MYSQL_DATABASE=OriginalScenario

When you start with mysql you need to have the DB your expecting the node service to connect. 当您从mysql开始时,您需要拥有您希望节点服务连接的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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