【问题标题】:How to run multiple mongodb databases on remote server如何在远程服务器上运行多个 mongodb 数据库
【发布时间】:2016-10-10 00:09:55
【问题描述】:

我在 CentOS 上通过 ssh 有远程服务器,我在端口 27017 上有一个 test mongo 数据库。

我想在同一个端口或另一个端口上运行另一个数据库,以便在我的应用程序中同时使用两个数据库。

【问题讨论】:

    标签: node.js mongodb mongoose centos


    【解决方案1】:

    您可以在一个 mongod 实例上运行多个数据库,如果您只需要另一个数据库,则无需启动多个实例。

    你不能在同一个端口上运行多个进程,因此你不能在 27017 上运行另一个 mongod。虽然你可以在不同的端口上启动另一个实例,但我不确定你为什么要这样做,除非你'正在尝试创建副本集。

    【讨论】:

    • 同意 robjwilkins。您所要做的就是在获得 db 连接时,使用不同的数据库名称,这样对于在同一端口上运行的不同数据库,您将拥有不同的“db”连接。
    【解决方案2】:

    我在一个连接上使用 graphQL 运行多个 mongodb 数据库。 这应该会给你一些想法:

    连接:

    import {MongoClient} from 'mongodb';
    import assert from 'assert';
    import graphqlHTTP from 'express-graphql';
    const server_url = 'mongodb://localhost:27017/contest_server';
    const client_url = 'mongodb://localhost:27017/contest_client';
    
    // Establish connection to serverPool
    module.exports = (app, PORT, ncSchema) => {
        MongoClient.connect(server_url, (serverErr, serverPool) => {
            assert.equal(null, serverErr);
            MongoClient.connect(client_url, (clientErr, clientPool) => {
                assert.equal(null, serverErr);
                app.use('/graphql', graphqlHTTP({
                    schema: ncSchema,
                    context: { serverPool, clientPool },
                    graphiql: true
                }));
    
                app.listen(PORT, () => {
                    console.log(`Server is listening on port ${PORT}`)
                });
    
                serverPool.collection("votes").count((err, count) => {
                    console.log(count);
                });
    
                clientPool.collection("counts").count((err, count) => {
                    console.log(count);
                });
            });
        });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2020-09-10
      • 2016-10-25
      • 2020-03-28
      • 2011-04-11
      • 2015-12-23
      • 1970-01-01
      相关资源
      最近更新 更多