AWS Document DB connection from Nodejs - node.js

I am working on a nodejs express app, which will finally be deployed to aws ec2 instance.
This nodejs app connects to aws document db and fetches some data. Now we know that to connect to aws doc db from the local machine (that is outside VPC) we need to use ssh tunnelling.
Now the question I have is, what is the good practice to use MongoClient in this situation, where locally we need to do ssh tunnelling but on production, we can skip this step. One can use env variables to do this, but I want to know what practise is widely accepted.

Related

MongoDb in Fastcomet shared hosting server

I haven't created an account in fastcomet yet, but I would like to ask the following questions. The site mentions that it supports node.js applications. So that means I can upload an express application right? How about MongoDB and mongoose? I can access MongoDB by typing in the terminal
$ mongod --dbpath=data -bind_ip 127.0.0.1
And the MongoDB URL in my case is
mongodb://localhost:27017/ilovearisti
How different would it be in fastcommet? I guess I'll have to contact them for the port numbers right?
Thanks,
Theo.
FastComet Team here! Indeed, our hosting plans are fully compatible with Node.js apps and you can have your project hosted on an appropriate package.
If you plan using the MongoDB database type in particular, our experience shows it requires a server solution with more resources and a higher level of private access. This way you would be able to accommodate the operation on an environment suitable for coding with Mongoose on top of the MongoDB driver for Node.js.
As for addressing your URL query, the default IP when starting a mongo instance is set to 127.0.0.1, so it is configured as localhost and port 27017. With this in mind, you only need to make changes on the IP configuration via the mongod daemon if you are using a remote database setup with a different IP address.
Having said all of the above, we can confirm that if you have created a database called iloveiristi, the MongoDB URL will be as follows:
mongodb://localhost:27017/iloveiristi
We hope this information proves useful and we wish you good luck with your project.
Best regards!

Procedure for creating a database locally using mongodb

I've connected to mLab using MongoClient.connect, but now I want to create a local database rather than use mLab. I've tried changing the URL to "mongodb://localhost:27017/mydb" to create a database locally as suggested, but I'm getting a failed to connect to server error. Is there anything I should install? Or am I just missing something?
First, install your mongoDB server:
https://docs.mongodb.com/manual/installation/
After that, you should be able to start your local mongodb server:
sudo service mongod start //in case of linux
Here the docs to start the mongodb server
Then, you will type mongo and the client command line will start.
And when typing use yourDatabase, mongo will create the database if
it does not exist.
Then, your server will be up, and the database created. Anyway, check the docs if you need the guides for windows or other systems.
First you need to understand how server works and mlab works. mLab is a company who provide database as a services. Means you no need to handle server hurdles, they take care server , backups , infrastructure etc. They provide mongodb server , You just need to create database on mongodb server's and use them.
Now comes to main points.
If you want use mongodb on your local system then you first need to install mongodb on your local system and then connect it.

Connecting to a mongoDB with a TCP prefix on a NodeJS application

I created a NodeJS app that connects with a MongoDB and it seems to work locally.
Now, after hosting the NodeJS application on a remote machine, I am trying to connect to a mongoDB that was already created on that machine. When I print out some of the environment variables, the only one I see of relevance seems to be :
MONGODB_PORT: 'tcp://172.30.204.90:27017',
I tried connecting like I usually do with
mongoose.connect('mongodb://localhost:27017/metadata/')
and replacing it with mongoose.connect('tcp://172.30.204.90:27017/metadata') but I get an error that says my URI needs to start with 'mongodb'.
So I tried replacing it with mongoose.connect('mongodb://172.30.204.90:27017/metadata') and it no longer throws any error. But on the MongoDB side I don't see any new connections happening and my app does not start up on the machine. What should I be putting in the URI?
Your URI should indeed start with mongodb:
mongoose.connect('mongodb://username:password#host:port/database?options...');
See this page for more information: https://docs.mongodb.com/manual/reference/connection-string/
Did you try to connect to the database from cli? Or Telnet to see that connection isn't blocked.

I want to connect openshift mongodb database from my local client tools

I have my premium openshift account
I have all types of access in openshift
I have ssh key & user's credentials
I have MongoVUE & Robomongo & others client tools to open mongodb database.
when I try to open any local mongodb database then its working fine
but I want to directly connect my mongodb database of openshift server to mongoVUE or any else
so I can see which data is in my server database.
I have tried so many times to connect locally but it can connect to openshift but can not connect with mongodb database so please provide some good & exact steps so I can connect it.
I use mongovue also. Just do a
'rhc port-forward '
Then run mongovue using the Mongodb credentials that OPENSHIFT gave you.
Steps.
rhc port-forward <app-name>
Wait a couple of seconds. You then should see something like this.
Then run Mongovue. Add a connection and enter in your Openshift Mongodb credentials like this:
Save, Select your connection, and press connect. It should work.
You should have been given you mongodb credentials when you added the mongodb cartridge. You can also ssh into your app and do a "env" and see your mongodb password and username.
Finally I got perfect tool for it to connect with mongodb with server database in which we can perform all kind of CRUD operations.
we can use Robomongo tool for it which doesn't require paid version to perform CRUD operation.
Robomongo is available here
http://robomongo.org/download.html
mongoVUE requires paid version.
I use Robomongo. Just do a 'rhc port-forward '
Then run Robomongo using the Mongodb credentials that OPENSHIFT gave you.
Steps.
rhc port-forward
Wait a couple of seconds. You then should see something like this.
port forward image
Then run Robomongo. Add a connection and enter in your Openshift Mongodb credentials:

Connecting to Redis databases via Node in Heroku

About Heroku: App is Nodejs, Express, WebSockets ...
I'm testing an app out on a small free Heroku instance.
I have my own Redis database instances through OpenRedis and RedisToGo.
Those have not been provisioned through Heroku addons but directly from the providers.
I connect to them fine when running locally. What special do I need to do to connect to them through Heroku?
I was kind of hoping to avoid special Heroku-specific environment variables, but am happy to use them if necessary.
But I definitely need to avoid the necessity of provisioning the Redis addons through Heroku.
Again, I have already my database instances provisioned directly with the relevant providers.
Everything works fine locally under NODE_ENV=production npm start, which indicates to my app that it should connect to the remote Redis instances (RedisToGo and/or OpenRedis) instead of the local Redis server.
I'm using Redis for at least three things:
Session store :: Connect-Redis :: This works fine locally (app server running locally but connecting to remote Redis instance) and also seems to work on Heroku-- I've checked by flushing the database and then pushing a slightly new version to Heroku and the session variable key is refreshed.
Database :: Node-Redis :: This works fine locally also (also app server running locally but connecting to remote Redis instance for database), but does not seem to be working on Heroku.
WebSockets Transport Pub/Sub :: I haven't tested this thoroughly. I'm guessing it's working but it's not important for this question.
What is most interesting to me,
is that the Heroku instance is able to connect to the Redis instance via the Connect-Redis client, but is not able to connect to the same Redis instance via the database client. Whereas my locally running version is able to connect to the same (remote) instance via both of these clients. (I have verified these findings through terminal clients, using flushall command and waiting for keys to be refreshed.)
Any insight would be greatly appreciated.
I'll post my edits and updates via comments below because they're timestamped.
It turns out we can't connect to a database (not sure why because I don't know the internals of Heroku's config over AWS) the 'normal' way through Heroku; we need to set the environment variable with the heroku config:set command. OpenRedis provides a terminal ready string, can probably put something similar together for self-provisioned RedisToGo instances, though I haven't done it yet.

Resources