Note1: 当开启身份认证后,会提示无法如下错误,线需要进行处理如下db.auth(“admin”,“admin”)
r4:PRIMARY> show collections 2020-06-08T13:45:42.693+0800 E QUERY [thread1] Error: listCollections failed: { "ok" : 0, "errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {} }", "code" : 13, "codeName" : "Unauthorized" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:807:1 DB.prototype.getCollectionInfos@src/mongo/shell/db.js:819:19 DB.prototype.getCollectionNames@src/mongo/shell/db.js:830:16 shellHelper.show@src/mongo/shell/utils.js:807:9 shellHelper@src/mongo/shell/utils.js:704:15 @(shellhelp2):1:1 r4:PRIMARY> db.auth("admin","admin") Error: Authentication failed. 0 r4:PRIMARY> db.auth("admin","password") Error: Authentication failed. 0 r4:PRIMARY> use admin switched to db admin r4:PRIMARY> db.auth("admin","password") 1 r4:PRIMARY> show collections system.users system.version r4:PRIMARY>Note2: 身份认证配置好了以后,进行登录测试,mongo的登录方式有两种:
在登录期间进行身份验证 mongo --port 29017 -u "admin" -p "password" --authenticationDatabase "admin" 连接后进行身份验证 r4:PRIMARY> use admin switched to db admin r4:PRIMARY> db.auth("admin","password") 1Note3: 按以下顺序重启所有服务(这个还没有验证第二个和第三个顺序颠倒会怎样) config副本集 router服务 shard副本集
未知问题: 启用用户认证以后,出现用户都被清空了(或者没有清空,只是我看不到了),然后连接数据库都无法连接进行认证,这个时候,按照初次启用认证,没有用户的情况,创建首个用户,创建成功,所以接下来的可能的操作就是把我们系统用到的用户再创建一遍
# mongo --port 29017 r4:PRIMARY> use admin switched to db admin r4:PRIMARY> db.createUser({user:"root",pwd:"root",roles:[{role:"root",db:"admin"}]}) Successfully added user: { "user" : "root", "roles" : [ { "role" : "root", "db" : "admin" } ] } r4:PRIMARY> db.auth("root","root") 1 r4:PRIMARY> db.system.users.find() { "_id" : "admin.root", "userId" : BinData(4,"v0RpNlKbRJqNnC6xZru7rA=="), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "viBoAxUMs/tT4pio1MbSxA==", "storedKey" : "XKCOfKkGdM/uo3RPMDaBUf48UHo=", "serverKey" : "go8VloZVuZrPQTz/D2/kx/ry8VQ=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } r4:PRIMARY> show dbs adbox 0.001GB admin 0.000GB fbbox 0.047GB local 0.001GB r4:PRIMARY> db.createUser( { "user" : "admin","pwd": "admin","roles" : [ { role: "dbOwner", db: "adbox" },{ role: "dbOwner", db: "fbbox" },{ role: "root", db: "admin" }] }) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "dbOwner", "db" : "adbox" }, { "role" : "dbOwner", "db" : "fbbox" }, { "role" : "root", "db" : "admin" } ] }还有用户认证问题:https://www.cnblogs.com/operationhome/p/9844268.html
对于上面提到的未知问题,现在找到了问题原因,进行解释一下。 问题中提到,在启用身份认证以后,原本存在的用户消失了,无法查询到,其原因是因为登录的端口不对,我再mongos的3000端口中建的用户,在mongodb的29017中就无法查询到了,这是正常的现象。在3000端口中,原本创建的用户还是存在的。