double colon in host identifier when connecting to mongodb - node.js

I have 2 nodejs application on Linux CentOs7 server. The first is running on the main domain and the second on a subdomain. Both have to connect to the same MongoDb replicaset but on different databases. They have different username and password in the connection string.
The application on the main domain is connecting without problems, but the subdomain gets the error: double colon in host identifier.
This is the config file for the MongoDb on the subdomain:
module.exports = {
'secret': 'mysecret',
'database': 'mongodb://myUID:myPass#127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/mySubDomainApp,replset: { rs_name: "rs0" }',
'hashidsecret': 'theSecret',
'cryptrsecret': 'thecryptosecret'
};

I found the solution in the config file:
module.exports = {
'secret': 'jK5skCC5spUWqrs7p',
'database': 'mongodb://fIujhYes:24KWWisPjfB52#127.0.0.1:27017/challenger?replicaSet=rs0',
'hashidsecret': 'MCZ4584jHMQsfC',
'cryptrsecret': 'wYrdS8KV51Rsvd',
'presetRoles': ['systemadmin', 'admin']
};
Changing ,replset: { rs_name: "rs0" } to ?replicaSet=rs0 did the trick.
But very strange that the first config file works on the main domain on the same server.

Related

Creating sub connections with azure-mobile-apps and NodeJS

I'm trying to create an API using nodeJS, express and azure-mobile-apps to do some data synchronisation between an Ionic3 mobile app (which use an SQLite local database) and a Microsoft SQL Database.
My API has to create a synchronisation connection for each mobile application. Each application will be linked to a distant database. For example, if user_01 wants to synchronise his data, he's going to be linked to his client_01 database. So each time it'll have to, the API will create a new process running on a different port.
here is an example : https://zupimages.net/up/19/36/szhu.png
The problem is that i'm not able to create more than one connection with azure-mobile-apps. The first one always works, but the second, third etc are still using the first connection that i have instantiated. I've looked into the app stack and everything seems fine.
Is that an issue with azure-mobile-app, or did I misunderstand something with express ?
Thanks for your responses !
var azureMobileApps = require('azure-mobile-apps');
var express = require('express');
module.exports = {
async createConnection(client) {
try {
let app = express();
mobileApp = azureMobileApps({
homePage: true,
swagger: true,
data: {
server: '****',
user: client.User,
password: client.Password,
port: '1443',
database: client.Database,
provider: 'mssql',
dynamicSchema: false,
options: {
encrypt: false
}
}
});
await mobileApp.tables.import('./tables');
await mobileApp.tables.initialize();
app.listen(global.portCounter);
app.use(mobileApp);
console.log(app._router.stack);
console.log('Listening on port ',global.portCounter);
global.portCounter++;
} catch (error) {
console.log(error);
}
}
}
It's working now. The thing is, it's impossible to do multiple connection with the azure-mobile-apps SDK for nodeJS.
I had to use worker-thread which seems to isolate the memory in a sub-proccess.
Hope it can help somebody one day

Configuring Couchbase Server and Couchbase Sync Gateway

I am successfully connecting my couchbase server with my application using localhost:3000
var express = require('express');
var bodyParser = require("body-parser");
var couchbase =require("couchbase");
var request = require("request");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
var cluster = new couchbase.Cluster('couchbase://localhost');
cluster.authenticate('Administrator', 'ABcd1234');
var bucket = cluster.openBucket('non-med'); //the name of bucket is 'example'
bucket.on('error', function(err) { console.log('Bucket: CONNECT ERROR:', err);});
module.exports.bucket = bucket;
var routes = require("./routes.js")(app);
var server = app.listen(3000, function(){
console.log("Listening on port%s...", server.address().port);
});
I have also downloaded Couchbase Sync Gateway from on my mac in commend line and service is loaded shown as follows
sudo ./sync_gateway_service_install.sh
chown: sync_gateway: illegal user name
chown: sync_gateway: illegal user name
/Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist: service already loaded
Could everyone tell you how can i configure the Conuchbase Sync Gateway in detail??
Any amendment on the app.js
Is there any new file needed to create e.g. sync-gateway-config.json?
If so,
(2a)Where do i include this file? My application project folder?
(2b)May i know the format of json?
Thanks
Sync gateway needs to run as a separate file.You can set set syncing criteria by creating channels in http://localhost:4985/_admin/. You can refer https://developer.couchbase.com/documentation/mobile/current/installation/sync-gateway/index.html to run the sync-gateway. I'll attach a sample sync-gateway configuration file below. Cheers!!
{
"log": [
"HTTP+"
],
"adminInterface": "localhost:4985", //Public port
"interface": "localhost:4984", //Admin port
"databases": {
"your_cluster_name": { //add your couchbase cluster name
"server": "http://localhost:8091", //add couchbase server url
"username": "your_username",
"password": "your_password",
"bucket": "your_bucket_name",
"users": {
"GUEST": {
"disabled": true
},
"admin": {
"admin_channels": ["*"], //give permission to all the channels
"password": "123456" //admin channel password
}
},
"import_docs": "continuous",
"enable_shared_bucket_access": true,
"sync":`
function(doc) {
channel(filter); // set your filtering criteria
}`
}
}
}
Sync Gateway talks directly to Couchbase Server. (In production you will usually not run them on the same machine. Make sure firewall/network filtering doesn't block access.)
Sync Gateway listens for connections from your client (mobile) applications. It does not interact with your Node app in most typical scenarios.
You configure Sync Gateway by supplying a file with your parameters. The name of the file is not important. The format and parameters are in the documentation here: https://developer.couchbase.com/documentation/mobile/current/guides/sync-gateway/config-properties/index.html
There are also sample configuration files included in the Sync Gateway distribution.
You do not need to create channels via the admin interface. You typically do this in the configuration file or via the sync function. It is very important to understand the sync function and what it does. See the documentation here: https://developer.couchbase.com/documentation/mobile/2.0/guides/sync-gateway/sync-function-api-guide/index.html

Elastic search undefined?

I used mongo-connector on my linux server
pip install mongo-connector
python mongo_connector.py -m localhost:27217 -t http://localhost:9200
Inside my local node.js server I am using mongoDB which is hosted on my linux server
var Movie = require('../models/Movie');
var mongoosastic = require("mongoosastic");
var elasticsearch = require('elasticsearch');
movie = new Movie({title: 'Warrior'});
movie.save(function() {
console.log(arguments)
Movie.search({query:"Warrior"}, function(err, results) {
console.log(results);
});
});
Schema
var mongoose = require('mongoose');
var elasticsearch = require('elasticsearch');
var mongoosastic = require("mongoosastic");
var movieSchema = new mongoose.Schema({
language: String,
year: String,
title: { type:String, es_indexed:true },
director: String
});
movieSchema.plugin(mongoosastic);
module.exports = mongoose.model('Movie', movieSchema);
I keep getting undefined. I think the data is indexed, but I can't search for it.
Testing
curl -XPUT 'http://localhost:9200/blog/user/dilbert' -d '{ "name" : "Dilbert Brown" }'
curl -XGET 'http://localhost:9200/blog/user/dilbert?pretty=true'
{
"_index" : "blog",
"_type" : "user",
"_id" : "dilbert",
"_version" : 1,
"found" : true,
"_source":{ "name" : "Dilbert Brown" }
}
The problem could be that node.js is hosted on my local machine and the mongoDB/elasticsearch is on my linux server.
By pointing at the fact that your node.js and ES are on two different hosts, you've probably put the finger on the issue.
Looking, at your command line :
python mongo_connector.py -m localhost:27217 -t http://localhost:9200
-m points to your Mongo DB on the localhost port 27217 (unless that port is the entry to an SSH tunnel to your remote linux server)
-t points to your replication endpoint (i.e. Elasticsearch on port 9200 of your local machine)
According to your statement that Elasticsearch and MongoDB are hosted on your remote linux server, you should replace localhost with the host name or IP address of your Linux server and make sure the ports 27217 and 9200 are open on that host.
Then you should also make sure that your Node.js and mongoosastic points to your linux server, because by default it uses localhost:9200, i.e. your local machine. So use this instead and point to your linux server:
movieSchema.plugin(mongoosastic, {
hosts: [
'your_linux_host:9200'
]
});
And also that your mongoose connection points to your linux server as well.
mongoose.connect('mongodb://your_linux_host/your_database');

Mongoose not connecting on Ubuntu Ubuntu 14.04

I've got a node app built on Hapi using MongoDB and mongoose. Locally, I can use the app without issue. I can connect to the db, add data, find it, etc.
I've created an Ubuntu 14.04 x64 droplet on Digital Ocean.
I can ssh into my droplet and verify that my db is there with the correct name. I'm using dokku-alt to deploy and I have linked the db name to the app using dokku's mongodb:link appName mydb
I was having issues once I deployed the app where it would hang and eventually timeout. After a lot of debugging and commenting out code I found that any time I try to hit mongo like this the app will hang:
var User = request.server.plugins.db.User;
User
.findOne({ id: request.auth.credentials.profile.raw.id })
.exec(function(err, user){
// do something
});
Without this, the app loads fine, albeit without data. So my thought is that mongoose is never properly connecting.
I'm using grunt-shell-spawn to run a script which checks if mongo is already running, if not it starts it up. I'm not 100% certain that this is needed on the droplet, but I was having issues locally where mongo was already running... script:
/startMongoIfNotRunning.sh
if pgrep mongod; then
echo running;
else
mongod --quiet --dbpath db/;
fi
exit 0;
/Gruntfile.js
shell: {
make_dir: {
command: 'mkdir -p db'
},
mongodb: {
command: './startMongoIfNotRunning.sh',
options: {
stdin: false,
}
}
},
And here's how I'm defining the database location:
/index.js
server.register([
{ register: require('./app/db'), options: { url: process.env.MONGODB_URL || 'mongodb://localhost:27017/mydb' } },
....
/app/db/index.js
var mongoose = require('mongoose');
var _ = require('lodash-node');
var models = require('require-all')(__dirname + '/models');
exports.register = function(plugin, options, next) {
mongoose.connect(options.url, function() {
next();
});
var db = mongoose.connection;
plugin.expose('connection', db);
_.forIn(models, function(value, key) {
plugin.expose(key, value);
});
};
exports.register.attributes = {
name: 'db'
};
My app is looking for db files in db/. Could it be that dokku's mongodb:link appName mydb linked it to the wrong location? Perhaps process.env.MONGODB_URL is not being set correctly? I really don't know where to go from here.
It turns out the solution to my problem was adding an entry to the hosts file of my droplet to point to the mongo db url:
127.0.0.1 mongodb.myurl.com
For some reason, linking the db to my app with Dokku didn't add this bit. I would have thought that it was automatic. The app container's host file did get a mongodb entry when i linked the db to the app.

Replica Set not working as expected

I have configured like below and my MongoDB don't need username or password:
mongo: {
module: 'sails-mongo',
url: "mongodb://127.0.0.1:27017/mydb",
replSet: {
servers: [
{
host: "127.0.0.1",
port : 27018
},
{
host: "127.0.0.1",
port : 27019
}
],
options: {connectWithNoPrimary:true, rs_name:"rs0"}
}
}
It's working fine, meaning I do not get a connection error and I am able to do querying. But when I brought down 127.0.0.1:27017, 127.0.0.1:27018 becomes PRIMARY as if I did a rs.status(). After this, I am no longer able to do any query and keep getting the following:
Error: no open connections
I am sure that I setup replica-set in my local machine correctly as I used MongoDB native driver to test the above mentioned scenario (bring down PRIMARY and SECONDARY take over as PRIMARY) and there is no problem.
var url = 'mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/mydb?w=0&wtimeoutMS=5000&replicaSet=sg1&readPreference=secondary';
mongodb.MongoClient.connect(url, function(err, result) {
if(err || result === undefined || result === null) {
throw err;
} else {
db = result;
}
});
ok I found the answer. This message emitted because of session.js. I commented everything in the file and now it is working. The reason I guess is in session.js, it only pointing to a single host, which is the original PRIMARY. when you bring down this mongodb PRIMARY, session.js no longer can connect so it threw exception. I also tried the mongodb URL string in sessions.js by putting in also the hosts ip in the replica set (mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/mydb) but failed to "sails lift". When put only single host then it is fine.
now if I need to store sessions info, I need to start another mongodb instance then session.js point to this new instant.

Resources