I have a node server and until now my database was local, but now I need it to be on mongolab.
Could you tell me how to connect to the database, please?
Go to https://mongolab.com/databases/YOUR_DATABASE_NAME
Copy the URI from your database's page on Mongolab.
Use var db = mongojs('mongodb://USERNAME:PASSWORD#ds043497.mongolab.com:43497/heroku_app14986764', ['CollectionName']);
And you'll be connected. I did not test this.
Related
I have a MongoDB Database set up on my host server and a docker container running on that host with a NodeJS application. In that application, I try to connect to the Database on the host but the request always times out.
I set the network to "host" and used the adress "host.docker.internal". I also tried installing mongodb-shell and it actually works! So basically I can connect to the database with the shell in the container but not with the MongoDB NodeJS API.
Code of the NodeJS application:
const mongodb = require('mongodb');
mongodb.MongoClient.connect("mongodb://host.docker.internal:27017/");
using mongo mongodb://host.docker.internal:27017/ in the shell works and I connect correctly.
I finally found out the issue.
I had to bind it to the correct IPs / in my config I used the argument --bind_ip_all.
Thanks a lot for the help!
I recently started working on a project with Express and since I'm using Node.js as backend I chose MongoDB as my database. It's my first time working with Mongo but I can't authenticate with Express, it works fine from terminal. I followed the guide from MogoDB blog here. I tried using their Atlas service where I had no problem authenticating. I'm using MongoDB driver. Here's how my connection URI looks like:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?retryWrites=true&w=majority`;
I tried changing mongodb:// to mongodb+srv:// but that resulted in invalid connection string error.
You need to specify the authentication database, usually admin:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?authSource=admin&retryWrites=true&w=majority`;
If you don't specify it then in your case MonogDB defaults the authentication database to cloud - which is most likely wrong.
When you are accessing mongodb on the web you can click on connect and on connect your application. You will show the uri to copy paste starting with
mongodb+srv://USERNAME:PASSWORD#CLUSTER/DATABASE
You forgot to specify the CLUSTER. Currently is your local Database
I am trying to connect to my local mongodb with my node express app hosted on litghtsail MEAN bitnami.
The connection string im using with mongoosse client look like this: mongodb://app:sang#127.0.0.1:27017/prisedesang
Im able to connect whit that user remotely with a SSH tunnel. Im able to connect with that same user localy using mongo cli.
However when I use the app I get ERR_CONNECTION_REFUSED when I try to connect to the db hosted on the same server... Any suggestion?
Can you try using MongooseJS? You can connect your application with MongoDB using MongooseJS, an object modeling driver for Node.js. It is already installed in the MEAN stack so you only have to add the following lines to your app.js file:
var Mongoose = require('mongoose');
var db = Mongoose.createConnection('mongodb://USER:PASSWORD#localhost/DATABASE');
I am learning NodeJS by building a JWT server. Basically I want to authorize users against credentials in a PostgreSQL database. I am considering node-postgres, passport, pg to connect with PostgreSQL but I have not found anyway to store my connection values encrypted. Ideally I would store them in a properties file so I can change them per environment.
Most examples I see do something like:
var pg = require('pg');
var conString = "postgres://YourUserName:YourPassword#localhost:5432/YourDatabase";
Can someone help show me how to encrypt and use my credentials so I don't have to hard code the plain values in my source?
There seem to exist npm packages for this already. See https://www.npmjs.com/package/secure-conf. Seems to fulfill your needs.
Please note, that you should also secure your Connection to the DB using SSL. See SSL for PostgreSQL connection nodejs for a Solution.
This should help.
if you use sequelize to connect postgres
const sequelize = new Sequelize("DB", usrname, password, {
host: "/var/run/postgresql",
dialect: "postgres",
});
NB: get the host string from your pgsl db might be different //
I have a NodeJS Express app that uses Mongoose for MongoDB. I'm confused as to how to connect it to the OpenShift database. For development, I'm connecting to a local database, which works fine. Here's what I have:
//====== MONGODB SETUP ======
mongo_url = process.env.OPENSHIFT_MONGODB_DB_HOST+":"+parseInt(process.env.OPENSHIFT_MONGODB_DB_PORT);
if(app.dev){
mongo_url = "mongodb://localhost:27017/my-db";
}
app.modules.mongoose.connect(mongo_url);
Any help would be great!
You need more than just host and port. You have to provide username and password.
A simpler way for it is to use:
mongo_url = process.env.OPENSHIFT_MONGODB_DB_URL;
OPENSHIFT_MONGODB_DB_URL has username and password too.
You have to provide username and password and the database name along with host and port.
So you can try appending OPENSHIFT_APP_NAME to the OPENSHIFT_MONGODB_DB_URL.
mongo_url = process.env.OPENSHIFT_MONGODB_DB_URL+process.env.OPENSHIFT_APP_NAME;
OPENSHIFT_MONGODB_DB_URL has the below format:
(e.g. mongodb://<username>:<password>#<hostname>:<port>/)