Database authentication is very important when creating a web application.
Authentication is for identifying users and provide different access rights and content depending on their access.
if you had enabled MongoDB authentication, then you must use username, password and authentication database with options.
This example should clear things up and provide a basic instruction.
From this demo, you can connect MongoDB database using authentication in mongoose - nodejs.
More description of options and parameters or database object.
host :
In this option, you should pass you hostname / domain or IP address if you have configured in the database.
By default, it should be localhost.
db :
Database name which you want to use in your web application
port :
Define your database port number if you have changed or set manual port.
By default, it should be 27017.
options:
if you have enabled MongoDB authentication, you must use options for authentication related information.
user :
username who has permission.
pass :
password of the authenticated user.
authdb :
This option is very important because all user access controls are stored in the defined database.
You have to pass authdb which is used at create time user access and permissions.
ex : admin, supperAdmin, subAdmin, etc.
If your connection will be successfully done, you will show "MongoDB connection successful" message in console.
Thanks.
Authentication is for identifying users and provide different access rights and content depending on their access.
if you had enabled MongoDB authentication, then you must use username, password and authentication database with options.
This example should clear things up and provide a basic instruction.
var express = require("express");
var http = require("http");
var app = express();
var mongoose = require("mongoose");
database = {
// the database url to connect
host: 'localhost',
db: 'database name',
port: '27017',
options: {
user: "database user name",
pass: "database password",
auth: {
authdb: 'database authentication db name'
}
}
}
mongoose.connect(database.host, database.db, database.port, database.options, function (err) {
if (err) {
console.log("connection error:", err);
} else {
console.log("MongoDB connection successful");
}
});
var http = require("http");
var app = express();
var mongoose = require("mongoose");
database = {
// the database url to connect
host: 'localhost',
db: 'database name',
port: '27017',
options: {
user: "database user name",
pass: "database password",
auth: {
authdb: 'database authentication db name'
}
}
}
mongoose.connect(database.host, database.db, database.port, database.options, function (err) {
if (err) {
console.log("connection error:", err);
} else {
console.log("MongoDB connection successful");
}
});
From this demo, you can connect MongoDB database using authentication in mongoose - nodejs.
More description of options and parameters or database object.
host :
In this option, you should pass you hostname / domain or IP address if you have configured in the database.
By default, it should be localhost.
db :
Database name which you want to use in your web application
port :
Define your database port number if you have changed or set manual port.
By default, it should be 27017.
options:
if you have enabled MongoDB authentication, you must use options for authentication related information.
user :
username who has permission.
pass :
password of the authenticated user.
authdb :
This option is very important because all user access controls are stored in the defined database.
You have to pass authdb which is used at create time user access and permissions.
ex : admin, supperAdmin, subAdmin, etc.
If your connection will be successfully done, you will show "MongoDB connection successful" message in console.
Thanks.
No comments:
Post a Comment