我正在尝试将 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/