MongoDB 副本集在 DESKTOP 而不是本地主机上启动
MongoDB replica-set starts on DESKTOP instead of localhost
我想使用 mongoose 的交易,所以我在开发阶段安装了 运行-rs 来测试它。根据这样的指南 https://medium.com/cashpositive/the-hitchhikers-guide-to-mongodb-transactions-with-mongoose-5bf8a6e22033 运行-rs 在本地主机上启动它。当我 运行 run-rs -v 4.2.5 --keep --shell
通过终端启动副本集时,它说:在 "mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs" 上启动副本集。为什么它对我说桌面而不是本地主机,我如何在 mongoose.connect()?
中连接到它
我在 index.js 中尝试了以下方法:
const app = express();
const PORT = 27017;
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
mongoose.Promise = global.Promise;
mongoose.connect(dbUri, {
replicaSet: 'rs',
useNewUrlParser: true,
useUnifiedTopology: true
});
另外,我将如何附加数据库名称?
更新:
PS C:\Users\Workstation\project> npm start
> project@1.0.0 start C:\Users\Workstation\project
> nodemon ./index.js --exec babel-node -e js
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js
[nodemon] starting `babel-node ./index.js`
Your server is running on Port 4000
(node:7436) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27018
at new MongooseServerSelectionError (C:\Users\Workstation\project\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\Workstation\ project\node_modules\mongoose\lib\connection.js:823:32)
at Mongoose.connect (C:\Users\Workstation\ project\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:/Users/Workstation/project/index.js:22:10)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at loader (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.<computed> [as .js] (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at Object.<anonymous> (C:\Users\Workstation\project\node_modules\babel-cli\lib\_babel-node.js:154:22)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
(node:7436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
更新
我接受了下面的答案,因为用于连接的字符串有效,因此解决了原始问题。但是我切换到 Linux 并且现在无需进一步设置即可使用。 运行-rs 似乎无法在我的 Windows 机器上运行。
替换:
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
与:
const dbUri = 'mongodb://localhost:27017,localhost,localhost:27019/databasenaeme'
还需要在这些端口手动运行 mongoDb。使用 link 作为参考:
link
我想使用 mongoose 的交易,所以我在开发阶段安装了 运行-rs 来测试它。根据这样的指南 https://medium.com/cashpositive/the-hitchhikers-guide-to-mongodb-transactions-with-mongoose-5bf8a6e22033 运行-rs 在本地主机上启动它。当我 运行 run-rs -v 4.2.5 --keep --shell
通过终端启动副本集时,它说:在 "mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs" 上启动副本集。为什么它对我说桌面而不是本地主机,我如何在 mongoose.connect()?
我在 index.js 中尝试了以下方法:
const app = express();
const PORT = 27017;
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
mongoose.Promise = global.Promise;
mongoose.connect(dbUri, {
replicaSet: 'rs',
useNewUrlParser: true,
useUnifiedTopology: true
});
另外,我将如何附加数据库名称?
更新:
PS C:\Users\Workstation\project> npm start
> project@1.0.0 start C:\Users\Workstation\project
> nodemon ./index.js --exec babel-node -e js
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js
[nodemon] starting `babel-node ./index.js`
Your server is running on Port 4000
(node:7436) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27018
at new MongooseServerSelectionError (C:\Users\Workstation\project\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\Workstation\ project\node_modules\mongoose\lib\connection.js:823:32)
at Mongoose.connect (C:\Users\Workstation\ project\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:/Users/Workstation/project/index.js:22:10)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at loader (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.<computed> [as .js] (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at Object.<anonymous> (C:\Users\Workstation\project\node_modules\babel-cli\lib\_babel-node.js:154:22)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
(node:7436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
更新 我接受了下面的答案,因为用于连接的字符串有效,因此解决了原始问题。但是我切换到 Linux 并且现在无需进一步设置即可使用。 运行-rs 似乎无法在我的 Windows 机器上运行。
替换:
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
与:
const dbUri = 'mongodb://localhost:27017,localhost,localhost:27019/databasenaeme'
还需要在这些端口手动运行 mongoDb。使用 link 作为参考:
link