Cannot connect to mongo on heroku due to missing replicaset parameter - node.js

I have a node.js app running on heroku. I am trying to update my mongodb module to "~2.0.3". In my local dev. environment everything is working fine. However, when I deploy the to Heroku I get a "replicaSet parameter must be set" error which did not fire with earlier version of the mongodb module. I am not how to go about it. In particular, I have no idea where can I find the name of that replicaSet.
Here's a transcript of a node REPL session showing the problem:
$ heroku run node --app my-app
Running `node` attached to terminal... up, run.8299
>var mongo = require('mongodb');
undefined
> var a = {}
undefined
> process.env.MONGOLAB_URI
'mongodb://UUUUU:PPPPP.mongolab.com:45970/heroku_appNNNNNNN'
> mongo.MongoClient.connect(process.env.MONGOLAB_URI, function (err, db) {
a.err = err; a.db = db; console.log('CALLED'); });
undefined
> CALLED
undefined
> a
{ err:
{ [MongoError: replicaSet parameter must be set]
name: 'MongoError',
message: 'replicaSet parameter must be set' },
db: null }
>
One more thing: when I access the DB through the web interface I see only two system collections: system.indexes and system.users. To the best of my understanding this suggests that my DB is not part of a replicaSet which makes the whole thing weird.

This is indeed a problem in 2.0.3. It was solved in 2.0.4 by this pull request.
Bottom line: If you encounter "replicaSet parameter must be set" error in 2.0.3, upgrade to 2.0.4.

Related

Authentication problem with connecting to mongodb Atlas

What I want to accomplish is probably the simplest thing ever. I just want to connect to mongodb atlas free tier M0 in nodejs using mongodb driver. I already have created a free account and cluster.
Here is my simple code:
const mongoclient = require('mongodb').MongoClient;
const URI = 'mongodb+srv://mohammed:mypassword#lebran-0qf7m.gcp.mongodb.net/test?retryWrites=true';
mongoclient.connect(URI, { useNewUrlParser: true }, (err, client) => {
if(err) throw err;
console.log('mongodb connected');
db = client.db('mern');
})
Here is what happens when I run the file:
Mohammeds-MacBook-Pro:mern ahmed$ node server.js
/Users/ahmed/Projects/mern/server.js:13
if(err) throw err;
^
Error: queryTxt ESERVFAIL lebran-0qf7m.gcp.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:199:19)
I also tried mongo shell with the following command:
mongo "mongodb+srv://lebran-0qf7m.gcp.mongodb.net/mern" --username mohammed
here is the output I get:
MongoDB shell version v4.0.6
Enter password:
connecting to: mongodb://lebran-shard-00-00-0qf7m.gcp.mongodb.net.:27017,lebran-shard-00-01-0qf7m.gcp.mongodb.net.:27017,lebran-shard-00-02-0qf7m.gcp.mongodb.net.:27017/mern?gssapiServiceName=mongodb&ssl=true
2019-02-20T22:48:57.232+0300 E QUERY [js] Error: Authentication failed. :
connect#src/mongo/shell/mongo.js:343:13
#(connect):1:6
exception: connect failed
I am sure I enter my password right. I have changed several ones and none of them work. I have my IP listed as white list. I also have an admin account. All the prerequisites I can think of.
I am able to connect to the local server but not to the Atlas. This really beginning to drive me crazy.
Any help is greatly appreciated...
**UPDATE:
I just deployed my app to heroku and you know what? It connects!! I could not get it to connect using my computer though. So probably it has to do something with my location or my connection, probably the location.

strapi start: "url is not defined" - using mlab mongo DB

When I run 'strapi start' I get the following output in my terminal:
debug ⛔️ Server wasn't able to start properly.
Error URL is not defined
Any solutions? I have recreated my mlab DB 3 times and when I enter all the values in the terminal to create the new strapi server I double check my values and everything from GitHub issues and strapi documentations say I'm doing the correct thing, but not able to figure out why this is happening! Appreciate any help!
As #soupette have said in the comment:
Strapi needs Node v10 to be installed in order to work. If you upgrade
your node version it should be working properly
I can confirm the same error on my end with NodeJS 8.11.1
Strapi's GitHub page confirms that minimum NodeJS version is 10.x
Ok when you create a Mondo db on mlab you get this information:
mongo ds129484.mlab.com:29484/blog -u <dbuser> -p <dbpassword>
mongodb://<dbuser>:<dbpassword>#ds129484.mlab.com:29484/blog
When you start a strapi project, you make this
strapi new blog
cd blog
strapi start
So you select mondodb how to default db and the cli show the follow questions, and you would be add the following answers, supposing that you have the db data showed before:
DBname: blog
host: ds129484.mlab.com
srv connection: false
port: 29484
username: username created on mlab for your db
password: username password created on mlab for your db
authentication db: blog (you have put the name of your db)
enable ssl: false
You press Enter and you project would be run perfectly
First of all you must have Node 10 or above. If you still getting error edit the following file:
config/host.json
Use an higher timeout value.
{
"timeout": 60000,
......
}
Assuming you are on the correct node version, goto the configuration.js found in node_modules/strapi/lib/core/configurations.js and update lines 331-333.
Replace
this.config.admin.url = this.config.admin.devMode ?
(new URL(adminPath, `http://${this.config.host}:4000`)).toString():
(new URL(adminPath, url)).toString();
With
this.config.admin.url = this.config.admin.devMode ? `http://${this.config.host}:4000/admin`:`http://${this.config.host}:1337/admin`
Thanks
Here is what worked for me:
Just upgrade your node js installation to version 10

Failed Database Connection between OpenShift Server and Mlab.com?

I'm hoping to host a node.js server at OpenShift, utilizing a MongoDB database hosted at mlab.com (the new version of mongolab.com) Here's a pretty straight forward tutorial. According to that tutorial, things seem pretty straight forward. That tutorial may be a bit dated, but seems to have been targeted directly for my application (less the update from Mongolab --> mlab) I've used Mongolab in the past and they provide a great service.
So I've built my database. I've written my node code and tested it from local host, where it works great. Yes there are a few lines of difference, but not much. I'm using the same git directory as I'm pushing to OpenShift. The code is pretty straight forward.
databaseUrl = 'mongodb://UserNameHere:PasswordHere#ds012345.mlab.com:12345/DataBaseName';
if (process.env.MLAB_URI) {
databaseUrl = process.env.MLAB_URI;
}
MongoClient.connect(databaseUrl, function(err, db) {
assert.equal(null, err, "Database Connection Troubles: " + err);
test process.env.MLAB_URI from my terminal after a RHC login.
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> echo $MLAB_URI
mongodb://<username>:<password>#ds012345.mlab.com:12345/DataBaseName
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> echo $OPENSHIFT_REPO_DIR
/var/lib/openshift/xxxxxxxxxxxx/app-root/runtime/repo/
Test with $, use in code with process.env. call. Obviously I've changed my username, password and Openshift server identification, but I've checked and there appear to be no spelling errors. I get the same fail on openshift if I don't use the MLAB_URI environment variable. It's like the connection from the OpenShift server is shut off.
Mlab does provide some tools to verify the connection to a MongoDB there. Here's link to the Mlab assist stuff. I can ping the mlab location from a RHC login and it works just fine. Unfortunately I'm unable to do the % netcat -w 3 -v ds012345.mlab.com 12345 test. That tool (netcat / nc) isn't available at OpenShift.
Again, this thing works fine when I run my node file.js from my local host. I can see data being deposited at the mlab server. It fails if I run from Openshift, with a
throw err ^
AssertionError: Database Connection Troubles: MongoError: auth failed
The code works fine if I use a MongoDB cartridge in the same gear at OpenShift. Unfortunately I've got a few different servers at different locations that are all sharing information. Anybody know what's going on here?
Update: I've done some additional testing from a terminal with RHC login to OpenShift.
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> mongo ds012345.mlab.com:12345/dbName -u <dbuser> -p <password>;
MongoDB shell version: 2.4.9
connecting to: 127.6.xyz.xyz:27017/admin
Fri Mar 11 04:14:52.770 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228
exception: login failed
The one surprise is that connecting to: url:27017/admin line... I'd like to understand that better. Stay tuned.
Update for anybody else who may get here. I submitted a support request to mlab. I received an immediate response (Awesome support!)
You'll need to upgrade your mongo shell version to 3.0+ in order to
connect and authenticate to an mLab Sandbox database server. It looks
like version 2.4.9 is being used.
So I was definitely using mongo shell version 3.0 from my localhost. I have little control # OpenShift for that command line feature.. But whoa... Let's not forget the big picture here. I'm really trying to use my node server to contact mlab via a var MongoClient = require('mongodb').MongoClient; connect call. Let's make the same check. Do I have the latest version of mongodb listed in my package.json file? Oops..
Easy fix. Update package.json to require a newer version of mongodb. Success at OpenShift. Yipee!

Local Node.js script not connecting to mongoDB database

I setup a MongoLab MongoDB database through Heroku and am able to connect to it in the shell by issuing the below command:
mongo ds061701.mongolab.com:61701/heroku_app35721468 -u <dbuser> -p <dbpassword>
I'm using Node.js and Express and wrote the following code in a script.js file, which sits in the same directory as a node_modules folder containing mongoose.
var mongoose = require('mongoose');
mongoose.connect('mongodb://<dbuser>:<dbpassword>#ds061701.mongolab.com:61701/heroku_app35721468');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error: '));
db.once('open', function(){
console.log('db connected');
});
When navigate to the directory of this script in Terminal and type node script.js, I get the following error message:
dyld: lazy symbol binding failed: Symbol not found: _node_module_register
Referenced from: /Users/Jack/Documents/node-express-101/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/build/Release/bson.node
Expected in: dynamic lookup
dyld: Symbol not found: _node_module_register
Referenced from: /Users/Jack/Documents/node-express-101/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/build/Release/bson.node
Expected in: dynamic lookup
Trace/BPT trap: 5
What am I doing wrong here and how can I connect to my MongoDB database hosted on MongoLab with mongoose?
Referring to comments from OP's original question: the solution was the original dependency install did not complete successfully. Reinstalling solved it.
That error will be thrown when you have installed modules (that require compilation) with Node 0.12, but subsequently try to run those modules with an older Node version (like 0.10).

Heroku + mongoose connection error: no primary server found in set

I have a mongodb replica set on mongolab.
I'm using nodejs + mongoose. When I'm trying to connect from my local machine everything goes ok.
But after deployment to heroku something wrong happens and mongoose got strange error:
[Error: no primary server found in set]
Here some code (server.js):
async.series([
function(callback){
console.log('DB Connection: ' + siteConf.mongo_url);
mongoose.connect(siteConf.mongo_url, siteConf.mongo_options, callback);
},
function(callback){
http.createServer(app).listen(siteConf.port, callback);
}
],
function(err, results){
if (err) {
console.log(err);
}
console.log('Running in ' + (process.env.NODE_ENV || 'development') + ' mode # ' + siteConf.uri);
}
);
This url I'm using as connection string:
mongodb://username:password#someid-a0.mongolab.com:39897/pm_prod,mongodb://someid-a1.mongolab.com:39897
The main thing I can't understand is: what is the differences between my maching and heroku cloud hosting.
I already tried to remove node_modules and npm install them to be sure that I have same versions as on heroku. (Because heroku do this on each deploy).
Thanks, and sorry for my bad english
This may be a URI problem. The format for DB URIs is:
mongodb://<user>:<pass>#host:port,host:port,...,host:port/db_name
That means that
mongodb://username:password#someid-a0.mongolab.com:39897/pm_prod,mongodb://someid-a1.mongolab.com:39897
should be:
mongodb://username:password#someid-a0.mongolab.com:39897,someid-a1.mongolab.com:39897/pm_prod
It's also worth noting for others: If you are using Heroku's MongoLab add-on, your URI is available as an environment variable at process.env.MONGOLAB_URI, so you don't have to place the URI in your code.
This could also be related to a running question about connectivity between Heroku and Mongo. See https://github.com/jcottr/temps-mort, which references two tickets for the Node Mongo driver:
https://jira.mongodb.org/browse/NODE-4
https://jira.mongodb.org/browse/NODE-5

Resources