0

我正在尝试将 express 连接到在 docker 中运行的 mysql

供参考。他们都在docker中运行

当 express 在我的本地运行时有效,但在尝试在 docker 中运行时无效

来自 docker 的错误代码: error connecting: Error: connect ECONNREFUSED 127.0.0.1:3306

应用程序.js

var mysql = require('mysql')
var connection = mysql.createConnection({
  host: '127.0.0.1',
  user: 'admini',
  password: 'admini',
  database: 'cooper',
  port:'3306',
  connectionLimit: 10
})

docker-compose.yaml

services:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    ports: 
      - 3306:3306

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

4

1 回答 1

0

尝试从其他 docker 容器连接时,尝试将主机从切换127.0.0.1db(您在 docker-compose 中设置的名称)。

于 2021-08-31T17:33:42.963 回答