Closed TranXuanHoang closed 3 years ago
Can you please print out err.reason.servers
and paste the result here?
One other possible explanation for this is authentication failed. Are you able to connect to this Atlas cluster using the mongo
shell from the command line?
Mongoose have their owns username and password variables.
REPLACE:
process.env.MONGO_URI = mongodb+srv://<username>:<password>@<cluster>.asdf1234.mongodb.net/<dbname>?retryWrites=true&w=majority
FOR THIS:
process.env.MONGO_URI = mongodb+srv://asdf1234.mongodb.net/
And finally add user and password params on your start code
const DB = mongoose.connect(process.env.MONGO_URI, {
user: 'username',
pass: 'Password',
dbName: 'databasename',
w: 'majority',
retryWrites: true
});
@vkarpov15 Thank you for your reply!
Here is the content of the err.reason.servers
:
Map(3) {
'devcluster-shard-00-00.ig6hm.mongodb.net:27017' => ServerDescription {
_hostAddress: HostAddress {
isIPv6: false,
host: 'devcluster-shard-00-00.ig6hm.mongodb.net',
port: 27017
},
address: 'devcluster-shard-00-00.ig6hm.mongodb.net:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1164506954,
lastWriteDate: 0,
error: MongoNetworkError: certificate is not yet valid
at connectionFailureError (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:390:14)
at TLSSocket.<anonymous> (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:358:16)
at Object.onceWrapper (node:events:514:26)
at TLSSocket.emit (node:events:394:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
},
'devcluster-shard-00-01.ig6hm.mongodb.net:27017' => ServerDescription {
_hostAddress: HostAddress {
isIPv6: false,
host: 'devcluster-shard-00-01.ig6hm.mongodb.net',
port: 27017
},
address: 'devcluster-shard-00-01.ig6hm.mongodb.net:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1164506989,
lastWriteDate: 0,
error: MongoNetworkError: certificate is not yet valid
at connectionFailureError (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:390:14)
at TLSSocket.<anonymous> (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:358:16)
at Object.onceWrapper (node:events:514:26)
at TLSSocket.emit (node:events:394:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
},
'devcluster-shard-00-02.ig6hm.mongodb.net:27017' => ServerDescription {
_hostAddress: HostAddress {
isIPv6: false,
host: 'devcluster-shard-00-02.ig6hm.mongodb.net',
port: 27017
},
address: 'devcluster-shard-00-02.ig6hm.mongodb.net:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1164507365,
lastWriteDate: 0,
error: MongoNetworkError: certificate is not yet valid
at connectionFailureError (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:390:14)
at TLSSocket.<anonymous> (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:358:16)
at Object.onceWrapper (node:events:514:26)
at TLSSocket.emit (node:events:394:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
}
One other possible explanation for this is authentication failed. Are you able to connect to this Atlas cluster using the mongo shell from the command line?
No, I tried but wasn't able to connect.
>mongosh "mongodb+srv://devcluster.ig6hm.mongodb.net/auth" --username devdb-admin
Enter password: ****************
Current Mongosh Log ID: 6159072f46e048ec94c0c683
Connecting to: mongodb+srv://devcluster.ig6hm.mongodb.net/auth
Error: querySrv EREFUSED _mongodb._tcp.devcluster.ig6hm.mongodb.net
But I was able to connect to the MongoDB
on Atlas
using MongoDB Compass
with the same username
and password
(however, I need to downgrade the connection to a form of mongodb://
instead of mongodb+srv://
even though my Compass
's version is 1.28.4
which should be able to connect to the DB using a connection string with the format of mongodb+srv://
). Another strange thing is that the connection was successful for the last 2 months and suddenly it becomes unsuccessful while no changes were added to the source code and no changes were made to the configuration of the DB Cluster
or users and network access
on the Atlas
as well.
@Zixasis Thank you for your comment!
{ user: 'username', pass: 'Password', dbName: 'databasename', w: 'majority', retryWrites: true }
The current definition of the ConnectOptions
object passed to the mongoose.connect()
(Mongoose version 6.0.8
) method is as follows and has neither w
nor retryWrites
. So passing those two options
will cause an error. I also tried removing those w
and retryWrites
options, I still got back the same error.
interface ConnectOptions extends mongodb.MongoClientOptions {
/** Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection. */
bufferCommands?: boolean;
/** The name of the database you want to use. If not provided, Mongoose uses the database name from connection string. */
dbName?: string;
/** username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility. */
user?: string;
/** password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility. */
pass?: string;
/** Set to false to disable automatic index creation for all models associated with this connection. */
autoIndex?: boolean;
/** Set to `true` to make Mongoose automatically call `createCollection()` on every model created on this connection. */
autoCreate?: boolean;
}
@TranXuanHoang the issue is this: "certificate is not yet valid". According to this SO, this may be indicative of system time issues on your machine. Can you please make sure your system time is accurate?
Also, are you able to connect by using the mongodb://
connection string rather than the mongodb+srv://
string?
@vkarpov15 Thank you for your suggestion! I actually run an Express.js app inside a node alpine
Docker container with time automatically set up to local time. Last weekend the code to connect to a MongoDB hosted on Mongo Atlas suddenly failed with the error I shared with you above. However, after waiting for 2 days, now the connection has gone back to normal - without any new revisions added to the source code or changing any configurations. I still don't know why such strange thing happened, but anyway I am able to continue focusing on writing queries inside my source code to interact with the database again. Once again thank you very much for checking and replying to my question.
I have the same error today without any changes in my code...
Do you want to request a feature or report a bug? bug
What is the current behavior? The connection to a MongoDB hosted on Mongo Atlas suddenly throws out an error. I have been searching on the Internet (Stackoverflow, ...) for a day about this bug, but haven't been able to figure out why it happens. Here are the detailed configs and error:
Connections from any IPs are allowed
Connection code
Connection string's format
Error
Dependencies' version
If the current behavior is a bug, please provide the steps to reproduce.
Create an
Express
app with thedependencies
,MongoDB
connection string, connection code,network access
config as above. Then run the apptsconfig.json
What is the expected behavior?
Connections to MongoDB on Atlas should be successful.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.