DENO mongo atlas connection issue when primary cluster is changing - mongodb

As written in deno.land/x/mongo, to connect to mongo Atlas, such configuration should be use :
import { MongoClient } from "https://deno.land/x/mongo#v0.21.0/mod.ts";
const client1 = new MongoClient();
await client.connect({
db: "<db>",
tls: true,
servers: [
{
host: "<host>",
port: 27017,
},
],
credential: {
username: "<user>",
password: "<password>",
db: "<db>",
mechanism: "SCRAM-SHA-1",
},
});
const db = client1.database("TestingDB");
export default db;
There, <host>should be replaced by a specific cluster address (looks something like that : cluster0.hmdnu.mongodb.net) and not a full string as mongodb+srv://user1:MYPASSWORD#cluster0.hmdnu.mongodb.net/TestingDB?retryWrites=true&w=majority.
This solution works well. But there is, I believe, a downside : Sometimes, a Mongo cluster can fail and a Secondary cluster can become Primary. Has we are using a specific cluster on our code, that cluster might be down and our program as well. Any one encounter a similar issue ? and how did you resolve it ?

Add more cluster
servers: [
{
host: "<host>", /*primary*/
port: 27017,
},
{
host: "<host>", /*secondary*/
port: 27017,
},
{
host: "<host>", /*secondary*/
port: 27017,
},
],

Related

Meteor MUP - Mongo version 4 incompatible

I was very happy to learn that the new Meteor versions support Mongo v4.
It seems that MUP does not...
If I run 'mup setup' with these mongo settings in mup.js everything goes fine.
module.exports = {
servers: {
one: {
host: '****',
}
},
meteor: {
name: 'nascholingen',
path: '/webserver/nascholingen',
volumes:{
'/webserver/nascholingen/private/.uploads':'/webserver/nascholingen/private/.uploads',
'/webserver/nascholingen/private/.downloads':'/webserver/nascholingen/private/.downloads',
'/webserver/nascholingen/backups/nascholingen':'/webserver/nascholingen/backups/nascholingen'
},
servers: {
one: {
env:{
PDFMERGE_PATH: "/usr/bin/pdfmerge",
}
},
},
buildOptions: {
serverOnly: true,
},
env: {
ROOT_URL: '*****',
MONGO_URL: 'mongodb://localhost/meteor',
PORT:'3002'
},
docker: {
image: 'appworkshop/meteord-graphicsmagick-pdftk-node8:node-8.9.1-base',
},
deployCheckWaitTime: 60,
enableUploadProgressBar: false
},
mongo: {
port: 27017,
version: "3.6.16",
servers: {
one: {}
}
}
};
When I change 'version' to 4.0.6 or anything higher, mup setup shows:
Connection refused :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
2019-12-21T18:07:28.576+0000 F - [main] exception: connect failed
2019-12-21T18:07:28.576+0000 E - [main] exiting with code 1
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2019-12-21T18:07:33.737+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: ca
Any ideas?

Shared MongoDb collection between two meteor apps deployed in amazon AWS not working.

I have deployed a meteor app to port 80 of my AWS instance using MUP. I deployed a second meteor app to port 3000, but this time omitting mongo setup and specifying mongo url in the mup.js file. The setup works fine and the second app is deployed but none of my publications seems to work. I have tried the same setup with two test Apps previously and it worked.
MUP.JS of App 1
module.exports = {
servers: {
one: {
host: 'IP',
username: 'ubuntu',
pem: 'path to my pem file'
}
},
meteor: {
name: 'Dashboard',
path: 'Path to my project',
servers: {
one: {}
},
buildOptions: {
serverOnly: true,
},
docker: {
image: 'abernix/meteord:base',
},
env: {
PORT: 80,
ROOT_URL: 'base url/',
MONGO_URL: 'mongodb://mongodb:27017/dbname'
},
deployCheckWaitTime: 320,
enableUploadProgressBar: true
},
mongo: {
oplog: true,
port: 27017,
servers: {
one: {},
},
},
};
MUP.JS of App 2
module.exports = {
servers: {
one: {
host: 'IP',
username: 'ubuntu',
pem: 'path to my pem file'
}
},
meteor: {
name: 'DashBoard2',
path: 'Path to my project',
servers: {
one: {}
},
buildOptions: {
serverOnly: true,
},
docker: {
image: 'abernix/meteord:base',
},
env: {
PORT: 3000,
ROOT_URL: 'base url/',
MONGO_URL: 'mongodb://mongodb:27017/dbname'
},
deployCheckWaitTime: 320,
enableUploadProgressBar: true
},
};
Are you certain the connection to the database has been made? Typically the Meteor console will give an error if it can't connect to the MongoDB database. Also not sure if you have omitted your connection string, but it should be;
mongodb://user:password#server:port/database

mongodb mongoose connection open callback doesnt get called

I have a MEAN project and this is a snippet from my server.js
var db = require('./config/db');
// url : 'mongodb://localhost/cdnserver'
// results are same for 127.0.0.1 or the machines ip
console.log(mongoose.connect(db.url));
mongoose.set('debug', true);
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + db.url);
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
This is a setup that has been working well for over 3 months now. Now I am replicating my whole MEAN stack along with database to other machine. I took a mongodump and did mongorestore. The restored db setup looks fine through mongo from terminal.
However, when starting the server, connection-open callback is not getting called. disconnected and error callbacks are getting called if I stop the mongodb service. How do I debug this further?
I am attaching the console output from both the setups.
Setup 1 :
Mongoose {
connections:
[ NativeConnection {
base: [Circular],
collections: {},
models: {},
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'cdnserver',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] } ],
plugins: [],
models: {},
modelSchemas: {},
options: { pluralization: true } }
Server up on 80
Mongoose default connection open to mongodb://localhost/cdnserver
1
Mongoose: videos.find({}) { skip: 0, limit: 5, fields: undefined }
Setup 2:
Mongoose {
connections:
[ NativeConnection {
base: [Circular],
collections: {},
models: {},
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'cdnserver',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] } ],
plugins: [],
models: {},
modelSchemas: {},
options: { pluralization: true } }
Server up on 80
1
1
Mongoose default connection disconnected
Mongoose default connection error: Error: connection closed
cat /var/log/mongodb/mongodb.log shows exactly the same output in both the machines.
Update 1: The setup started working properly out of blue and it stopped again. I am not able to figure out what is making this happen.
I figured it out finally, the new setup was using a newer version of nodejs.
When I moved to 6.x from 7.x it worked fine. I guess the mongoose, node 7, mongodb versions didnt go well together.

Using different mongo database than admin in sails

I'm struggling to connect with sails to a mongodb database that uses a database for authentication named "dbadmin". Where I am DBA decided to have all users centralized in a users database.
I can connect to this "dbadmin" database but then sails complains it cannot create collections there.
How can I use an authentication database and then a different database for collections?
You can define two database connection in your connections config file and then you can pass the connection in your data model directly
see the example:
in your config file
// server/config/connections.js
dbadmin: {
adapter: 'sails-mongo',
host: 'dbadmin-host',
port: 27017,
user: 'username',
password: 'password',
database: 'dbadmin'
}
dbuser: {
adapter: 'sails-mongo',
host: 'dbuser-host',
port: 27017,
user: 'username',
password: 'password',
database: 'dbuser'
}
and now you can use in your model:
// server/api/models/AdminUser.js
module.exports = {
connection:'dbadmin',
attributes: {
name : { type: 'string' },
pass : { type: 'string' }
}
};
// server/api/models/Users.js
module.exports = {
connection:'dbuser',
attributes: {
name : { type: 'string' },
pass : { type: 'string' }
}
};
NOTE: you can not make any collection between this two model .
It turns out that using the URL for the connection has more possibilities. So all I had to do is append at the end authSource=dbadmin
module.exports = {
models: {
connection: 'mongoRM'
},
connections: {
mongoRM: {
adapter: 'sails-mongo',
url: 'mongodb://user:password#db_url:port/database?authSource=dbadmin'
}
}
}

Configure adapter in Model

I use sails 0.9.
Here is adapters.js code:
module.exports.adapters = {
mongo: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'tracker'
},
mongo2: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
}
};
And model:
module.exports = {
adapter: 'mongo2',
config: {
database: 'offer'
},
attributes: {
name: {
type: 'string',
unique: true,
required: true
}
}
};
From docs http://sailsjs.org/#!documentation/models i get that this model will be saved in database named "offer", but it use db named "sails". Looks like its just ignore config section of model.
What is my mistake?
You should put the database in adapter config itself
mongo2: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'offer'
}