Docker Compose Sql DB映像连接错误

时间:2018-11-12 09:58:44

标签: mysql node.js docker docker-compose sequelize.js

我有一个docker-compose.yml文件,我想创建三个容器来运行我的节点应用程序:客户端,服务器,带有某些数据的数据库容器。

我在创建db容器并将其连接到服务器容器时遇到麻烦,我正在使用sequelize与db连接,但是在docker-compose up命令中,服务器应用程序启动时出现连接错误:

  

server_1 |无法连接到SQL数据库:webgrit_superactivation {SequelizeConnectionRefusedError:连接ECONNREFUSED 127.0.0.1:3306

这是我的docker-compose文件:

version: "3"

services:
  # client
  client:
    image: "ngapp"
    build: "client/dist/."
    ports:
      - "4200:80"
  # Server
  server:
    image: "exapp"
    build: "server/."
    ports:
      - "3000:3000"
    links:
      - db
  # MySQL
  db:
    image: mysql:5.7
    command: mysqld --user=root
    volumes: 
      - ./mysql-dump:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypass
      MYSQL_DATABASE: mydbname

在mysql映像中,我使用data.sql转储文件来初始化数据库,但是出了点问题

这是我用于连接mysql db的服务器容器dbconfig.js文件:

CONFIG.db_name = process.env.DB_NAME_DEV || "mydbname";
CONFIG.db_user = process.env.DB_USER_DEV || "myuser";
CONFIG.db_password = process.env.DB_PASSWORD_DEV || "mypass";
CONFIG.db_dialect = process.env.DB_DIALECT_DEV || "mysql";
CONFIG.db_host = process.env.DB_HOST_DEV || "localhost";
CONFIG.db_port = process.env.DB_PORT_DEV || "3306";

这是我的data.sql转储文件的开始:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

CREATE DATABASE IF NOT EXISTS `mydbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `mydbname`;

CREATE TABLE `users` (
...

编辑:在这里您可以找到错误日志:https://github.com/ufollettu/SEANSA/blob/master/docker%20error%20log.log

我是Docker的新手,如何解决此问题? 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

将主机更改为数据库服务名称,即db-

CONFIG.db_host = process.env.DB_HOST_DEV || "db";