I have load balance with three server and 1 is main server storing data into database from server 2 and 3 via tunnel ssh using mongoose, currently I have only one port let's say 1900 but I wanna increase replica member 3 or more, Anyone know how to do?
Sorry for English might not correct, Thank you
const config = {
username: 'root',
host: '128.199.xx.xx',
port: 22,
agent: 345,
dstPort: 1900,
password: 'xxx'
};
tunnel(config, function (error, server) {
mongoose
.connect("mongodb://localhost:1900/db_name", {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
replicaSet: "rs",
})
.then(() => {
console.log("MongoDB Connected");
})
});
I solved it by adding hosts to origin machine Thank you very much.We dont need any ssh tunnel any more
1. Allow your Local IP to your server
ufw allow from xx.xx.xx.xx to any port xxxx (database port)
Optional: Adding IP to host file in your local machine
For better security please addming username and password to access Databse
Related
I have a node.js express api which I host on heroku. It connects to mongodb atlas via mongoose as follows
mongoose.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PWD}#${process.env.MONGO_HOST}/${process.env.MONGO_DEFAULT_DB}?retryWrites=true`, {
useNewUrlParser: true,
autoReconnect: true,
keepAlive: 300000,
connectTimeoutMS: 300000,
socketTimeoutMS: 300000
}
)
.then(result => {
console.log('Connected and listening to requests!');
app.listen(process.env.PORT || 3000);
.catch(err => console.log(err));
I want to use Atlas MongoDB Cloud's whitelist for the Heroku app, instead of opening access up to all. Heroku doesn't have fixed a IP address but makes them possible via an add-on called Fixie Socks, which acts as a proxy for outbound traffic.
How can I use this add-on to get the connection to work? The documentation gives several examples on how to connect to other services and databases, but there is no help on mongodb. All examples use the FIXIE_SOCKS_HOST which contains the SOCKSV5 proxy URL, user, pass and port, but I'm at a loss on how to use it in conjunction with the mongoose.connect method.
This question has been asked before, but when a different add-on was used (Fixie vs Fixie Socks), which didn't work.
I tried Fixie Socks but couldn't get it to work. But managed to successfully connect to Mongodb Atlas database with Quota Guard's static IP addresses following this documentation:
https://support.quotaguard.com/support/solutions/articles/12000066411-how-to-connect-to-mongodb-using-quotaguard
The trick is to use the use mongodb atlas' connection string without +srv command. To do that use an older driver version and it will provide you a connection string with 3 replica servers (each with 27017 port). Then create 3 tunnels at the Quota Guard dashboard.
Make sure you download the gqtunnel package and extract it your app's root folder. Then you just have to add the bin/qgtunnel to your procfile.
Example:
web: bin/qgtunnel your-application your arguments
I manage to connect from Heroku + node.js + Fixie SOCK add-on to MongoDB Atlas. A few parameters in MongoClient need to be set to direct MongoDB traffic to Fixie proxy. This is the snippet:
const {MongoClient, ServerApiVersion} = require('mongodb');
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
const host = process.env.MONGO_DB_HOST;
const uri = "mongodb+srv://" + username + ":" + password + "#" + host + "/?retryWrites=true&w=majority";
const fixieData = process.env.FIXIE_SOCKS_HOST.split(new RegExp('[/(:\\/#/]+'));
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
proxyUsername: fixieData[0];
proxyPassword: fixieData[1];
proxyHost: fixieData[2];
proxyPort: fixieData[3];
});
Some of the MongoDB client connection options can be found here:
https://mongodb.github.io/node-mongodb-native/4.5/interfaces/ConnectionOptions.html
With modern versions of Mongoose, you can include the proxy-related driver options in the options object on mongoose.connect, and this works both with and without the +srv URL modifier.
const mongoose = require('mongoose');
const fixieData = process.env.FIXIE_SOCKS_HOST.split(new RegExp('[/(:\\/#/]+'));
mongoose.connect(process.env.DB_CONNECTION,
{
proxyUsername: fixieData[0],
proxyPassword: fixieData[1],
proxyHost: fixieData[2],
proxyPort: fixieData[3]
},
(error) => {
if (error){
console.log(error)
} else {
console.log('Connected to database')
}
}
)
I was trying to connect to a remote server mongoDB through SSH and made the configurations as provided
import tunnel from 'tunnel-ssh';
const config = {
username: 'username',
Password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: 'localhost',
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27018
};
This is the config that has been defined where i need to connect to the remote server 192.168.9.104. So the particular is chosen as the SSH host. Username and password for the same is provided. and the connection made is as follows.
class DB {
initDB() {
tunnel(config, (error, server) => {
if (error) {
console.log('SSH connection error: ' + error);
}
const url = 'mongodb://127.0.0.1:27018/myDBname';
mongoose.connect(url, { useNewUrlParser: true });
mongoose.plugin(toJson);
mongoose.plugin(setProperties);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log('DB connection successful');
});
});
}
}
But when the db.init() function is called following error pops up
events.js:183
throw er; // Unhandled 'error' event
^
Error: All configured authentication methods failed
I am not able to figure out where the config goes wrong. i have tried using 127.0.0.1 for dstHost. as well as put the 192.168.9.104 as the dstHost as well but the error persists. kevin lee suggests a similar approach. this question is used as an example
There was an error with the documentation which suggested the config as mentioned above with the key "Password" but it should be "password" so the config would look something like this
const config = {
username: 'username',
password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: 'localhost',
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27018
};
Rest of the implementation is spot on and tested.
I have node server on which i used mysql module as shown below
const db = mysql.createConnection({
host:'localhost',
user: 'root',
password: '',
database: 'test'
});
db.connect(function(err){
if (err) console.log('Errors', err)
console.log('connenected');
})
when i replace ip address of remote database or ip address of my local machine on which xammp is running instead of localhost it fails somehow i tried to find out solution some says i need to use port in connection but it fails. Thanks in advance
Step1: Allow database to connect remote IP by given this SQL.
GRANT ALL PRIVILEGES ON *.* TO 'root'#'ipaddressofyourpc' IDENTIFIED BY 'password' WITH GRANT OPTION;
Step2: Flush the PRIVILEGES
FLUSH PRIVILEGES;
Step3:
var mysql = require('mysql');
var connection = mysql.createPool({
connectionLimit: 100,
host:'ipaddressofremotedb',
user:'usernameofdb',
password:'password',
database:'dbname',
port: 3306,
debug: false,
multipleStatements: true
});
I am using MongoDB 4.0. I have a Replica Set which run on my machine and different ports that is:
127.0.0.1:27017 (Master)
127.0.0.1:27018 (Slave)
127.0.0.1:27019 (Arbiter)
My replica name is "xdr"
Now on creating the connection on localhost in my nodejs code it will create the connection i.e
const options = {
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
autoReconnect : true
};
mongoose.connect('mongodb://localhost:27017, localhost:27018, localhost:27019/my_db?replicaSet=xdr, options);
mongoose.Promise = global.Promise;
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
Everything is fine in this on my local connection but when i will host my mongodb On seperate EC2 Instance on AWS, it will not connect to my replica set.
lets assume that my mongodb AWS IP is 12.12.13.12.
So when i will create the connection it will not be able to connect. My code is
const options = {
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
autoReconnect : true
};
mongoose.connect('mongodb://12.12.13.12:27017, 12.12.13.12:27018, 12.12.13.12:27019/my_db?replicaSet=xdr, options);
mongoose.Promise = global.Promise;
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
It will generate an error i.e ""
if I will connect without replicaset then it will connect only to primary i.e
mongoose.connect('mongodb://12.12.13.12:27017/my_db, options);
Is there anything which I am doing wrong on my code?
Have you verified that port 27017 and 27018 are open on your EC2 ?
You don't need to specify the arbitrer.
You have to inform mongo that you use a replicaset, with the replset option.
I use the following for my replicaset :
var config = {
db: "mongodb://myMasterIp:27017/myDb,mySlave1Ip:27018/myDb"
options: {
user: config.db_user,// only if you use username/password for DB authentication
pass: config.db_pass,// only if you use username/password for DB authentication
replset: {
rs_name: "MyReplicaSetName",
ssl: true,// only if you use ssl for your replicaset
sslValidate:false,// only if you use ssl for your replicaset
sslCA: myDbCertificate,// only if you use ssl for your replicaset
ca: myDbCertificate,// only if you use ssl for your replicaset
sslKey: myDbKey,// only if you use ssl for your replicaset
sslCert: myDbKey // only if you use ssl for your replicaset
},
socketOptions : {
keepAlive : 1,
connectTimeoutMS : 5000
},
server: { // only if you use ssl for your replicaset
ssl: true,
sslValidate:false,
sslCA: myDbCertificate,
ca: myDbCertificate
sslKey: myDbKey,
sslCert: myDbKey
},
auth: { // only if you use username/password for DB authentication
authdb: 'myAuthenticationDatabse'
}
}
};
mongoose.connect(config.db, config.options);
I have a mongodb replica set with the following config:
M1 (192.168.77.3) primary (host App1)
M2 (192.168.77.4) secondary (host App2)
M3 (192.168.77.5) secondary (host App3)
M4 arbitrator
Here is my Express code to connect to db (I use mongoose as the ODM):
const config = {
db: `mongodb://192.168.77.3,192.168.77.4,192.168.77.5/mydb`,
dbOptions: {
useMongoClient: true,
reconnectTries: Number.MAX_VALUE,
replicaSet: process.env.REPLICA_SET,
poolSize: 100,
keepAlive: 1,
connectTimeoutMS: 30000,
socketTimeoutMS: 300000,
w: 1
}
}
mongoose.connect(config.db, config.dbOptions, (error) => {
if (error) {
console.log('Error on connecting to th db: ', error)
console.log('\x1b[31m', '*** PLEASE CONNECT TO DATABASE BEFORE RUN SERVER', '\x1b[0m')
process.exit(1)
}
callback()
})
The app works as expected.
When M1 get down, either M2 or M3 get elected to be the primary, the App2 and App3 still working, BUT the App1 CAN'T connect to the replica set with the error message:
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
This is my mongodb config on App1:
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: [127.0.0.1, 192.168.77.3]
replication:
oplogSizeMB: 3000
replSetName: my_rs
Is there any wrong in my config?
You CAN NOT mix IP-addresses of multiple nodes at one single config file.
Your bindIp string
bindIp: [127.0.0.1, 192.168.77.4, 192.168.77.5]
should be
bindIp: "127.0.0.1,192.168.77.3"
at first node and
bindIp: "127.0.0.1,192.168.77.4"
at second node. And so on... So, you can have localhost bind and other addresses what belong to that node.
My initial answer was wrong because I thought that it has something to do with mongooos, what I don't know.