下面我们试验一下用户的权限设置是否正确:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/test
- > use foo
- switched to db foo
- > db.system.users.find();
- error: {
- "$err" : "unauthorized db:foo lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- > use admin
- switched to db admin
- > db.system.users.find();
- error: {
- "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- >
|
通知以上实验结果,说明登录时不指定用户名和口令时会报错,也就是说安全性的部署生效了。下面我再看一下另一个场景:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/test
- Sat Apr 21 19:23:15 uncaught exception: login failed
- exception: login failed
|
奇怪了,我们明明指定了用户名而且口令也没有错呀,这时我们看一下系统日志上是否有一些有价值的信息:
auth: couldn't find user root, test.system.users
哦,原来是这样,说明连接mongodb时,如果不指定库名,那么会自动连接到test库,但刚才我们新建的用户,都不是在test库上建立的,所以我们需要显示指定需要连接的库名:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/admin
- > show collections;
- system.indexes
- system.users
- > use foo
- switched to db foo
- > show collections
- system.indexes
- system.users
- t1
- >
|
可以看到root这个用户有所有库的操作权限,那么user1这个用户有什么权限呢?我们一试便知:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: 127.0.0.1:27888/foo
- > show collections;
- system.indexes
- system.users
- t1
- > use test
- switched to db test
- > show collections
- Sat Apr 21 19:28:25 uncaught exception: error: {
- "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- >
|
通过结果我们看到,由于user1是在foo库里建立的用户,所以它不具有操作其它数据库,甚至是test库的权限。
相关链接:
教你如何利用MySQL学习MongoDB之安装篇
教你如何利用MySQL学习MongoDB之数据存储结构
教你如何利用MySQL学习MongoDB之SQL语法