leabum
leabum

Reputation: 395

MongoDB replica-set starts on DESKTOP instead of localhost

I want to use transactions with mongoose, therefore I installed run-rs to test it in the development stage. According to guides like this one https://medium.com/cashpositive/the-hitchhikers-guide-to-mongodb-transactions-with-mongoose-5bf8a6e22033 run-rs starts it on the localhost. When I run run-rs -v 4.2.5 --keep --shell to start the replica-set via the terminal it says: Started replica set on "mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs". Why is it saying DESKTOP for me instead of localhost and how would I connect to it in mongoose.connect()?

I tried the following in my 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
});

Also how would I append the databasename?

UPDATE:

PS C:\Users\Workstation\project> npm start

> [email protected] 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.

UPDATE I accepted the answer below, because the string for connecting works, hence the original question is solved. However I switched to Linux and it works now without further setup. run-rs didn't work on my Windows machine it seems.

Upvotes: 0

Views: 489

Answers (1)

Pardeep Baboria
Pardeep Baboria

Reputation: 468

replace:

const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'

with:

const dbUri = 'mongodb://localhost:27017,localhost,localhost:27019/databasenaeme'

Also need to run mongoDb at these port manully. use link for refrence:

link

Upvotes: 0

Related Questions