MLab with Heroku: Getting Authentication Failed despite confirming credentials - mongodb

I deployed a NodeJS app to Heroku and provisioned an instance of MongoDB with MLab. Given that the password for the default database user is unclear (it never asks to set one up), I created a new user with admin privileges called mknerr. I set the MONGOLAB_URI variable like so:
heroku config:set MONGOLAB_URI=mongodb://mknerr:{password}#ds159631.mlab.com:59631/heroku_ws1jw516?authSource=heroku_ws1jw516&w=1
I tried to test the connection locally using mongo ds159631.mlab.com:59631/heroku_ws1jw516 -u mknerr -p {password} but I received the error "-bash: mongo: command not found" despite having already run npm mongodb. Rather than waste time trying to figure out where my mongo installation is, I switched to an instance of Cloud9 I know has mongo installed and ran the same command as above.
It appeared to confirm my credentials were accurate:
MongoDB shell version v3.6.5
connecting to: mongodb://ds159631.mlab.com:59631/heroku_ws1jw5l6
MongoDB server version: 3.6.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
rs-ds159631:PRIMARY>
However when I try to load my application, it throws a 503 error. After running heroku logs --tail I see:
2018-07-30T15:04:41.171411+00:00 heroku[web.1]: State changed from up to starting
2018-07-30T15:04:42.187835+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-07-30T15:04:42.324261+00:00 heroku[web.1]: Process exited with status 143
2018-07-30T15:04:43.802715+00:00 heroku[web.1]: Starting process with command `npm start`
2018-07-30T15:04:40.930637+00:00 app[api]: Set MONGOLAB_URI config vars by user {me}
2018-07-30T15:04:46.562425+00:00 app[web.1]:
2018-07-30T15:04:46.562447+00:00 app[web.1]: > todos_api#1.0.0 start /app
2018-07-30T15:04:46.562449+00:00 app[web.1]: > node index.js
2018-07-30T15:04:46.562450+00:00 app[web.1]:
2018-07-30T15:04:47.302228+00:00 app[web.1]: App is running on Port 21448
2018-07-30T15:04:47.339759+00:00 app[web.1]: ERROR connecting to: mongodb://mknerr:{password}#ds159631.mlab.com:59631/heroku_ws1jw516?authsSource=heroku_ws1jw516. MongoError: Authentication failed.
2018-07-30T15:04:47.341632+00:00 app[web.1]: (node:20) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
2018-07-30T15:04:47.341635+00:00 app[web.1]: at /app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:598:61
2018-07-30T15:04:47.341637+00:00 app[web.1]: at authenticateStragglers (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:516:16)
2018-07-30T15:04:47.341638+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:552:5)
2018-07-30T15:04:47.341640+00:00 app[web.1]: at emitMessageHandler (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:309:10)
2018-07-30T15:04:47.341642+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:452:17)
2018-07-30T15:04:47.341644+00:00 app[web.1]: at emitOne (events.js:116:13)
2018-07-30T15:04:47.341646+00:00 app[web.1]: at Socket.emit (events.js:211:7)
2018-07-30T15:04:47.341647+00:00 app[web.1]: at addChunk (_stream_readable.js:263:12)
2018-07-30T15:04:47.341649+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:250:11)
2018-07-30T15:04:47.341650+00:00 app[web.1]: at Socket.Readable.push (_stream_readable.js:208:10)
2018-07-30T15:04:47.341652+00:00 app[web.1]: at TCP.onread (net.js:597:20)
2018-07-30T15:04:47.341703+00:00 app[web.1]: (node:20) 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)
2018-07-30T15:04:47.341773+00:00 app[web.1]: (node:20) [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.
Why is it failing to connect and throwing an authentication error despite my already having confirmed my credentials are accurate?

Related

mongo-express does not connect to mongodb

i have installed mongodb and i am able to connect via shell, now i am trying to install mongo-express, the installation was successful, i copied config.default.js file in config.js but it can't connect to mongo. As can be seen from the error it cannot retrieve the hostname
(node:3085) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(Use `node --trace-deprecation ...` to show where the warning was created)
Could not connect to database at index "0"
(node:3085) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [undefined:27017] on first connect [Error: getaddrinfo ENOTFOUND undefined
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) {
name: 'MongoNetworkError'
}]
at Pool.<anonymous> (/Users/name/.config/yarn/global/node_modules/mongodb/lib/core/topologies/server.js:438:11)
at Pool.emit (events.js:315:20)
at /Users/name/.config/yarn/global/node_modules/mongodb/lib/core/connection/pool.js:562:14
at /Users/name/.config/yarn/global/node_modules/mongodb/lib/core/connection/pool.js:995:11
at /Users/name/.config/yarn/global/node_modules/mongodb/lib/core/connection/connect.js:32:7
at callback (/Users/name/.config/yarn/global/node_modules/mongodb/lib/core/connection/connect.js:280:5)
at Socket.<anonymous> (/Users/name/.config/yarn/global/node_modules/mongodb/lib/core/connection/connect.js:310:7)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(node:3085) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3085) [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.
I had the same error.
Forcing the connection URL fixed it for me :
mongo-express --url "mongodb://localhost:27017" --admin
(--admin option allows to list all databases)

Can't connect to mongodb cloud

I'm working on a web application with node.js and using Mongodb for database. everything is cool when I run my app in localhost. but when I run it in a shared host it gives an error which looks like its Mongoose connection problem.
here is my code:
const uri = 'mongodb://****:****#iranroboticacademy-bdziw.mongodb.net/test';
mongoose.connect(uri,{ useNewUrlParser: true });
mongoose.connection.once('open', function(){
console.log('DataBase is connected. ');
}).on('error', function(error){
console.log('Connection error:', error);
});
and here is the erro that I get when I run the app in host:
Connection error: { MongoNetworkError: failed to connect to server [iranroboticacademy-shard-00-00-bdziw.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 3.226.212.92:27017]
at Pool.<anonymous> (/home/iranrob1/public_html/node_modules/mongodb/lib/core/topologies/server.js:433:11)
at Pool.emit (events.js:182:13)
at createConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:577:14)
at connect (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:1021:9)
at makeConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:31:7)
at callback (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:247:5)
at TLSSocket.err (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:276:7)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {} }
(node:6409) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [iranroboticacademy-shard-00-00-bdziw.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 3.226.212.92:27017]
at Pool.<anonymous> (/home/iranrob1/public_html/node_modules/mongodb/lib/core/topologies/server.js:433:11)
at Pool.emit (events.js:182:13)
at createConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:577:14)
at connect (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:1021:9)
at makeConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:31:7)
at callback (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:247:5)
at TLSSocket.err (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:276:7)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
(node:6409) 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:6409) [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.
Sure you can use the free tier of MongoDB Cloud.
It’s actually much easier to mantain than the hosted version as you have visually all under control, automatic backups etc.
https://mongodb.com
I have asked my host provider in a ticket and they said it doesn’t support mongodb. I have written everything with mongodb database in my application and I really can’t change it into other things. I need to use it in this host. Is there anyone who has a solution for this?!
try to use 'connect to your aplication', copy the then change the

How to connect to external Mongo (MLab) from a Node app in Docker

I have a database setup on Mlab and I have aconnection string that does work remotely. If I run my node app on docker using my local mongo, everything works as expected, however if I change the configuration and connect to my Mlab mongo, I get the following error:
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:66:8)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
(node:24) 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: 6)
Apart from handling the error on Node side (which I'm not catching, but that's another problem), how can I connect to an external mongo server form my Docker container?
I've seen this and follow the steps described there but it didn't help me.
Any suggestions?
EDIT
This is a Node app. In my Dockerfile I have the following setting:
ENV DB_HOST=mongodb+srv://<user>:<password>#<mlab>/<db_name>?retryWrites=true
And in my Node app where I have setup the database I have this:
mongoose.connect(process.env.DB_HOST, {useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true});
When I print the above env variable at run time, I get the correct string for the db (running locally everything works fine).
I don't understand why the error shows localhost. This is my first time using Docker. Do I need any additional configuration so my container can talk to an external service?

Play Framework PostgreSQL db.default.username defaulting to linux username

I keep getting errors like this...
app[web.1]: Configuration error: Configuration error[Cannot connect to database [default]]
app[web.1]: at scala.collection.immutable.List.foreach(List.scala:381)
app[web.1]: Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "lcnjgrwbmbffwv"
app[web.1]: at play.core.StaticApplication.<init>(ApplicationProvider.scala:55)
app[web.1]: at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:408)
app[web.1]: at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
app[web.1]: at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
app[web.1]: at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
app[web.1]: at org.postgresql.Driver.makeConnection(Driver.java:410)
app[web.1]: at java.sql.DriverManager.getConnection(DriverManager.java:664)
app[web.1]: at com.jolbox.bonecp.BoneCP.obtainRawInternalConnection(BoneCP.java:363)
app[web.1]: at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:240)
app[web.1]: at java.sql.DriverManager.getConnection(DriverManager.java:247)
app[web.1]: at org.postgresql.Driver.connect(Driver.java:280)
app[web.1]: at com.jolbox.bonecp.BoneCP.<init>(BoneCP.java:416)
app[web.1]: at com.jolbox.bonecp.BoneCPDataSource.getConnection(BoneCPDataSource.java:120)
app[web.1]: at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
app[web.1]: ... 18 more
heroku[web.1]: Process exited with status 255
Not just in Heroku, but when I run my Play app with a user whose name is different than the default user name in the database.
My Play Framework "application.conf" is like this...
db.default.username="nameOfDatabaseUser"
db.default.password="passOfDatabaseUser"
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://urlofdatabase:5432/name_of_database"
Here's the thing. When my terminal user is named "nameOfDatabaseUser", it works. But when my terminal user has a different name, it connects to the database as the name of the current Linux terminal user.
In the above case it appears that Heroku tried to connect as a user lcnjgrwbmbffwv and there was no user lcnjgrwbmbffwv in the database.
How do I specify the default database username so that it doesn't use my terminal username?
The correct key to specify the user is db.default.user, not db.default.username.

Mongo DB Remote Server Connection Issue in Meteor?

I need to know about the Mongo DB Remote Server Connection using in Meteor.As i did something shown below :
database = new MongoInternals.RemoteCollectionDriver("mongodb://x.x.x.x:3001/dbname");
I got the below error :
I20150121-11:51:14.066(5.5)? Exception in callback of async function: Error: failed to connect to [x.x.x.x:3001]
I20150121-11:51:14.066(5.5)? at null.<anonymous> (/home/abc/.meteor/packages/mongo/.1.0.8.hj5n7c++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/server.js:536:74)
I20150121-11:51:14.066(5.5)? at emit (events.js:106:17)
I20150121-11:51:14.066(5.5)? at null.<anonymous> (/home/abc/.meteor/packages/mongo/.1.0.8.hj5n7c++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
I20150121-11:51:14.066(5.5)? at emit (events.js:98:17)
I20150121-11:51:14.066(5.5)? at Socket.<anonymous> (/home/abc/.meteor/packages/mongo/.1.0.8.hj5n7c++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/connection.js:516:10)
I20150121-11:51:14.067(5.5)? at Socket.emit (events.js:95:17)
I20150121-11:51:14.067(5.5)? at net.js:440:14
I20150121-11:51:14.067(5.5)? at process._tickCallback (node.js:419:13)
How to solve the above error using Meteor.So please suggest me what to do for this.