Mongoose连接身份验证失败

时间:2017-08-08 19:13:51

标签: node.js mongodb mongoose

借助此帮助,我在mongo shell中创建了一个超级用户:Create Superuser in mongo

user: "try1"
passw: "hello"

在mongo cmd中,我有3个数据库:' admin',' myDatabase'和' local'。

现在我尝试使用这个名为' myDatabase'的数据库的授权连接。

mongoose.connect('mongodb://try1:hello@localhost:27017/myDatabase');

但这是我得到的错误:

  

名称:' MongoError',
    消息:'身份验证失败。',
    好的:0,
    errmsg:'身份验证失败。',
    代码:18,
    codeName:' AuthenticationFailed' }
  猫鼬断开了   通过$ {msg}

断开Mongoose

4 个答案:

答案 0 :(得分:6)

我几小时前遇到了同样的问题,毕竟我解决了。我的代码是:

 mongoose.createConnection("mongodb://localhost:27017/dbName",{"auth":
 {"authSource": "admin"},
"user": "admin",
"pass": "password"});

答案 1 :(得分:5)

除了@kartGIS之外,我还添加了一个选项,使连接代码尽可能完美。

mongoose.connect("mongodb://localhost:27017/databaseName", {
    "auth": { "authSource": "admin" },
    "user": "username",
    "pass": "password",
    "useMongoClient": true
});

答案 2 :(得分:3)

我遇到同样的问题,通过移除“authSource”来解决问题。 PARAM

/* Not working */
mongoose.connect("mongodb://localhost:27017/test", {
    "auth": { "authSource": "admin" },
    "user": "admin",
    "pass": "admin123",
    "useMongoClient": true
});

/* Working */
mongoose.connect("mongodb://localhost:27017/test", {
    "user": "admin",
    "pass": "admin123",
    "useMongoClient": true
});

在Mongoose-v5.0.0上测试。

答案 3 :(得分:1)

在Mongodb 4.2和Mongoose 5.7.13上对我来说工作正常

Node.js

Public Class Form1
    Dim currenttime As String
    Dim messagetime As String
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        currenttime = TimeOfDay.ToString("hh:mm:ss tt")
        Label1.Text = currenttime
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick

        messagetime = MaskedTextBox1.Text + " " + ComboBox1.Text
        Label4.Text = "Reminder set for: " + messagetime
        If currenttime = messagetime Then
            Timer2.Stop()
            MsgBox(MaskedTextBox1.Text)
            Button1.Enabled = True
            Button2.Enabled = False
            Label4.Text = ""
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer2.Start()
        Button1.Enabled = False
        Button2.Enabled = True
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Timer2.Stop()
        Button1.Enabled = True
        Button2.Enabled = False
        Label1.Text = ""

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        NotifyIcon1.Visible = True
        Me.Hide()
        NotifyIcon1.ShowBalloonTip(3000)
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Show()
        NotifyIcon1.Visible = False
    End Sub
End class

/etc/mongod.conf

const Connect = async () => {

    let url = "mongodb://localhost:27017/test_db";

    try {

        let client = await Mongoose.connect( url, {
            poolSize: 10,
            authSource: "admin",
            user: "root",
            pass: "root123", 
            useCreateIndex: true,
            useNewUrlParser: true,
            useUnifiedTopology: true
        } );

        log( "Database is connected!" );
    } catch ( error ) {
        log( error.stack );
        process.exit( 1 );
    }

}
Connect();

数据库用户

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017
  bindIp: 0.0.0.0 

setParameter:
   enableLocalhostAuthBypass: false

security:
  authorization: enabled