Ubuntu mongodb 安裝和配置- 常用命令

這個文檔挺好的https://blog.csdn.net/u010523770/article/details/54599548

先添加超級用戶:

use admin
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
);
exit; 


mongodb 3添加新用戶:

db.createUser( { "user" : "test1",


                 "pwd": "test1",
                 "customData" : { employeeId: 12345 },
                 "roles" : [ { role: "clusterAdmin", db: "admin" },
                             { role: "readAnyDatabase", db: "admin" },
                             "readWrite"
                             ] },

               { w: "majority" , wtimeout: 5000 } )



轉載來源:http://www.cnblogs.com/zj1111184556/p/3599828.html


安裝 MongoDB

  sudo apt-get install mongodb

  sudo apt-get install mongodb

用戶操作(2.4.9上面測試的):

mongo

user  admin;

db.auth('', '');

db['system.users'].find({}, {user:1, pwd:1, _id: 0});
刪除一個用戶:

db.removeUser('root2')

修改一個用戶的密碼:

db.changeUserPassword("accountUser", "SOh3TbYhx8ypJPxmt1oOfL")

關閉/啓動

  sudo service mongodb stop
  sudo service mongodb start

 

設置數據庫連接密碼:

  在跟目錄創建文件夾: data/db

  關閉現有服務。

    sudo service mongodb stop

  重新啓動服務

    $ mongod –auth

  創建連接用戶

    $ mongo

    > use admin

    switched to db admin

    > db.addUser("root","1983")

  關閉服務(直接在 mongod 啓動的命令窗口 “ctrl + C”

  重啓服務:

    $: mongod –auth

  查看是否開始驗證:、

    $ mongo

    MongoDB shell version: 2.0.4

    connecting to: test

    > use admin

    switched to db admin

    > show collections

    Fri Mar 14 09:07:08 uncaught exception: error: {

    "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",

    "code" : 10057

    }

  有提示 鏈接錯誤。

  進行用戶驗證:

    > db.auth("root","1983")

    1

  重新查看就可以查看數據集

    > show collections

    system.indexes

    system.users


設置客戶端連接:

  默認安裝的話只允許 127.0.0.1 IP 連接.

  需要修改/etc/mongodb.conf 註釋下列記錄:

  打開文件:

    $ sudo gedit /etc/mongodb.conf

  註釋記錄:

    #bind_ip = 0.0.0.0 



mongodb 2.6以後的版本添加用戶方法,每個庫需要授權才能訪問:

https://docs.mongodb.org/manual/reference/method/db.createUser/

use products
db.createUser(
   {
     user: "accountUser",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]
   }
)

在centos6.5上面安裝後建立 用戶後連接報錯,查看日誌mongodb.log:
AuthenticationFailed: MONGODB-CR credentials missing in the user document

非常感覺此哥vivek(http://stackoverflow.com/users/2512718/vivek)的回答:

http://stackoverflow.com/questions/29006887/mongodb-cr-authentication-failed


go to mongoDB console and delete your current user & set authSchema version to 3 instead of 5 , follow these commands in mongo console -


mongo
use admin
db.system.users.remove({})    <== removing all users
db.system.version.remove({}) <== removing current version 
db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 })
Now restart the mongod and create new user then it should work fine.


Note: use remove commands in test db only, if in production use update.



重啓mongodb後再用下面的命令新建用戶,就正常:


db.createUser(
  {
    user: "username",
    pwd: "passwd",
    roles: [ "readWrite", "dbAdmin" ]
  }
)

=========================

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



admin庫:


db.grantRolesToUser(
  "admin",
  [ 
    {role: "readWrite", db: "admin"} ,
    {role: "readAnyDatabase", db: "admin"} ,
   ]
)

普通庫

db.grantRolesToUser(
  "admin",
  [ 
    {role: "readWrite", db: "test"} ,
    {role: "readWrite", db: "files"} ,
   ]
)

設置自動啓動:


update-rc.d mongodb defaults



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章