Mongodb connection error
SudoDios opened this issue · comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
3.22.1
Plugin version
4.1.1
Node.js version
16.13.0
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
20.04
Description
Hello.
I launched a clustered mongodb and enabled authentication.
But the fastify-mongodb plugin does not connect with authentication (AuthenticationFailed).
But there is no problem with MongoClient.
Steps to Reproduce
const fastify = require("fastify");
const app = fastify();
app.register(require('fastify-multipart'))
app.register(require('fastify-formbody'))
app.register(require('./FastifyRouter'))
//database
app.register(require('fastify-mongodb'), {
forceClose: true,
url: 'mongodb://localhost:27017/db',
auth: {
username: "username",
password: "password"
}
})
app.listen(port,"hostname",(err,adrs) => {
if (err) {
console.log(err)
} else {
console.log(address)
}
})
Expected Behavior
No response
I think you should setup correctly the url
parameter such as mongodb://localhost:27017/test?authSource=admin
I used this but it did not work :
mongodb://user:pass@localhost:27017/test
.
You may write a complete url with the parameters.
(With this new url you mentioned)
(username & password)
Do not have a solution?
Do not have a solution?
I'm afraid no one owes you a solution in any given timeframe.
How do you connect with basic MongoClient?
How do you connect with basic MongoClient?
Yes. can connect.
with this url :
mongodb://user:pass@localhost:27017/test
An irrelevant question
Does fastify clustering have an effect at higher speeds ?
How do you connect with basic MongoClient?
Yes. can connect. with this url :
mongodb://user:pass@localhost:27017/test
From my knowledge of mongodb
, it means that your account is stored in test
db?
If that is the case, your connection string with mongodb://localhost:27017/db
is actually authenticating against db
which is expected to be fail.
I think simply pass mongodb://user:pass@localhost:27017/test
into url
is fine. The option is directly pass to MongoClient
, if it can connect in MongoClient
, it must be able to connect for fastify-mongodb
.
I even tested in mongodb compass and authentication was done.
But not in fastify-mongodb.
I even tested it without considering the database, but it didn't work :
mongodb://user:pass@localhost:27017
You can also take a test ?
You can also take a test ?
I always use a single url which contain both credential
, host
and options
to connect the database.
I do not have any problem with it. You can setup a repro with docker to simulate the error.
Or provide the code you are using, which I can connect my database to test it.
You can also take a test ?
I always use a single url which contain both
credential
,host
andoptions
to connect the database.
I do not have any problem with it. You can setup a repro with docker to simulate the error.
Or provide the code you are using, which I can connect my database to test it.
Can you write me a complete URL with options?
Note that if you do not specify database
or authSource
, Mongodb Compass
by default using admin
.
Which is actually mongodb://user:pass@localhost:27017/?authSource=admin
Thankful.
I will take the test again and report here
Connection String is form like below
https://docs.mongodb.com/manual/reference/connection-string/#components
mongodb://<username>:<password>@<host>:<port>/[default database]?[<options>]
Note that if username
or password
contain : / ? # [ ] @
please encode your username and password.
Okey.
thanks
I tested and authentication was successful 👍.
I just had to add the last URL authSource = admin
Note that if you do not specify
database
orauthSource
,Mongodb Compass
by default usingadmin
.
Which is actuallymongodb://user:pass@localhost:27017/?authSource=admin
if you want to link a new created db, you must add ?authSource=admin
I tried ,this is right