linux上安裝MongoDB副本集(帶keyfile安全認證以及用戶權限)

搭建前準備html

MongoDB版本:4.0linux

主要參考搭建MongoDB副本集網站:https://www.jianshu.com/p/f021f1f3c60bmongodb

安裝以前最好先肯定一下幾點:shell

    • 防火牆關閉
    • MongoDB的端口號對須要訪問你的服務器開放

相關linux命令可參考博客http://www.javashuo.com/article/p-uzdudndl-s.html數據庫

 

在安裝的過程當中,能夠說遇到問題多多,下面就介紹一下問題狀況,以及解決方法:服務器

問題一(鏈接被拒絕):網站

{
    "operationTime" : Timestamp(0, 0),
    "ok" : 0,
    "errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 10.xx.xx.xx:27018 failed with Error connecting to 10.xx.xx.xx:27018 :: caused by :: Connection refused, 10.xx.xx.xx:27018 failed with Error connecting to 10.xx.xx.xx:27018 :: caused by :: Connection refused",
    "code" : 74,
    "codeName" : "NodeNotFound",
    "$clusterTime" : {
        "clusterTime" : Timestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

緣由:spa

沒有指定本機IP,默認是127.0.0.1;.net

解決方法:prototype

在mongo.conf配置文件中,加上bind_ip=本機IP便可;

 

問題二(文件權限過大):

permissions on /root/mongodb/keyfile are too open

緣由:

我指定的keyFile=/root/mongodb/keyfile/keyfile路徑少寫了文件「keyfile」,僅指定到文件夾「keyfile」,因此無論我怎麼減小權限,都不行。

 解決方法:

補全keyfile文件的路徑

keyFile=/root/mongodb/keyfile/keyfile

 

 

問題三(查當作員狀態失敗):

replSet:PRIMARY> rs.status()
{
    "operationTime" : Timestamp(1553750837, 1),
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0, lsid: { id: UUID(\"49593e29-481a-4765-a7ad-2b1ca5a8fbcf\") }, $clusterTime: { clusterTime: Timestamp(1553750837, 1), signature: { hash: BinData(0, BAD749F5F3799A6EDEB97259390BD616FF6A9F5D), keyId: 6673011308209635329 } }, $db: \"admin\" }",
    "code" : 13,
    "codeName" : "Unauthorized",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1553750837, 1),
        "signature" : {
            "hash" : BinData(0,"utdJ9fN5mm7euXJZOQvWFv9qn10="),
            "keyId" : NumberLong("6673011308209635329")
        }
    }
}

緣由:

因爲開啓了權限驗證,即「auth=true」,因此查看狀態以前,須要先認證。

解決方法:

replSet:PRIMARY> use admin
switched to db admin
replSet:PRIMARY> db.auth('admin','admin')
1
replSet:PRIMARY> rs.status()

 

 

問題四(插入數據失敗):

replSet:PRIMARY> db.admin.insert({"name":"zhangsan"})
WriteCommandError({
    "operationTime" : Timestamp(1553752333, 1),
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { insert: \"admin\", ordered: true, lsid: { id: UUID(\"fa7499f8-f4be-4059-a713-0a2dd2ddbcc7\") }, $clusterTime: { clusterTime: Timestamp(1553752333, 1), signature: { hash: BinData(0, 77558A08F8FF8235DF3CD87E13C2771943EBBDE0), keyId: 6673011308209635329 } }, $db: \"admin\" }",
    "code" : 13,
    "codeName" : "Unauthorized",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1553752333, 1),
        "signature" : {
            "hash" : BinData(0,"d1WKCPj/gjXfPNh+E8J3GUPrveA="),
            "keyId" : NumberLong("6673011308209635329")
        }
    }
})

緣由:

沒有按照如下原則進行:

  • 數據庫和用戶是綁定的,光建立一個超級用戶並不能操做在其餘新建的數據庫中插入數據
  • 在切換數據庫時,先切換認證用戶,否則會出現too many users are authenticated的錯誤。

解決方法:

  1. 輸入use admin,進入admin數據庫,root用戶須要在admin數據庫中認證。
  2. 輸入db.auth('root','root')超級用戶進行認證
  3. 輸入db.createUser({user: "okevin",pwd: "123456",roles: [ { role: "readWrite", db: "recommended" } ]} )建立okevin用戶,併爲它指定數據庫爲recommended。
  4. 輸入db.auth('okevin','123456')切換認證用戶。
  5. 輸入use recommended切換至recommended數據庫
  6. 輸入db.repo.insert({"name":"kevin"}),建立一條數據。

主要參考網址:http://www.cnblogs.com/yulinfeng/p/10226977.html

 

問題五(建立用戶失敗):

replSet:PRIMARY> db.createUser({user:"test", pwd:"test", roles:[{role: "userAdminAnyDatabase", db:"test" }]})
2019-03-28T13:56:08.968+0800 E QUERY    [js] Error: couldn't add user: No role named userAdminAnyDatabase@test :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1491:15

緣由:

在db.createUser()方法中roles裏面的db必須寫成是admin庫。

解決方法:

db的值改成admin

db.createUser({user:"test", pwd:"test", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})

可參考網址:https://blog.csdn.net/jianlong727/article/details/53889990

 

問題六(從節點讀取主節點數據失敗):

019-03-28T14:43:13.643+0800 E QUERY    [js] Error: not master and slaveOk=false :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1763:1
shellHelper.show@src/mongo/shell/utils.js:859:9
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1

緣由:

這是正常的,默認狀況下,主節點有讀寫權限,從節點是沒有讀寫權限的。

解決方法:

經過命令進行設置

rs.slaveOk()

讀寫權限設置可參考網址:

https://blog.csdn.net/u011191463/article/details/68485529

 

配置完成後,Java鏈接池配置,可參考:

http://www.javashuo.com/article/p-ukqbuycx-bd.html

相關文章
相關標籤/搜索