Authentication Failed Mongoose - node.js

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");

Related

Can't connect to a MongoDB server

I'm trying to deploy this Fullstack app to a DigitalOcean droplet:
https://github.com/maismin/stock-portfolio-app-demo
I installed MongoDB like how to documentation said and then I started it and didn't do anything else (I don't know if I need to actually make a database or not this is the first time I'm using MongoDB). Then I cloned the repo, then I installed NPM with npm install, then I just copied this in to the terminal:
PORT=3000
MONGODB_URI='LINK TO YOUR MONGDB SERVER'
MONGODB_URI_DEV='LINK TO YOUR LOCAL MONGODB SERVER'
MONGODB_URI_TEST='LINK TO LOCAL MONGODB SERVER'
IEX_URI=https://cloud.iexapis.com/stable
IEX_KEY='YOUR IEX KEY'
JWT_SECRET='YOUR SECRET'
and replace this MONGODB_URI='LINK TO YOUR MONGDB SERVER' with
MONGODB_URI='xxx.xxx.xxxx' (my servers IP, this is where I really have no idea what to do and am just trying things). I also did the MONGODB_URI_DEV and MONGODB_URI_TEST with the same. The IEX and JWT variables are fine and I knew what to do there.
I got this error when I npm run dev:
See https://webpack.js.org/plugins/environment-plugin for example.
Error connection to MongoDB: The 'uri' parameter to 'openUri()' must be a string, got "undefined". Make sure the first parameter to 'mongoose.connect()' or 'mongoose.createConnection()' is a string.
[nodemon] app crashed - waiting for file changes before starting...
This error is due to invalid mongodb URL, ensure that it is correct and the .env or the file where the URL is stored is imported correctly..
it might also be the issue with the .env, so add the enviroment variables directly through digital Ocean: https://thecloudhub.com/tag/digitalocean-droplet-environment-variables/
Did you add the .env file?
PORT=3000
MONGODB_URI='LINK TO YOUR MONGDB SERVER'
MONGODB_URI_DEV='LINK TO YOUR LOCAL MONGODB SERVER'
MONGODB_URI_TEST='LINK TO LOCAL MONGODB SERVER'
IEX_URI=https://cloud.iexapis.com/stable
IEX_KEY='YOUR IEX KEY'
JWT_SECRET='YOUR SECRET'

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

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!

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