You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

后端新手求助:如何正确关闭MongoDB?

Properly Closing MongoDB Connections in Your Express App

Hey there! As someone new to backend dev and MongoDB, it’s totally normal to wonder about graceful shutdowns—let’s fix your current setup so you can be confident your database connections are closed properly.

The Problem With Your Current Approach

Your process.on("exit") handler has a key issue: the exit event fires after Node.js has already started shutting down its event loop. Since mongoose.disconnect() is an asynchronous operation, Node.js won’t wait for it to finish before exiting. That means your database connection might not actually close cleanly, which can lead to connection leaks or unexpected errors on your database server.

A Better, Graceful Shutdown Solution

We need to listen for signals that trigger a shutdown before Node.js exits, and use async/await to ensure all operations complete. Here’s how to adjust your code:

Updated index.js

const server = require("./server");
const mongoose = require("mongoose");
const port = process.env.PORT || 5100;

let appInstance;

// Function to handle graceful shutdown
const gracefulShutdown = async () => {
  console.log("\nInitiating graceful shutdown...");
  
  try {
    // First, close the MongoDB connection
    await mongoose.disconnect();
    console.log("✅ MongoDB disconnected successfully");

    // Then, close the Express server to stop accepting new requests
    if (appInstance) {
      await new Promise((resolve) => appInstance.close(resolve));
      console.log("✅ Express server closed");
    }

    process.exit(0); // Exit with success code
  } catch (err) {
    console.error("❌ Error during shutdown:", err);
    process.exit(1); // Exit with error code
  }
};

// Listen for common termination signals
process.on("SIGINT", gracefulShutdown); // Triggered by Ctrl+C in terminal
process.on("SIGTERM", gracefulShutdown); // Triggered by system/container stop commands

server
  .create()
  .then((app) => {
    appInstance = app.listen(port, () => {
      console.log(`Server has started on port ${port}!`);
    });
  })
  .catch((err) => console.log(err));

What This Does

  • gracefulShutdown Function: This async function ensures we first close the MongoDB connection (waiting for it to finish with await) before shutting down the Express server. Using await guarantees these operations complete before the process exits.
  • Signal Listeners: SIGINT handles when you press Ctrl+C to stop your server locally, and SIGTERM handles production scenarios like when a container orchestrator (Docker/Kubernetes) or process manager (PM2) stops your app.
  • Server Instance Tracking: We save the server instance returned by app.listen() so we can close it properly, preventing new requests from coming in during shutdown.

Quick Notes

  • mongoose.disconnect() is the correct method to close all connections in the Mongoose connection pool—you were using the right method, just in the wrong place!
  • In production, tools like PM2 or Docker will send these termination signals automatically, so this setup works seamlessly there.

内容的提问来源于stack exchange,提问作者ViP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:18:12