I've been trying for over 2 hours now trying to figure out what's wrong with this database. I've tried everything. From reinstalling the server, restarting the processes, rebooting and so much more. It keeps giving me this error when trying to connect:
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (D:\TheShed\MX_\node_modules\mongoose\lib\connection.js:797:32)
at D:\TheShed\MX_\node_modules\mongoose\lib\index.js:330:10
at D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:1151:10)
at Mongoose.connect (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:329:20)
at module.exports (D:\TheShed\MX_\other\DB\mong.js:4:20)
at D:\TheShed\MX_\app.js:195:37
at Object.<anonymous> (D:\TheShed\MX_\app.js:197:3) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1421094,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:293:20)
at Socket.<anonymous> (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:267:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
This error wont resolve no matter what I do. The MongoDB server is running, I've checked by doing >show dbs and it lists them perfectly fine. Also, C:/data/db exists and its fine too.
What do I do?
Here's my connection code:
(async () => {
await require('./other/DB/mong')();
console.log("Connected to Database.");
})();
and here's ./other/DB/mong:
var mongoose = require("mongoose");
module.exports = async () => {
await mongoose.connect('mongodb://localhost:27017/MX', {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true
});
return mongoose;
}
I just found a solution in the internet ,
If you are using latest nodejs (v17.x) , then try updating mongodb url from localhost to 127.0.0.1
This worked for me :slightly_smiling_face:
Updated my mongodb url from 'mongodb://localhost:27017/student' to 'mongodb://127.0.0.1:27017/student' and it worked fine for me
MongoDB does not bind to localhost on ipv6 by default.
If you want it to listen on ::1 you will need to enable net.ipv6 and either enable net.bindIpAll or explicitly list the loopback address in net.bindIp
update from
'mongodb://localhost:27017/myapp' to
'mongodb://127.0.0.1:27017:27017/myapp'
then check to be sure it connects.
mongoose
.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('DB Connected'));
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
}
use mongoose.connect("mongodb://127.0.0.1:27017/databasename"); instead of localhost
just change this, mongoose.connect('mongodb://localhost:27017/Database-name');
---> mongoose.connect("mongodb://127.0.0.1:27017/Database-name");
If you are using latest nodejs. If connecting fails on your machine, try using 127.0.0.1 instead of localhost like
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
you can also use mongodb://0.0.0.0:27017/database_name
step 1. check MongoDB service is running -> if OK.
try next:
step 2. replace 127.0.0.1 from localhost -> this should work 100%
Related
I keep getting this error although I have allowed access from anywhere in the Network access section of the cluster. (Screenshot of the same is attached below)
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/
at NativeConnection.Connection.openUri (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/connection.js:824:32)
at /home/runner/store-cipher-backend-1/node_modules/mongoose/lib/index.js:380:10
at /home/runner/store-cipher-backend-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/index.js:1225:10)
at Mongoose.connect (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/index.js:379:20)
at intialDbConnection (/home/runner/store-cipher-backend-1/db/db.connect.js:5:20)
at Object.<anonymous> (/home/runner/store-cipher-backend-1/index.js:15:1) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'cluster0-shard-00-00.fkvic.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01.fkvic.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-02.fkvic.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-on2j4f-shard-0',
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
Here is the connection code
const mongoose = require('mongoose');
const mySecret = process.env['mongoUrl']
const intialDbConnection = async () => {
try {
await mongoose.connect(mySecret, {
useNewUrlParser: true,
useUnifiedTopology: true
})
console.log("db connected")
}
catch (error) {
console.error(error);
}
}
module.exports = { intialDbConnection }
And the entire code is here https://replit.com/#KumaraswamyA/store-cipher-backend-1
Access from anywhere mongodb access management
If you are looking for an answer follow this thread. mongoDB community support
The MongoDB community has made it clear that the error is not from their end.
and after reaching out to Replit. They responded that they are aware of the issue and have provided a temporary solution:
“You can run “kill 1” in the shell of your Repl which will reboot it and may fix the issue, although it isn’t a 100% reliable workaround.”
Apart from this the solution that has worked for me is changing the file name and running, the DB gets connected. In case you have already used the APIs in the frontend, after doing the above step change back your file name to the old one and the DB gets connected without any issues.
Cheers.
I have been working on an small app and connecting with Heroku PostgreSQL, for many days was working right but now is showing me this SSL error
The server does not support SSL connections
I have been looking for solutions but I cannot find anything that works for me, my code is:
import pg from 'pg'
import db from '../config.js'
const pool = new pg.Pool({
host: db.host,
database: db.database,
user: db.user,
port: db.port,
password: db.password,
ssl: { rejectUnauthorized: false },
})
export default function query(text, params) {
return pool.query(text, params)
}
Try changing it from Pool to Client and then using that to connect and query.
async function get(){
const client = new pg.Client({
ssl: {
rejectUnauthorized: false
},
user: ...,
password: ...,
port: ...,
host: ...,
database: ...
});
client.connect();
const response = await client.query(`SELECT * FROM ...;`)
return response.rows
}
Double check your values in the Heroku database.
Also, in production, you should just need
const client = new pg.Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
},
}
The first set of code is for local connection to your db.
One last note, I would put your user, password, etc... into a .env file and don't commit that to your repo.
UPDATE:
You can also put this into a config file like ./db.config.js as the following
const pg = require('pg')
module.exports =
process.env.DATABASE_URL
?
new pg.Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
},
})
:
new pg.Client({
// connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
},
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
port: process.env.DATABASE_PORT,
host: process.env.DATABASE_HOST,
database: process.env.DATABASE
})
So if it is in production, it will use the database url, and if there is none (which is local) then it will use the username password connection.
I'm just getting started with MongoDB. After installing it and running the code I'm getting an error.
Code:
mongoose.connect('mongodb://localhost:27017/yelp-camp', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", ()=> {
console.log("Database connected");
});
Error:
First part of error
Second part of error
ECONNREFUSED usually means that your services isn't available. Is seems to me that your mongodb isn't running or is listening on a different port
Maybe try port 27018 or 27019
https://docs.mongodb.com/manual/reference/default-mongodb-port/
I am trying to establish a fixed connection pool using the typeorm for postgres in my rest service.
let option = {
name: 'default',
type: 'postgres',
host: 'host',
port: port,
username: 'username',
password: 'password',
database: 'database',
synchronize: true,
dropSchema: false,
logging: true,
ssl: {
ca: process.env.SSL_CERT,
},
entities: ['src/**/*.entity.ts'],
}
In app.ts
await createConnection(option).then(() => {
logger.debug('Database connection established');
}).catch((error: any) => {
logger.error('Database connection failed', error);
throw new Error(error);
});
When the app gets initialised the connectionpool gets established via ssl but it then closes, every time a query is executed the connection is created and then closed.
But without the ssl the connectionpool does not close until the .close method is called.
It would be helpful if someone could let me know on how to prevent the connectionpool from closing.
Or is there an option to set the time to live in ssl connection for postgres but I couldn't find reference for that in typeorm documentation.
My connection to my mongo database will close or timeout if left in active. I'm getting the following error when I leave my app in active "no open connections"
My mongo DB is running in a replication set up on AWS. I'm using the following options when connecting using mongoose. I'm unsure if any other flags should be set. I was basing my options on teh monodo node driver doc # http://mongodb.github.io/node-mongodb-native/api-generated/server.html.Users will be in my applicaiton for 8+ hours at a time and I don't want anything to timout when they go to lunch or leave for a meeting.
MongoOptions : {
user: 'root',
pass: '********',
replset: {
auto_reconnect: true,
poolSize: 25,
socketOptions: { keepAlive: 1 },
ssl: true,
sslCert: fs.readFileSync('./server/config/ssl/mongodb-cert.crt'),
sslKey: fs.readFileSync('./server/config/ssl/mongodb-cert.key')}
}
mongoose.connect('mongodb://server.com:27017', config.MongoOptions);
The connection getting closed turned out to be another issue that was causing the problem.
We have a replicate set running w/ SSL and are able to keep the connection up for days using the following config:
MongoOptions : {
user: 'ssssss',
pass: 'xxxxxx',
replset: {
auto_reconnect: false,
poolSize: 10,
socketOptions: { keepAlive: 1 },
ssl: true,
sslCert: fs.readFileSync('./server/config/ssl/mongodb-cert.crt'),
sslKey: fs.readFileSync('./server/config/ssl/mongodb-cert.key')}
}
Try adding
connectTimeout: 43200000
that should be good for 12 hours