Failed Database Connection between OpenShift Server and Mlab.com? - node.js

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!

Related

throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string'

I try to connect my database to express, but I've this message.. on PostgreSQL
throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string'
if somebody had fix the same problem ;)
when you installed Postgresql did you set the password for Postgres user? Because I had this same issue and I solved it by doing this silly but apparently important step when you installed Postgres.
run the shell and then use the command \password.
Then should put this same password in the code you use to connect the database with express.
I had this same error message come up when I tried to connect my website running locally to my database running locally.
I was able to connect my local website to my live database. And my live website worked properly with my live database too.
But I couldn't connect my local website (or Postman) to my local database.
However, I just received help from a friend to fix this. I added this line to the top of my pool.js file:
import dotenv from 'dotenv/config';
The problem was that it wasn't accessing the environment variables from the pool.js file. Although, my friend said a better solution would be to add the 'dotenv/config' to a script in the package.json file, but he wasn't sure what the proper syntax was for that, so I'm going to try to find that out next.
It could be password value undefined/null. If you put the password inside .env file, make sure to config it. In my program when the password value was undefined, it logged this error.
The syntax is
nodemon -r dotenv/config src/server.js

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

Authentication Failed Mongoose

I'm trying to enable auth into my mongo server. I've already done:
-Configured security into mongo.conf
-Created the users(if use it with mongo -u -p..... from console works fine)
In the node server js my config is this
mongoose.connect(mongodb://user:pass#localhost:27017/dbName);
Also I've already tried with
mongoose.connect(mongodb://user:pass#localhost:27017/dbName,{auth:{authdb:"admin"}});
And this
mongoose.connect(mongodb://user:pass#localhost:27017/dbName,{auth:{authdb:"dbName"}});
And always get this error:
Authentication failed
What would be doing wrong?
Try specifying the authDB like this -
mongoose.connect("mongodb://user:pass#localhost:27017/dbName?authSource=admin");

Parse server error "Protocol not supported"

I've been migrating datas for two days now, everything is ok in AWS - I used a Bitnami MEAN machine, it was only a very small app.
FYI, I'm moving from Heroku + Parse, set up also nginx on AWS to run more than one nodejs app.
I had to downgrade the default mongodb installation due to incompatibility with Parse (WHY?)
So, straight to the problem: installed node.js parse server, configured like they show on git
var api = new ParseServer({
databaseURI: 'mongodb://127.0.0.1:27017/database',
cloud: './cloud/main.js',
appId: 'my-app-id',
masterKey: 'my-master-key'
});
but when I try to execute any query I got
Error: Protocol not supported.
at send (/opt/bitnami/apps/bellboy-admin/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:299:15)
at dispatch (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:137:11)
at Object.ajax (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:139:5)
at ParsePromise.<anonymous> (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:208:29)
at ParsePromise.wrappedResolvedCallback (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:135:41)
at /opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:196:35
at runLater (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:180:12)
at ParsePromise.then (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParsePromise.js:195:9)
at Object.request (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/RESTController.js:201:8)
at Object.find (/opt/bitnami/apps/bellboy-admin/node_modules/parse-server/node_modules/parse/lib/node/ParseQuery.js:1141:27)
I tried almost everything, any ideas from you?
Did you install the dependencies for ParseServer? More specifically, is the MondoDB NodeJS drive installed?
npm install mongodb
If it helps, I have a tutorial that explains how the ParseServer should be setup, providing you have MongoDB and NodeJS already installed to the correct versions.
Solved
I guessed it was something involving the http/https protocols between my node app and parse server so I just added the http:// before the address of Parse.serverURL
Parse.initialize('my-id','unused');
Parse.serverURL = 'http://localhost:3030/parse';
Maybe it goes by default on https when not specified.

How to seed data for a node+mongo app deployed to Heroku?

I have a node express app with MongoDB as database. I want to have a seed.js file which I can run to fill in initial data to the database. I have no problem doing this on my local maschine. I just write the mongo commands in my seed.js file and run it with:
$ mongo localhost:27017/myApp seed.js
However when I deploy my app to Heroku including MongoLab I am not sure how to seed the data there. Google leads me to rails stuff most of the time.
So is there a simple way of seeding data for Heroku MongoLab without writing a script?
Update:
If I try to run it with the MONGOLAB_URI from heroku I get an error.
$ mongo mongodb://heroku_xxxx:xxxxx.mongolab.com:xxx/heroku_xxxx seed.js
MongoDB shell version: 3.0.5
connecting to: mongodb://heroku_xxxxxxxx
2015-08-19T21:44:22.694+0100 E QUERY Error: More than one ':' detected. If this is an ipv6 address, it needs to be surrounded by '[' and ']'; heroku_xxxxxxxxxxxxx
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Here the complete answer how to run a seed.js file with mongo commands for an app deployed to Heroku, thanks to pneumee and hunterloftis:
Get your MONGOLAB_URI by running $ heroku config inside your app directory
The MONGOLAB_URI variable has the format mongodb://user:pass#host:port/db.
Using those different parts stick together the correct terminal command to run the seed.js file:
$ mongo host:port/db -u user -p pass yourSeedFile.js
If you run heroku config -a yourapp you'll see the connection string for your mongolab instance. You can then connect to that database via the same mongo client you use locally, running the same seed.js.

Resources