欢迎光临
我们一直在努力

MongoDB(12)如何启动和停止MongoDB服务?

启动和停止MongoDB服务的方式因操作系统的不同而有所不同。下面我们分别介绍在Ubuntu(及其他基于systemd的Linux系统)、Windows和macOS上的具体操作。

在Ubuntu(及其他基于systemd的Linux系统)上

启动MongoDB服务
  • 启动服务

    使用systemctl命令启动MongoDB服务:

    sudo systemctl start mongod

  • 验证服务是否启动

    使用以下命令检查MongoDB服务的状态:

    sudo systemctl status mongod

    示例输出:

    ● mongod.service – MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: active (running) since Mon 2023-10-02 12:34:56 UTC; 10s ago
    Docs: https://docs.mongodb.org/manual

  • 停止MongoDB服务
  • 停止服务

    使用systemctl命令停止MongoDB服务:

    sudo systemctl stop mongod

  • 验证服务是否停止

    使用以下命令检查MongoDB服务的状态:

    sudo systemctl status mongod

    示例输出:

    ● mongod.service – MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: inactive (dead) since Mon 2023-10-02 12:36:56 UTC; 10s ago
    Docs: https://docs.mongodb.org/manual

  • 重新启动MongoDB服务
  • 重新启动服务

    使用systemctl命令重新启动MongoDB服务:

    sudo systemctl restart mongod

  • 在Windows上

    启动MongoDB服务
  • 通过命令提示符启动服务

    打开命令提示符,以管理员身份运行以下命令启动MongoDB服务:

    net start MongoDB

  • 验证服务是否启动

    使用以下命令检查MongoDB服务的状态:

    sc query MongoDB

    示例输出:

    SERVICE_NAME: MongoDB
    TYPE : 10 WIN32_OWN_PROCESS
    STATE : 4 RUNNING

  • 停止MongoDB服务
  • 通过命令提示符停止服务

    打开命令提示符,以管理员身份运行以下命令停止MongoDB服务:

    net stop MongoDB

  • 验证服务是否停止

    使用以下命令检查MongoDB服务的状态:

    sc query MongoDB

    示例输出:

    SERVICE_NAME: MongoDB
    TYPE : 10 WIN32_OWN_PROCESS
    STATE : 1 STOPPED

  • 重新启动MongoDB服务
  • 通过命令提示符重新启动服务

    打开命令提示符,以管理员身份运行以下命令重新启动MongoDB服务:

    net stop MongoDB
    net start MongoDB

  • 在macOS上

    启动MongoDB服务
  • 使用Homebrew启动服务

    如果通过Homebrew安装的MongoDB,可以使用以下命令启动MongoDB服务:

    brew services start mongodb/brew/mongodb-community

  • 验证服务是否启动

    使用以下命令检查MongoDB服务的状态:

    brew services list

    示例输出:

    Name Status User Plist
    mongodb-community started user ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist

  • 停止MongoDB服务
  • 使用Homebrew停止服务

    使用以下命令停止MongoDB服务:

    brew services stop mongodb/brew/mongodb-community

  • 验证服务是否停止

    使用以下命令检查MongoDB服务的状态:

    brew services list

    示例输出:

    Name Status User Plist
    mongodb-community stopped

  • 重新启动MongoDB服务
  • 使用Homebrew重新启动服务

    使用以下命令重新启动MongoDB服务:

    brew services restart mongodb/brew/mongodb-community

  • 使用命令行参数启动MongoDB实例

    除了使用服务管理器(如systemd、Windows服务或Homebrew服务)管理MongoDB服务外,还可以直接使用mongod命令启动MongoDB实例。

    启动MongoDB实例

    使用默认设置启动MongoDB实例:

    mongod –dbpath /path/to/your/db –logpath /path/to/your/log –fork

    停止MongoDB实例

    可以使用从db.shutdownServer()命令在MongoDB shell中停止实例:

    mongo –eval "db.shutdownServer()"

    总结

    启动和停止MongoDB服务的步骤如下:

    • Ubuntu(及其他基于systemd的Linux系统):

      • 启动:sudo systemctl start mongod
      • 停止:sudo systemctl stop mongod
      • 重新启动:sudo systemctl restart mongod
    • Windows:

      • 启动:net start MongoDB
      • 停止:net stop MongoDB
      • 重新启动:net stop MongoDB && net start MongoDB
    • macOS:

      • 启动:brew services start mongodb/brew/mongodb-community
      • 停止:brew services stop mongodb/brew/mongodb-community
      • 重新启动:brew services restart mongodb/brew/mongodb-community

    这些命令和代码示例可以帮助你在不同操作系统上管理MongoDB服务的启动和停止,从而确保MongoDB服务的正常运行。

    赞(0)
    未经允许不得转载:171主机测评 » MongoDB(12)如何启动和停止MongoDB服务?
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址