【问题标题】:Create Two Separate MongoDB instances on localhost在 localhost 上创建两个单独的 MongoDB 实例
【发布时间】:2013-10-08 19:45:09
【问题描述】:

我正在尝试找到一种方法来拥有两个单独的本地托管 mongoDB 实例,这样一个中的数据库就不会影响另一个中的数据库。我尝试过使用不同的端口,但这仍然会导致一个数据库的更改影响另一个数据库,但两者都需要在本地托管。我在 mongodb.org 上看到我应该更改配置文件,但我不确定它在哪里或如何编辑它(是的,我知道它将是文本或 xml 文件的某个版本。)如果你有一个示例会愿意张贴我会很感激。我正在使用 robomongo 的 Windows 机器上工作,我使用 c# 4.0 restful 服务访问数据库

编辑:我还希望能够同时打开两个连接。

【问题讨论】:

    标签: c# mongodb localhost


    【解决方案1】:

    您只需要确保每个实例都有自己的数据目录、端口和日志文件。

    在启动进程之前,确保您指定的所有目录都已经存在。例如,如果 Mongod 找不到您在 --dbpath 中指定的文件夹,它将抛出错误并退出。

    如果您使用的是 linux 机器,则可以使用 --fork 启动多个 mongod 进程 from a single command session。 Windows 使用start 命令执行此操作。假设 mongod.exe 在您的路径上:

    start mongod.exe --logpath C:\logs\log1.log --dbpath C:\data\db1 --port 27017
    start mongod.exe --logpath C:\logs\log2.log --dbpath C:\data\db2 --port 27018
    start mongod.exe --logpath C:\logs\log3.log --dbpath C:\data\db3 --port 27019
    

    这些命令中的每一个都将生成一个新的命令窗口,该窗口运行带有指定参数的mongod 进程。如果您想将 MongoDB 作为 Windows 服务运行,请查看 the documentation

    【讨论】:

    • 能否请您详细说明将 mongodb 作为 Windows 服务运行意味着什么
    • 这是一种管理长时间运行程序的方法。每MSDN:Microsoft Windows services [...] enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.
    【解决方案2】:

    您甚至不需要更改配置文件(这仅适用于在默认端口上作为服务运行的 mongod。您可以简单地从命令行使用配置参数启动其他 mongod 实例(我没有用windows,不过应该和linux差不多)

    >  mongod --logpath "log/foo.log" --dbpath data/foo --port 27020 --fork
    >  mongod --logpath "log/bar.log" --dbpath data/bar --port 27021 --fork
    

    【讨论】:

    • --fork 不适合我。这个扩展有什么作用?这个命令(至少没有 --fork)不允许我打开两个连接。抱歉刚刚添加了
    • --fork 是一个特定于 linux 的命令,它将进程与父进程分离,它在 Windows 上没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2012-03-27
    相关资源
    最近更新 更多