【发布时间】:2016-10-10 00:09:55
【问题描述】:
我在 CentOS 上通过 ssh 有远程服务器,我在端口 27017 上有一个 test mongo 数据库。
我想在同一个端口或另一个端口上运行另一个数据库,以便在我的应用程序中同时使用两个数据库。
【问题讨论】:
标签: node.js mongodb mongoose centos
我在 CentOS 上通过 ssh 有远程服务器,我在端口 27017 上有一个 test mongo 数据库。
我想在同一个端口或另一个端口上运行另一个数据库,以便在我的应用程序中同时使用两个数据库。
【问题讨论】:
标签: node.js mongodb mongoose centos
您可以在一个 mongod 实例上运行多个数据库,如果您只需要另一个数据库,则无需启动多个实例。
你不能在同一个端口上运行多个进程,因此你不能在 27017 上运行另一个 mongod。虽然你可以在不同的端口上启动另一个实例,但我不确定你为什么要这样做,除非你'正在尝试创建副本集。
【讨论】:
我在一个连接上使用 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);
});
});
});
};
【讨论】: