Sails application fails to connect localhost mongo db - mongodb

My sails application not able to connect to locally running mongo db instance
local.js is like below
module.exports.connections = {
sailsMongoDBServer: {
adapter: 'sails-mongo',
host: '127.0.0.1',
port: 27017,
database: 'dp-manager-db'
}
}
Error :
2019-03-06T12:32:01.081Z - error:
{ message:
'The hook `orm` is taking too long to load.\nMake sure it is triggering its `initialize()` callback, or else set `sails.config.orm._hookTimeout to a higher value (currently 20000)',
stack:
'Error: The hook `orm` is taking too long to load.\nMake sure it is triggering its `initialize()` callback, or else set `sails.config.orm._hookTimeout to a higher value (currently 20000)\n at Timeout.tooLong [as _onTimeout] (E:\\workspace_DpForm\\Eclipse\\dpm-app\\node_modules\\sails\\lib\\app\\private\\loadHooks.js:85:21)\n at ontimeout (timers.js:436:11)\n at tryOnTimeout (timers.js:300:5)\n at listOnTimeout (timers.js:263:5)\n at Timer.processTimers (timers.js:223:10)',
code: 'E_HOOK_TIMEOUT' }
2019-03-06T12:32:03.521Z - error: A hook (`orm`) failed to load!
E:\workspace_DpForm\Eclipse\dpm-app\node_modules\mongodb\lib\mongo_client.js:224
throw err

Related

Goland mongodb "connection refused" error handling

Good day!
I have a service which is running in loop like this:
for {
select {
case <-ctx.Done():
return fmt.Errorf("context error: %w", ctx.Err())
default:
}
task, err := provider.Next(ctx)
if !errors.Is(err, ErrNotFound) {
return fmt.Errorf("next: %w", err)
}
// some work todo
}
Sometimes something happens with my mongodb and I get an error like
connection() : connection(mongorc:27017[-48]) incomplete read of message header: context deadline exceeded"
So I have to check
errors.Is(err, context.DeadlineExceeded).
as well as ErrNotFound.
But If I dont put timeout to mongo requests I get error like
server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
And I have no idea how to check it - I didnt find any "mongo.Err..." that is up to this error. Is there a way to check it in the code?

Problem connecting to Mongodb Atlas - Error: "querySrv ECONNREFUSED"

I have created a free cluster for mongodb atlas. Under that cluster I have created a database and a collection to test things out. As I was using node js driver 4.0 or above, this is the connection string I got:
mongodb+srv://myName:myPass#cluster0.2sjva.mongodb.net/myDb?retryWrites=true&w=majority
This is my backend code:
const { MongoClient } = require("mongodb");
// Connection URI
const uri =
"mongodb+srv://myName:myPass#cluster0.2sjva.mongodb.net/myDb?retryWrites=true&w=majority";
// Create a new MongoClient
const client = new MongoClient(uri);
async function run() {
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db("myDb").command({ ping: 1 });
console.log("Connected successfully to server");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
But when I run this code, I get this following error:
Error: querySrv ECONNREFUSED _mongodb._tcp.cluster0.2sjva.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:210:19) {
errno: undefined,
code: 'ECONNREFUSED',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.2sjva.mongodb.net'
}
Here is some information I can provide:
I have whitelisted my current Ip address, After this error I used 0.0.0.0. But the problem persists.
Os: Arch linux
I have also tried to use this connection string for older driver version: mongodb://... and that worked.
My question: why connection string for node js driver version > 4.0 doesn't work in my case?
Thank you for reading!

Authentication fail when connecting to Atlas MongoDB

I have an error when connecting to the MongoDB Atlas database after I migrated the data from the old MLAB.
I have definitely setup the username and password correctly as in the documentation (obviously i replaced PASSWORD with my correct MLAB password:
var mongoURI = 'mongodb+srv://heroku_3kcdl3j9:PASSWORD#cluster-3kcdl3j9.auof1.mongodb.net/heroku_3kcdl3j9?retryWrites=true&w=majority';
I have migrated my database from MLAB to Atlas successfully set the correct Network access settings to 0.0.0.0 IP address. Setup the environment variable in Heroku.
I connect to the atlas database with this code, do I need some special options? (this code works with the old MLAB connection)
mongoose.connect(mongoURI,
// { config: { autoIndex: true } },
// { options : { ssl: true } },
function (error) {
if (error) console.error(error);
else console.log('mongo connected');
const con = new mongoose.mongo.Admin(mongoose.connection.db)
con.buildInfo( (err, mongoURI) => {
if(err){
throw err
}
// see the db version
// console.log('mongo db.version(): '+ db.version);
})
});
However, I still get this error, I don't know what I am doing wrong:
{ MongoError: authentication fail
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/topologies/replset.js:1462:15
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:868:7
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:844:20
at finish (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:232:16)
at handleEnd (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:242:7)
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:351:15
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:531:18
at process._tickCallback (internal/process/next_tick.js:61:11)
name: 'MongoError',
message: 'authentication fail',
errors:
[ { name: 'cluster-3kcdl3j9-shard-00-01.auof1.mongodb.net:27017',
err: [MongoError] },
{ name: 'cluster-3kcdl3j9-shard-00-00.auof1.mongodb.net:27017',
err: [MongoError] },
{ name: 'cluster-3kcdl3j9-shard-00-02.auof1.mongodb.net:27017',
err: [MongoError] } ],
[Symbol(mongoErrorContextSymbol)]: {} }
(node:47015) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 's' of undefined
at Admin.buildInfo (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb/lib/admin.js:100:37)
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/index.js:95:13
at $initialConnection.then.err (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongoose/lib/connection.js:556:14)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:47015) 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:47015) [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.
(node:47015) UnhandledPromiseRejectionWarning: MongoError: authentication fail
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/topologies/replset.js:1462:15
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:868:7
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:844:20
at finish (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:232:16)
at handleEnd (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:242:7)
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:351:15
at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:531:18
at process._tickCallback (internal/process/next_tick.js:61:11)
(node:47015) 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: 2)
On the migration documentation from MLAB to ATLAS I can read:
Atlas servers always run with requireSSL and only accept TLS/SSL encrypted connections.
I can see that your ssl option is commented.
This is one problem.
The problem was it was something to do with the username and password. I believe there was an illegal character in the password. So I created another user with a better password and the server authenticated and logged in allowing it to connect.

Problem on virtualize hyperledger network on multiple hosts

I have followed and finished the tutorial
https://medium.com/#malliksarvepalli/hyperledger-fabric-1-2-on-multiple-hosts-using-docker-swarm-and-compose-11c13635e69e
and I have 3 hosts connected and all services are up. I had the correct result when I run the ./script.sh in the PC2 host.
Now I am following the next tutorial which is:
https://medium.com/#malliksarvepalli/hyperledger-explorer-with-fabric-1-2-running-on-multiple-hosts-89c5af691b7e
Can anyone please enlighten me in this tutorial? I have figured out that I should create a new host with ubuntu 16.04 and install the following prerequites:
nodejs 8.11.x
PostgreSQL 9.5 or greater
Jq
Am I right?
And in the exploreconfig.json I updated postgres variables:
host:192.168.1.136(4th's vm ip),
port:5432,
username:postgres,
password:psql,
database:fabric
Are they correct?
I have also modified Orderer, Org1 [peer0 & peer1), Org2(peer0 & peer1) IP address in config.json file with the ips of first three Vms where the network is up and running.
And follow the rest instructions and tests but when I run the command ./start.sh I have logs in console which are:
false 'ssl-certs' '/home/database/blockchain-explorer/ssl-certs'
postgres://christy:christy#192.168.1.136:5432/fabric
error when connecting to db: { Error: connect ECONNREFUSED 192.168.1.136:5432
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.136',
port: 5432 }
******* Initialization started for hyperledger fabric platform ******, {
'network-1':
{ version: '1.0',
clients: { 'client-1': [Object] },
channels: { mychannel: [Object] },
organizations: { Org1MSP: [Object], Org2MSP: [Object], OrdererMSP:
[Object] },
peers:
{ 'peer0.org1.ntua.com': [Object],
'peer1.org1.ntua.com': [Object],
'peer0.org2.ntua.com': [Object],
'peer1.org2.ntua.com': [Object] },
orderers: { 'orderer.ntua.com': [Object] } },
'network-2': {} }
client_configs.name undefined client_configs.profile undefined
FabricUtils.createFabricClient
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
Error : [ 'Invalid platform configuration, Please check the log' ]
error when connecting to db: TypeError: Cannot read property 'on' of
undefined
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:68:16)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
TypeError: "callback" argument must be a function
at setTimeout (timers.js:450:11)
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:85:5)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Received kill signal, shutting down gracefully
Closed out connections
false 'ssl-certs' '/home/database/blockchain-explorer/ssl-certs'
postgres://christy:christy#192.168.1.136:5432/fabric
error when connecting to db: { Error: connect ECONNREFUSED 192.168.1.136:5432
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.136',
port: 5432 }
******* Initialization started for hyperledger fabric platform ******,
{
'network-1':
{ version: '1.0',
clients: { 'client-1': [Object] },
channels: { mychannel: [Object] },
organizations: { Org1MSP: [Object], Org2MSP: [Object], OrdererMSP:
[Object] },
peers:
{ 'peer0.org1.ntua.com': [Object],
'peer1.org1.ntua.com': [Object],
'peer0.org2.ntua.com': [Object],
'peer1.org2.ntua.com': [Object] },
orderers: { 'orderer.ntua.com': [Object] } },
'network-2': {} }
client_configs.name undefined client_configs.profile undefined
FabricUtils.createFabricClient
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
Error : [ 'Invalid platform configuration, Please check the log' ]
error when connecting to db: TypeError: Cannot read property 'on' of
undefined
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:68:16)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
TypeError: "callback" argument must be a function
at setTimeout (timers.js:450:11)
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:85:5)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Received kill signal, shutting down gracefully
Received kill signal, shutting down gracefully
Closed out connections
If the other logs are also needed please let me know. Any help will be helpful. Thanks a lot.
I can give you some aspects to be checked on your environment.
First you need to do check if you are able to access postgresql DB(4th's vm) from the vm where you are trying to launch Hyperledger Explorer. If not, you need to change your postgres configuration (postgresql.conf/pg_hba.conf) for enabling external access, I think.
$ sudo -u postgres psql -h 192.168.1.136 -d fabric -c 'table peer'
Second, please confirm that you did run ./createdb.sh on 4th VM. If you changed database name, you also need to align the script
$ cd app/persistence/fabric/postgreSQL/db/
$ ./createdb.sh

Node and Mongoose - Not reconnecting if mongod was not running when first tried to connect

Im using docker-composer and Im finding issues with execution order of services. The main issue happens when my express app tries to connect to mongod but this is not yet ready.
The issue can be reproduced easily by running first the nodejs application but not mongod (manually forcing this case).
My app uses mongoose and try to establish connection to mongod. Because mongod is not up and running, the app throws an error about it.
$ nodemon server/app.js
24 Apr 21:42:05 - [nodemon] v1.7.0
24 Apr 21:42:05 - [nodemon] to restart at any time, enter `rs`
24 Apr 21:42:05 - [nodemon] watching: *.*
24 Apr 21:42:05 - [nodemon] starting `node server/app.js`
Listening on port 8000
disconnected
connection error: { [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
Starting mongod later seems to reconnect.
24 Apr 21:51:28 - [nodemon] v1.7.0
24 Apr 21:51:28 - [nodemon] to restart at any time, enter `rs`
24 Apr 21:51:28 - [nodemon] watching: *.*
24 Apr 21:51:28 - [nodemon] starting `node server/app.js`
Listening on port 8000
disconnected
connection error: { [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
connected
reconnected
Despite of that, operations that require access to mongo will not come through... neither error is shown
This is the code to connect to mongo using mongoose:
// Starting mongo
mongoose.connect(config.database, {
server:{
auto_reconnect:true,
reconnectTries: 10,
reconnectInterval: 5000,
}
});
// Listening for connection
var mongo = {};
var db = mongoose.connection;
db.on('connected', console.error.bind(console, 'connected'));
db.on('error', console.error.bind(console, 'connection error:'));
db.on('close', console.error.bind(console, 'connection close.'));
db.once('open', function() {
console.log("We are alive");
});
db.on('reconnected', function(){
console.error('reconnected');
});
db.on('disconnected', console.error.bind(console, 'disconnected'));
And here is the route that will try to get data from mongo but fail.
router.post('/auth', function(req, res){
User.findOne({name: req.body.name})
.then(function(user){
if(!user)
{
res.status(401).send({ success: false, message: 'Authentication failed. User not found.' });
}
...
How can I recover from running nodejs before mongo is ready?.
In my case, I created separate function only for mongoose connect method:
const connect = () => {
mongoose.connect('mongodb://localhost:27017/myapp', {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
poolSize: 10,
});
};
I'm calling it at the same start. I also added Event Handler for error event:
mongoose.connection.on('error', (e) => {
console.log('[MongoDB] Something went super wrong!', e);
setTimeout(() => {
connect();
}, 10000);
});
If mongoose fails to connect because MongoDB is not running, error event handler is fired and setTimeout schedules "custom" reconnect.
Hope it helps.
How long does it take before mongod is ready? Because it seems like this is an edge case issue, where mongod might take a couple of seconds to get ready; and when mongoose is connected it serves requests as expected. Just trying to understand why the slight delay (probably a only a few seconds) is necessary to resolve?
But here is a solution anyway:
You could set up an express middleware to check if mongoose is ready and throw an error if not:
app.use(function(req,res,next){
if (mongoose.Connection.STATES.connected === mongoose.connection.readyState){
next();
} else {
res.status(503).send({success:false, message: 'DB not ready' });
}
});
This should go before you inject your router.
I had the same issue with Mongoose 5+. I was able to get this working by creating a retry function using set timeout.
const mongoose = require('mongoose');
const {
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_HOSTNAME,
MONGO_PORT,
MONGO_DB,
MONGO_DEBUG,
MONGO_RECONNECT_TRIES,
MONGO_RECONNECT_INTERVAL,
MONGO_TIMEOUT_MS,
} = process.env;
if (MONGO_DEBUG) {
console.log(`********* MongoDB DEBUG MODE *********`);
mongoose.set('debug', true);
}
const DB_OPTIONS = {
useNewUrlParser: true,
reconnectTries: MONGO_RECONNECT_TRIES,
reconnectInterval: MONGO_RECONNECT_INTERVAL,
connectTimeoutMS: MONGO_TIMEOUT_MS,
};
const DB_URL = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}#${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;
// Initialize conenction retry counter
let reconnectTriesAlready = 1;
// Connect to database with timeout and retry
const connectWithRetry = () => {
mongoose.connect(DB_URL, DB_OPTIONS).then(() => {
// Connected successfully
console.log('********* MongoDB connected successfully *********');
// Reset retry counter
reconnectTriesAlready = 1;
}).catch(err => {
// Connection failed
console.error(`********* ERROR: MongoDB connection failed ${err} *********`)
// Compare retries made already to maximum retry count
if (reconnectTriesAlready <= DB_OPTIONS.reconnectTries) {
// Increment retry counter
reconnectTriesAlready = reconnectTriesAlready + 1;
// Reconnect retries made already has not exceeded maximum retry count
console.log(`********* MongoDB connection retry after ${MONGO_RECONNECT_INTERVAL / 1000} seconds *********`)
// Connection retry
setTimeout(connectWithRetry, MONGO_RECONNECT_INTERVAL)
} else {
// Reconnect retries made already has exceeded maximum retry count
console.error(`********* ERROR: MongoDB maximum connection retry attempts have been made already ${DB_OPTIONS.reconnectTries} stopping *********`)
}
})
}
connectWithRetry();