docker-compose:nodejs + mysql无法连接mysql
Posted
技术标签:
【中文标题】docker-compose:nodejs + mysql无法连接mysql【英文标题】:docker-compose: nodejs + mysql can't connect mysql 【发布时间】:2018-10-10 17:06:57 【问题描述】:我尝试 dockerlized 我自己的节点应用程序,但无法连接 mysql 容器。我的代码:
docker-compose.yml
version: '3.2'
services:
node:
build: ./
ports:
- "8787:8787"
depends_on:
- db
networks:
- docker_xxx
environment:
- PORT=8787
- DATABASE_HOST=db
- DATABASE_PASSWORD=xxx
- EGG_SERVER_ENV=local
- NODE_ENV=development
# command: ["./wait-for-it.sh", "db:3306", "--", "npm run docker"]
db:
build: ./db
networks:
- docker_xxx
environment:
- MYSQL_ROOT_PASSWORD=xxx
- MYSQL_DATABASE=database
- MYSQL_USER=user
- MYSQL_PASSWORD=passwd
networks:
docker_xxx:
driver: bridge
./Dockerfile(用于 nodejs)
FROM node:8.9.4-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install --production
COPY . /usr/src/app
# COPY wait-for-it.sh /usr/src/app
EXPOSE 8787
CMD npm run docker
db/Dockerfile (mysql)
FROM mysql:5.6
ADD honggang.sql /docker-entrypoint-initdb.d
config/config.default.js
config.mysql =
// mysql settings
client:
// host
host: process.env.DATABASE_HOST || '127.0.0.1',
// port
port: '3306',
// username
user: 'root',
// password
password: 'xxx',
database: 'xxx',
charset: 'utf8',
dialectOptions:
collate: 'utf8_general_ci',
,
,
app: true,
agent: false,
;
我运行 docker-compose up -d
,只有 db
运行。
我运行docker logs hash
来查找错误,它显示以下信息:
2018-04-30 14:43:51,334 ERROR 54 nodejs.ECONNREFUSEDError: connect ECONNREFUSED 172.19.0.2:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
--------------------
at Protocol._enqueue (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at PoolConnection.connect (/usr/src/app/node_modules/mysql/lib/Connection.js:130:18)
at Pool.getConnection (/usr/src/app/node_modules/mysql/lib/Pool.js:48:16)
at /usr/src/app/node_modules/ali-rds/node_modules/pify/index.js:29:7
at new Promise (<anonymous>)
at Pool.<anonymous> (/usr/src/app/node_modules/ali-rds/node_modules/pify/index.js:12:10)
at Pool.ret [as getConnection] (/usr/src/app/node_modules/ali-rds/node_modules/pify/index.js:56:34)
at Pool.query (/usr/src/app/node_modules/mysql/lib/Pool.js:202:8)
at /usr/src/app/node_modules/ali-rds/node_modules/pify/index.js:29:7
sql: select now() as currentTime;
code: 'ECONNREFUSED'
errno: 'ECONNREFUSED'
syscall: 'connect'
address: '172.19.0.2'
port: 3306
fatal: true
name: 'ECONNREFUSEDError'
pid: 54
hostname: d9cd95667a5d
我加了CMD ping db
,它回复了。
我尝试使用wait-for-it.sh
(刚刚注释掉的代码),但得到了错误信息:
env: can't execute 'bash': No such file or directory
【问题讨论】:
主机:process.env.DATABASE_HOST || '127.0.0.1', => host: 'db' 至少我是这样做的 哦,nvm,我看到了环境变量。检查它是否包含“db”。还有这个数据库:'xxx', 【参考方案1】:我解决了https://github.com/ycjcl868/eggjs-mysql-docker
有几点:
1.apk add --no-cache bash
运行wait-for-it.sh
等待mysql服务器ok
2.hostname
是0.0.0.0
不是localhost
/127.0.0.1
【讨论】:
以上是关于docker-compose:nodejs + mysql无法连接mysql的主要内容,如果未能解决你的问题,请参考以下文章
docker-compose RabbitMQ与Nodejs接收端同时运行时的错误
docker-compose ECONNREFUSED 用于 nodeJS 上的 Postgres
使用 docker-compose 将 NodeJS-App 连接到 MongoDB
如何在 docker-compose 上使用 nodejs 在 mongoDB 上设置身份验证?
尝试通过 docker-compose 将 NodeJS 应用程序连接到 MySQL 映像时出现 ECONNREFUSED
Docker-compose.yml for NodeJs with MySQL on AWS Elastic Beanstalk 单容器 Docker