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
Related
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
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?
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
I'm trying to use the mysql module to connect to my database. But everytime, I get the following error: read eCONNRESET There is problem. (Note, that last part is from my console log. See below.)
I don't think this is a problem with database security settings. I've been trying to connect to my new database (hosted on AWS) for the last several days with no luck. Then, just now I attempted to connect to an Azure database that has been running smoothly for a couple years. Same problem: read eCONNRESET.
By the way, if I randomly change the host string to something invalid, my code returns an error saying the host wasn't found. So that tells me it's working to some extent.
I'm very new to the coding world and need all the help I can get.
Here's my code:
console.log('starting Launch');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '....windows.net',
user : 'test',
password : 'test',
port : '1433'
})
console.log('step2')
connection.connect(function (err) {
if (!err)
console.log("conncetd");
else
console.log(err + "There is problem");
});
Copy full error message.
Check connectivity to your DB instance, use nmap (linux) or telnet (windows). If you can't reach host - check your local machine and server firewall, restrictions. If you can, go to 2.
Try to use different MySQL client, MySQL WorkBench, HeidiSQL, DBeaver.
If you can't - than something wrong with MySQL configuration. If you can, go to 3.
Copy info about: OS, node version, mysql module version.
you could try
mysql.createPool({});
instead of
mysql.createConnection({})
I am having issues connecting my express app to MongoDB via Mongoose. There is no complicated set up, its fairly basic:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/homeApp');
var db = mongoose.connection;
db.on("connecting", function(){
console.log("Connecting to DB...");
});
db.on('connected', function(){
console.log('Connected to db');
});
db.on('error', function(error){
console.log('Error');
});
Simple enough, right? But none of these events are being fired. Mongo is running ok and it's on the default port. I have also tried changing localhost to 127.0.0.1 and checked the contents of my etc/hosts file and all seems ok. If I mangle the URI string to something like:
'mongoldb://lclhost/homeApp'
I get an error saying it cannot connect, which is to be expected so it seems like a connection is being made/attempted but nothing is happening.
I've logged the contents of db.Db to the console and noticed this connected: false, which suggests it's not even attempting a connect?
Any ideas? This is Mongoose 4.7.1 running on macOS Sierra 10.12.5.
Thanks in advance!
Figured it! I tried using the mongodb client without mongoose and everything worked as expected, so I knew the connection was ok and that left the blame with mongoose.
Turns out I was not using 4.7.1 as I had thought, and that just running npm install mongoose --save had in fact installed version 3.x, which is not compatible with the latest version of MongoDB according to the Mongoose docs. http://mongoosejs.com/docs/compatibility.html
Upgrading the version number in my package.json file to 4.7.1 fixed the issue.
the mongoose.connect method takes in a callback, which you could use to check the connection:
mongoose.connect(uri, options, function(error) {
// Check error in initial connection.
if (error) {
console.log(error);
} else {
console.log('connected ok');
}
});
I think your connection is being created, but your db.on events are not being triggered.