Node.JS Express connect to Mysql (both Docker) ECONNREFUSED



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

供参考。它们都运行在docker

当express在我的本地运行时,它工作,但当试图在docker中运行时不工作

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

app.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/

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

相关内容

  • 没有找到相关文章