服务器在 SASL 身份验证步骤上返回错误:身份验证失败
server returned error on SASL authentication step: Authentication failed
下面是我的MongoDBGoLang连接拨号。但它返回了一个恐慌“服务器在 SASL 身份验证步骤上返回错误:身份验证失败。”。我的用户名、密码、hostAddrs 和 dbName 是正确的。我在这里错过了什么?
dbName: = os.Getenv("ENV_DBNAME")
userName: = os.Getenv("ENV_DBUSER")
password: = os.Getenv("ENV_DBPASS")
dbHost: = os.Getenv("ENV_DBHOST")
mongoDialInfo: = & mgo.DialInfo {
Addrs: [] string {
dbHost
},
Database: dbName,
Username: userName,
Password: password,
Timeout: 60 * time.Second,
}
sess, err: = mgo.DialWithInfo(mongoDialInfo)
if (err != nil) {
panic(err)
}
mgo
returns 如果 username
、password
或 database
错误,则此错误。检查您的凭据两次。没有其他情况可以看到 Authentication failed
错误消息。
你报告的错误似乎是由一个 nil 指针引起的身份验证失败,你应该在使用它们创建连接之前检查数据
我们经常将 mongoexport 命令中的参数与 "Log-In" 用户混淆。
该命令需要 "Database Username" 而不是 Log-in 用户名。
这是输入错误用户名的一种可能性。
"Database Username" 可以在数据库的 "Users" 选项卡中找到
我遇到了类似的错误并添加了 --authenticationDatabase
参数,它在我们连接到远程 MongoDB
时有效
在您的代码中使用类似以下的格式:
$mongorestore --host databasehost:98761 --username restoreuser
--password restorepwd --authenticationDatabase admin --db targetdb ./path/to/dump/
我从这个 link 得到了答案:https://newbiedba.wordpress.com/2016/11/21/mongodb-3-2-server-returned-error-on-sasl-authentication-step-authentication-failed/
除了以上所有答案,唯一没有提及的原因是我的密码中有一个特殊字符“$”。我认为这是使用特殊字符的一种非常常见的做法,如果没有这个简单的提示,这可能会绊倒很多人:
使用命令行时mongo/mongostat/etc..单引号包含特殊字符的用户名或密码!
我在使用 dokku mongo:import 时遇到了同样的错误。在我的例子中,我在我的数据库名称中包含了点(句点)
当 'dokku mongo:create ' 时,您不应该在 mongodb 名称中包含点
我已将其更改为 seunghunlee 而不是 seunghunlee.net
现在这个命令有效
dokku mongo:import seunghunlee < seunghunlee.net.dump.gz
希望对您有所帮助!
我在通过 --uri
标志使用来自 Heroku 应用程序的连接字符串时遇到此错误。在我的案例中解决它的方法是添加数据库名称 -d
:
mongodb_uri="$(heroku config:get MONGODB_URI -a myapp)"
mongorestore --uri=$mongodb_uri -d heroku_7m41q4xs db/
在我的例子中,我缺少 --authenticationDatabase 和 --ssl,所以这里是将 json 文件导入 Atlas 集群上的 Mongodb 集合的完整语法(进入主分片):
./mongoimport --host mycluster-shard-00-02.d0b2r.mongodb.net:27017 --authenticationDatabase admin --username TestUser --db Test --collection Messages --type json --file RAW.json --ssl
如果您尝试将 MongoDB Atlas 连接到 Golang 应用程序,连接函数将是这样的:
func getSession() *mgo.Session {
tlsConfig := &tls.Config{}
dialInfo := &mgo.DialInfo{
Addrs: []string{<add your MongoDB shards as string array> }
Username: "<MongoDB username>",
Password: "<MongoDB password",
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
panic(err)
} else {
fmt.Printf("DB connected")
}
return session
}
在我的例子中,在 uri 中指定身份验证数据库和身份验证机制确实有所帮助
./mongoimport --uri="mongodb://root:root@localhost:27017/labelDb?authSource=admin&authMechanism=SCRAM-SHA-1" --collection=repo_item --file=/tmp/repo_item.json
下面是我的MongoDBGoLang连接拨号。但它返回了一个恐慌“服务器在 SASL 身份验证步骤上返回错误:身份验证失败。”。我的用户名、密码、hostAddrs 和 dbName 是正确的。我在这里错过了什么?
dbName: = os.Getenv("ENV_DBNAME")
userName: = os.Getenv("ENV_DBUSER")
password: = os.Getenv("ENV_DBPASS")
dbHost: = os.Getenv("ENV_DBHOST")
mongoDialInfo: = & mgo.DialInfo {
Addrs: [] string {
dbHost
},
Database: dbName,
Username: userName,
Password: password,
Timeout: 60 * time.Second,
}
sess, err: = mgo.DialWithInfo(mongoDialInfo)
if (err != nil) {
panic(err)
}
mgo
returns 如果 username
、password
或 database
错误,则此错误。检查您的凭据两次。没有其他情况可以看到 Authentication failed
错误消息。
你报告的错误似乎是由一个 nil 指针引起的身份验证失败,你应该在使用它们创建连接之前检查数据
我们经常将 mongoexport 命令中的参数与 "Log-In" 用户混淆。 该命令需要 "Database Username" 而不是 Log-in 用户名。 这是输入错误用户名的一种可能性。 "Database Username" 可以在数据库的 "Users" 选项卡中找到
我遇到了类似的错误并添加了 --authenticationDatabase
参数,它在我们连接到远程 MongoDB
在您的代码中使用类似以下的格式:
$mongorestore --host databasehost:98761 --username restoreuser
--password restorepwd --authenticationDatabase admin --db targetdb ./path/to/dump/
我从这个 link 得到了答案:https://newbiedba.wordpress.com/2016/11/21/mongodb-3-2-server-returned-error-on-sasl-authentication-step-authentication-failed/
除了以上所有答案,唯一没有提及的原因是我的密码中有一个特殊字符“$”。我认为这是使用特殊字符的一种非常常见的做法,如果没有这个简单的提示,这可能会绊倒很多人:
使用命令行时mongo/mongostat/etc..单引号包含特殊字符的用户名或密码!
我在使用 dokku mongo:import 时遇到了同样的错误。在我的例子中,我在我的数据库名称中包含了点(句点)
当 'dokku mongo:create ' 时,您不应该在 mongodb 名称中包含点 我已将其更改为 seunghunlee 而不是 seunghunlee.net 现在这个命令有效
dokku mongo:import seunghunlee < seunghunlee.net.dump.gz
希望对您有所帮助!
我在通过 --uri
标志使用来自 Heroku 应用程序的连接字符串时遇到此错误。在我的案例中解决它的方法是添加数据库名称 -d
:
mongodb_uri="$(heroku config:get MONGODB_URI -a myapp)"
mongorestore --uri=$mongodb_uri -d heroku_7m41q4xs db/
在我的例子中,我缺少 --authenticationDatabase 和 --ssl,所以这里是将 json 文件导入 Atlas 集群上的 Mongodb 集合的完整语法(进入主分片):
./mongoimport --host mycluster-shard-00-02.d0b2r.mongodb.net:27017 --authenticationDatabase admin --username TestUser --db Test --collection Messages --type json --file RAW.json --ssl
如果您尝试将 MongoDB Atlas 连接到 Golang 应用程序,连接函数将是这样的:
func getSession() *mgo.Session {
tlsConfig := &tls.Config{}
dialInfo := &mgo.DialInfo{
Addrs: []string{<add your MongoDB shards as string array> }
Username: "<MongoDB username>",
Password: "<MongoDB password",
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
panic(err)
} else {
fmt.Printf("DB connected")
}
return session
}
在我的例子中,在 uri 中指定身份验证数据库和身份验证机制确实有所帮助
./mongoimport --uri="mongodb://root:root@localhost:27017/labelDb?authSource=admin&authMechanism=SCRAM-SHA-1" --collection=repo_item --file=/tmp/repo_item.json