MongoDB 3.4.7 用戶基本操做

MongoDB 默認不須要用戶名密碼,但現實狀況仍是須要用戶密碼驗證。本篇介紹基本的用戶操做。linux

官網有很詳細的例子 https://docs.mongodb.com/manual/reference/method/js-user-management/mongodb

下面是個人一部分總結。shell

 

MongoDB 默認以以下配置啓動數據庫

dbpath=/usr/local/mongodb-linux-x86_64-rhel70-3.4.7/db
logpath=/usr/local/mongodb-linux-x86_64-rhel70-3.4.7/mongodb.log
port=27017
fork=true
logappend=true

 

先建立一個管理員帳號bash

命令行登錄 mongodb, 在bin目錄下執行 ./mongoapp

[root@localhost bin]# ./mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Server has startup warnings: 
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] 
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] 
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] 
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] 
2017-08-14T18:14:19.248+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-08-14T18:14:19.249+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-08-14T18:14:19.249+0800 I CONTROL  [initandlisten] 
>

在 admin 庫下面建立超級用戶,超級用戶用於管理其餘用戶的權限ui

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

用戶名爲 admin 密碼爲 admin 角色爲 userAdminAnyDatabasethis

關於角色 官網有詳細說明 https://docs.mongodb.com/manual/reference/built-in-roles/#built-in-rolesspa

 

建立普通用戶prototype

use demo
db.createUser({user:"demo_user", pwd:"123456", roles:[{role:"readWrite", db:"demo"}]});

在 demo 庫下面建立了一個 demo_user 密碼爲 123456 有讀寫權限

查看庫下面全部用戶

use demo
db.getUsers()

使用admin用戶修改普通用戶密碼

use admin
db.changeUserPassword("demo_user", "SOh3TbYhx8ypJPxmt1oOfL")

刪除庫下面用戶

use demo
db.dropUser("demo_user");

刪除庫下面全部用戶

use demo
db.dropAllUsers()

獲取角色信息

use demo
db.getUser("demo_user")

 

開啓權限需設置 auth=true 到 mongodb 啓動配置文件

dbpath=/usr/local/mongodb-linux-x86_64-rhel70-3.4.7/db
logpath=/usr/local/mongodb-linux-x86_64-rhel70-3.4.7/mongodb.log
port=27017
fork=true
logappend=true
auth=true

重啓mongodb

[root@localhost bin]# ps aux|grep mongod
root      8203  0.7  4.9 980176 50268 ?        SLl  18:14   0:03 ./mongod --config mongodb.conf
root      8242  0.0  0.0 112648   956 pts/0    R+   18:23   0:00 grep --color=auto mongod
[root@localhost bin]# kill -HUP 8203
[root@localhost bin]# ./mongod --config mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 8245
child process started successfully, parent exiting
[root@localhost bin]#

如今權限已生效

 

經過命令行登錄能夠發現,執行命令提示沒有權限

[root@localhost bin]# ./mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
> use demo
switched to db demo
> show collections
2017-08-14T18:25:24.277+0800 E QUERY    [thread1] Error: listCollections failed: {
	"ok" : 0,
	"errmsg" : "not authorized on demo 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:762:9
shellHelper@src/mongo/shell/utils.js:659:15
@(shellhelp2):1:1
>

經過受權以後能夠執行 show collections 命令能夠獲取結果

> db.auth("demo_user", "123456")
1
> show collections
restaurants
>

經過下面的方式能夠直接登錄數據庫而且已得到權限

[root@localhost bin]# ./mongo demo -u demo_user -p
MongoDB shell version v3.4.7
Enter password: 
connecting to: mongodb://127.0.0.1:27017/demo
MongoDB server version: 3.4.7
> show collections
restaurants
>
相關文章
相關標籤/搜索