【发布时间】:2020-03-12 01:21:24
【问题描述】:
我目前正面临一种我无法理解的非常奇怪的行为。我的设置是:一个微服务,使用节点 10.18.1 和 knex js v0.20.10,用于访问 mysql 5.7 db
错误 对于本地测试,我有一个 docker-compose 文件,它创建了包括我的项目在内的所有图像。我遇到的问题是,每次我尝试从数据库中获取信息时,我的项目图像都会被杀死。查看日志后,我看到了来自 mysql 的以下消息:
mysql | 2020-03-12T00:40:18.457996Z 2 [Note] Aborted connection 2 to db: <DB_NAME> user: 'root' host: '172.18.0.3' (Got an error reading communication packets)
我能够将问题缩小到以下代码部分:
async function getUser (condition) {
console.log('>> here', condition)
const users = await knexClient(TABLE_NAME).where(condition) //<-- all good here
const user = users[0] //<-- still nothing wrong
console.log('>', user) // <-- i can see the user from the DB
await sleep(10000) // <-- It can even wait for 10 s
return users.pop() // <-- BOOM!
// return null // <-- If i return null, there is no error!?!?!? wtf?
}
```
函数调用
async function getByEmail (email) {
**const user = await getUser({ email })**
if (user == null) {
throw applicationErrors.notFoundError('User not found or is not activated')
}
return user
}
基本上我面临的问题是,每次我尝试在数据库中返回找到的用户时,我都会收到 mysql 错误(读取通信数据包时出错)。发生此错误后,我的 docker 映像停止,因为正在以状态码 0 退出。
趣事
当我运行相同的源代码,使用相同的 Knex 配置,指向本地 mysql docker 镜像时,永远不会发生错误!
我的 docker 文件如下:
FROM node:10-alpine
ADD . /app
WORKDIR /app
RUN npm i --only=prod
RUN rm -rf ./config/local.json
RUN rm -rf ./docker-compose.override.yml
CMD ["npm", "start"]
我的 docker-compose 看起来像这样:
version: "3"
services:
app:
build: .
container_name: <SERVICE_NAME>
ports:
- "9000:9000"
# expose:
# - 9000
depends_on:
- mysql
mysql:
image: mysql:5.7.13
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
volumes:
- mysql:/var/lib/mysql
- "./infrastructure/docker/initdb.sql:/docker-entrypoint-initdb.d/init.sql"
environment:
- MYSQL_ROOT_PASSWORD=password
ports:
- "3306:3306"
# expose:
# - 3306
volumes:
mysql:
任何帮助或建议,将不胜感激!
【问题讨论】: