我正在尝试将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/发布于 2021-08-31 17:33:43
尝试从其他docker容器连接时,尝试将主机从127.0.0.1切换到db (您在docker-compose中设置的名称)。
https://stackoverflow.com/questions/69002684
复制相似问题