When connecting to my MongoDB Atlas server from a node.js server, the following code works completely fine on Heroku:
mongoose.connect(process.env.dbLink, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const db = mongoose.connection
db.once("open", () => {
console.log("Database connected")
})
However, when I run this on my computer, I get the following error:
const serverSelectionError = new ServerSelectionError();
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
However, my IP address is whitelisted.
How can I make this work on my local environment?
Related
Why my mongo dB is not connecting with nodejs? its showing thi error in console
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/ its also not consoling Database connected! and in Atlas its showing Current IP Address not added. You will not be able to connect to databases from this address. i tried to add ADD IP ADDRESS but when ever i am refreshing it than its showing IP Address not added again and again
const mongoose = require("mongoose");
require('dotenv').config();
mongoose.connect(process.env.MONGO_URL).then(
() => {
console.log("Database connected!")
}
).catch((err) => {
console.log(err)
})
env:
MONGO_URL = 'mongodb+srv://app123:app123#cluster0.w6xczff.mongodb.net/?retryWrites=true&w=majority'
JWT_SECERT = 'app'
I have a node.js express server deployed on Azure App Services, which connects to an Azure flexible postgresql database to serve requests. When running the server locally the postgres database can access the database fine, but when the server is deployed to an azure app service, all requests time out:
The server uses a pool to make requests, this is what my server config file looks like:
const {Pool} = require('pg');
require('dotenv').config();
const config = {
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DB_NAME,
port: process.env.PORT,
ssl: true,
max: 50,
idleTimeoutMillis: 10000,
allowExitOnIdle: true,
}
module.exports = new Pool(config);
So far I've tried:
Allowing all IP addresses to access the database
Allowing all services within Azure to access the database
Giving the server's App service contributor permissions to the database.
But none of these solutions have prevented requests from timing out, so any help would be greatly appreciated.
Thank you
I have thankfully found the solution to this problem!
I suspect that the Azure App Service overrides the PORT env variable to 8080, and so the port variable used to connect to the database ends up being wrong when a connection is attempted.
I've renamed the .env variable to something else and it is now working correctly.
I recently ran into this problem. When I try to run my Node.js app, I get this error MongooseServerSelectionError: connect ETIMEDOUT. It also said type: 'ReplicaSetNoPrimary'. For the past few months it was working perfectly and I had never got this error. I am using MongoDB Atlas for the database and Mongoose for its driver. I am using the latest version of Mongoose.
Here is my app.ts:
const CONNECTION_URL = "mongodb+srv://name:<password>#cluster0.vyegx.mongodb.net/MyApp?retryWrites=true&w=majority"
mongoose.connect(CONNECTION_URL).then(_INIT_)
async function _INIT_(){
const server = app.listen(PORT, ()=>{
console.log("listening on port "+PORT+"...")
});
const io = new Server(server);
}
I have tried to whitelist my ip, but it's not working.
UPDATE It works perfectly with no errors in Heroku but when I try to run it from my computer it gives an error.
need to set useUnifiedTopology: false
I am unable to pass requests with postman, and the error message is the one being printed on console. Before applying then method I was able to see the connection successful mssage but still wasn't able to get/post via postman
//database connection
mongoose.connect(
process.env.MONGO_URL,
{ useNewUrlParser: true, useUnifiedTopology: true }).then(()=>{
console.log("connection successful")
}).catch((error)=>{
console.log("Error occured on connection")
});
Requiring the connection via
const mongoose = require("mongoose");
and the MONGO_URL mentioned in .env file is
MONGO_URL = mongodb+srv://username:password#cluster0.8noo9.mongodb.net/dbname?retryWrites=true&w=majority
been stuck here for a while. I am setting up mongoose for the first time
It was due to not whitelisting m IP. If anyone has the same issue try it first. Go to mongodb and whitelist your IP in security -> network connections
I have a Cloud Function that I want to connect to a SQL Server Instance.
By documentation, you can only connect using a private IP.
Everytime I try to connect I get the error:
ERROR: (gcloud.functions.call) ResponseError: status=[400], code=[Bad Request], message=
[Function failed on loading user code. Error message: {"code":"ELOGIN","originalError":
{"message":"Logon failed for login 'sqlserver' due to trigger execution.","code":"ELOGIN"},"name":"ConnectionError"}]
My Cloud Function code:
const sql = require('mssql');
exports.test = (req, res) => {
const config = {
user: 'sqlserver',
password: 'test',
server: '10.60.80.3',
port:1433,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
}
};
const pool = new sql.ConnectionPool(config);
pool.connect()
.then(() => {
res.status(200).send({message: "Connection ready."});
})
.catch(err => {
res.status(500).send(err);
pool.close();
});
};
I have done the following configurations:
VPC Connector on default network
The default network was applied to Private IP configuration of SQL Server instance.
SQL Server Instance configuration
When creating the Cloud Function, you are required to select a service account and a VPC connector. I chose the connect-ip-sql connector. For service account, I tried with App Engine Default Service Account and Compute Engine Service Account ( both given the role of Cloud SQL client). Same error.
It seems to be a problem with SQL Server login and not about finding the network since I tried changing the network (setting the SQL Server in another VPC) and it returned CONNECTION TIMEOUT.
I can login using public ip in my SSMS using the default username-password. When I run the query:
select * from sys.server_triggers
There are three server triggers but there is no information what they do.
gcloudsql_RoleManagement
TRG_ProtectDropCustRootLogin
TRG_DisableRemoteConnectionForDbRoot
You cannot drop or change them, because Cloud SQL is a managed service and you have no access on the 'sa' superuser.
It would be best to request Google Cloud Support assistance (public tracker is mostly for bugs) since it could be a lot of things that could go wrong here.
But with that said, I assume you have the following in place:
GCP firewall rules that allows traffic from/to your Cloud Funtion and Cloud SQL MSSQL
Cloud SQL uses VPC peering for RFC-1918 connections, so I assume that your VPC connector route is there as exported and your MSSQL route is there as well as imported
Your Cloud Funtion VPC connector is in the same region as your Cloud MSSQL
I would recommend for you to enable flow logs and GCP firewall logs, also running connectivity test can also give you a hint on what might be blocking you