搜尋此網誌

2017年1月5日 星期四

MongoDB 3.x 啟用連線認證

先參考
https://scalegrid.io/blog/configuring-mongodb-cr-authentication-as-default-on-mongodb-3-x/

以及這篇 另外 Bugj問題這篇(MongoDB - admin user not authorized)


先新增管理員

切換到admin資料庫
use admin

新增使用者
db.createUser(
   {
     user: "superuser",
     pwd: "xxxxxxxxxx",
     roles: [ { role: "root", db: "admin" } ]
   }
)

新增唯獨權限的使用者 將來管理方便
use admin
db.createUser(
   {
     user: "reader",
     pwd: "reader1234567",
     roles: [ { role: "readAnyDatabase", db: "admin" } ]
   }
)

啟用 MongoDB 的帳密連線
vi /etc/mongod.conf

security:
  authorization: enabled

然後重啟
systemctl restart mongod 


還沒結束!!!
因為Mongodb 啟用連線認證之後,所以備份的資料結構都改變了,
如果另一台MongoDB不調整認證方式,會導致資料恢復失敗,所以:

連線進去
mongo --authenticationDatabase=admin -u superuser -p xxxxxxxxxx 

執行以下指令:
db.adminCommand({authSchemaUpgrade: 1});

確認出現 OK 的提示之後,日後進行 Data Restore 才不會出現以下
the users and roles collections in the dump have an incompatible auth version with target server: cannot restore users of auth version 5 to a server of auth version 3

官方文件參考

- The End -

沒有留言:

張貼留言

搜尋此網誌