NodeJS应用请求MSSQL数据库时崩溃问题求助
旧NodeJS应用MSSQL连接崩溃问题排查
公司遗留的旧NodeJS应用,Docker镜像可正常构建启动,操作PostgreSQL无问题,但请求MSSQL服务器时直接崩溃。本人几乎无NodeJS经验,求排查建议。
Docker构建输出
docker build -t fixver . Sending build context to Docker daemon 2.427MB Step 1/21 : FROM node:16-alpine as base ---> c4ee3c9d7bc1 Step 2/21 : ARG NODE_ENV=production ---> Using cache ---> ba79cfac2e2c Step 3/21 : ENV NODE_ENV=${NODE_ENV} NODE_OPTIONS="--max_old_space_size=8192" ---> Using cache ---> 1a344f8791d8 Step 4/21 : WORKDIR /usr/src/app ---> Using cache ---> 1c591a772bcd Step 5/21 : FROM base as clientBuilder ---> 1c591a772bcd Step 6/21 : COPY ./client/package.json ./client/yarn.lock ./ ---> Using cache ---> 7382b944fcc0 Step 7/21 : RUN yarn install --production=false --frozen-lockfile ---> Using cache ---> 431700be035b Step 8/21 : COPY ./client . ---> Using cache ---> 87790e7c061a Step 9/21 : RUN yarn build ---> Using cache ---> 9ba70dd8301c Step 10/21 : FROM base as serverBuilder ---> 1c591a772bcd Step 11/21 : COPY ./server/package.json ./server/yarn.lock ./ ---> Using cache ---> bf5dc70ee2eb Step 12/21 : RUN yarn install --production=false --frozen-lockfile ---> Using cache ---> c26b02f2af5c Step 13/21 : COPY ./server . ---> 16fdc772d650 Step 14/21 : RUN yarn build ---> Running in 676753d20a77 yarn run v1.22.19 $ node_modules/.bin/rimraf dist $ node_modules/.bin/nest build Done in 5.75s. Removing intermediate container 676753d20a77 ---> 66a53b6af7cd Step 15/21 : FROM base as production ---> 1c591a772bcd Step 16/21 : COPY ./server/package.json ./server/.env ./ ---> dbbbe7295cfd Step 17/21 : RUN yarn install --pure-lockfile ---> Running in f3b965d1ec0d yarn install v1.22.19 info No lockfile found. [1/4] Resolving packages... warning Resolution field "uuid@8.3.2" is incompatible with requested version "uuid@^3.1.0" warning jest > jest-cli > jest-config > jest-environment-jsdom > jsdom > w3c-hr-time@1.0.2: Use your platform's native performance.now() and performance.timeOrigin. [2/4] Fetching packages... [3/4] Linking dependencies... warning " > ts-loader@9.4.2" has unmet peer dependency "webpack@^5.0.0". [4/4] Building fresh packages... Done in 39.04s. Removing intermediate container f3b965d1ec0d ---> 8d804071a8d2 Step 18/21 : COPY --from=serverBuilder /usr/src/app/dist ./ ---> f760fda4b1a1 Step 19/21 : COPY --from=clientBuilder /usr/src/app/build ./public ---> 4f697bab8bc6 Step 20/21 : EXPOSE 80 ---> Running in 95fae0df9266 Removing intermediate container 95fae0df9266 ---> 4bebff7957bc Step 21/21 : CMD ["node", "./main"] ---> Running in 19b4df36b37e Removing intermediate container 19b4df36b37e ---> 362fbadf0b4a Successfully built 362fbadf0b4a Successfully tagged fixver:latest
应用启动日志
docker run -p 7007:80 fixver [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [NestFactory] Starting Nest application... [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] AppModule dependencies initialized +60ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] PassportModule dependencies initialized +1ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ServeStaticModule dependencies initialized +0ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] AuthModule dependencies initialized +8ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] PagesModule dependencies initialized +0ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [InstanceLoader] ScriptsModule dependencies initialized +1ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RoutesResolver] ScriptsController {/api/scripts}: +8ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts, GET} route +4ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id, GET} route +1ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id/check, POST} route +0ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/scripts/:id/apply, POST} route +1ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RoutesResolver] PagesController {/api/pages}: +1ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [RouterExplorer] Mapped {/api/pages/:id, GET} route +0ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG [NestApplication] Nest application successfully started +5ms [Nest] 1 - 12/01/2022, 1:49:54 PM LOG Server started! Port: 80
MSSQL请求错误日志
node:events:491 throw er; // Unhandled 'error' event ^ Error: No event 'featureExtAck' in state 'SentLogin7WithNTLMLogin' at Connection.dispatchEvent (/usr/src/app/node_modules/tedious/lib/connection.js:1663:26) at Parser.<anonymous> (/usr/src/app/node_modules/tedious/lib/connection.js:1224:12) at Parser.emit (node:events:513:28) at Readable.<anonymous> (/usr/src/app/node_modules/tedious/lib/token/token-stream-parser.js:27:14) at Readable.emit (node:events:513:28) at addChunk (node:internal/streams/readable:315:12) at readableAddChunk (node:internal/streams/readable:289:9) at Readable.push (node:internal/streams/readable:228:10) at next (node:internal/streams/from:98:31) at processTicksAndRejections (node:internal/process/task_queues:96:5) Emitted 'error' event on Readable instance at: at emitErrorNT (node:internal/streams/destroy:157:8) at emitErrorCloseNT (node:internal/streams/destroy:122:3) at processTicksAndRejections (node:internal/process/task_queues:83:21)
排查建议
- 锁定依赖版本:Docker生产构建阶段未复制
yarn.lock,导致依赖版本和构建阶段不一致,可能引发tedious库兼容性问题。修改Dockerfile的production阶段,添加COPY ./server/yarn.lock ./后再执行yarn install --pure-lockfile,保证依赖版本统一。 - 检查tedious与MSSQL版本兼容性:错误来自tedious的连接状态处理,该库不同版本对应不同的SQL Server版本。查看
server/package.json中的tedious版本,对照MSSQL服务器版本调整到兼容范围。 - 添加错误捕获逻辑:应用崩溃是因为未处理的error事件,在MSSQL连接代码中添加
connection.on('error', (err) => { console.error(err); }),避免进程直接退出,同时获取更详细的错误上下文。 - 切换认证方式测试:错误状态显示使用NTLM认证失败,尝试在MSSQL连接配置中改用SQL Server账号密码认证,排查是否是NTLM认证的配置或环境问题。
- 更换基础镜像测试:Alpine镜像缺少部分系统依赖(如kerberos相关库),可能影响NTLM认证。改用
node:16(非Alpine)镜像构建,测试是否能正常连接MSSQL。
内容的提问来源于stack exchange,提问作者MAzZY
相关产品推荐
相关产品推荐

