cyr*_*slk 11 mongodb node.js mongodb-atlas
我试过这个,但它不起作用。
Mongodb + Atlas 出现以下错误:
MongoError: bad auth Authentication failed.
at /Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/auth/auth_provider.js:46:25
at /Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/auth/scram.js:215:18
at Connection.messageHandler (/Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/connection/connect.js:334:5)
at Connection.emit (events.js:203:13)
at processMessage (/Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/connection/connection.js:364:10)
at TLSSocket.<anonymous> (/Users/cyrus/Documents/Code/01. Code/Franklin-ford/franklin-ford-server/node_modules/mongodb-core/lib/connection/connection.js:533:15)
at TLSSocket.emit (events.js:203:13)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:276:11)
at TLSSocket.Readable.push (_stream_readable.js:210:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:166:17) {
ok: 0,
errmsg: 'bad auth Authentication failed.',
code: 8000,
codeName: 'AtlasError',
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {}
Run Code Online (Sandbox Code Playgroud)
这是我实例化我的数据库的方法:
来自credentials.js:
const config = {
mongoConnectionURL: "mongodb://cyruslk:<MY_PASS_HERE>@franklinford-shard-00-00-3dveb.mongodb.net:27017,franklinford-shard-00-01-3dveb.mongodb.net:27017,franklinford-shard-00-02-3dveb.mongodb.net:27017/test?ssl=true&replicaSet=FranklinFord-shard-0&authSource=admin&retryWrites=true&w=majority",
mongoDatabaseName: "FranklinFord",
}
Run Code Online (Sandbox Code Playgroud)
从我的主要server.js应用程序:
const {MongoClient} = require("mongodb");
const connectionURL = config.mongoConnectionURL;
const databaseName = config.mongoDatabaseName;
let sendToDb = (dataToInsert) => {
MongoClient.connect(connectionURL, {
useNewUrlParser: true,
}, (error, client) => {
if(error){
console.log(error);
return console.log("Unable to connect to the db.");
}
const db = client.db(databaseName);
console.log(db);
db.collection("ford_twitter").insertOne({
masterData: dataToInsert
}, (error, result) => {
if(error){
return console.log("unable to insert users");
}
console.log("Data successfully inserted");
})
})
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到代码
谢谢你帮助我。我不知道发生了什么。
Ind*_*gns 14
I just had this problem knowing that I was using the correct username, password and DBname.
I tried to change the password for the db user to double check, but it still didn't work.
I then instead created a new user, gave the user admin role, set the password etc, and then used that new user and password (same dbname) for the connection and it worked.
I don't know exactly what the issue was, but hoping it can help some of you save some time :)
Good luck!
小智 11
我有同样的问题,在那个连接字符串中它是写的"mongodb+srv://<username>:<password>@cluster0.4h226.mongodb.n...",你应该在那个字符串中输入用户名和密码。
假设我的用户名是“user”,密码是password,我们应该写"mongodb+srv://user:password@cluster0.4h226.mongodb.n...",没有“<”和“>”;
如果您的密码中有任何特殊字符,请将它们替换为以%符号开头的 ASCII 代码。例如,#将是%35。
| 归档时间: |
|
| 查看次数: |
14621 次 |
| 最近记录: |