问题描述:

I have remote server on CentOS via ssh and I have one test mongo database on port 27017.

I would like to run another databases on the same port or maybe on another port to use two databases in my application at the same time.

网友答案:

You can run multiple databases on one instance of mongod, there is no need to start up more than one instance if all you need is another database.

You cannot run more than one process on the same port, therefore you cant run another mongod on 27017. You could start up another instance on a different port though, but I'm not sure why you would want to unless you're trying to create a replicaset.

网友答案:

I am running multiple mongodb databases using graphQL on one connection. This should give you some idea:

connection:

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);
            });
        });
    });
};
相关阅读:
Top