[root@iZ28dbijkuzZ ~]# /usr/local/mongodb/bin/mongo
MongoDB shell version: 2.2.6
connecting to: test
use admin#mongodb的用户数据库
switched to db admin
show table
Sun Aug 30 13:58:49 uncaught exception: don’t know how to show [table]
show tables;
db.addUser(‘sa’,’passwd’,false);
{
“user” : “sa”,
“readOnly” : false,
“pwd” : “42d22e352749e6cd51f06037671bda3d”,
“_id” : ObjectId(“55e29c367974e18b8f4f9326”)
}
exit
[root@iZ28dbijkuzZ bin]# pkill -9 mongo
[root@iZ28dbijkuzZ bin]# /usr/local/mongodb/bin/mongod –dbpath=/usr/local/mongodb/data –logpath=/usr/local/mongodb/logs –port=27017 –fork –auth (加auth需要用户名与密码登录)
forked process: 24833
all output going to: /usr/local/mongodb/logs –port=27017
log file [/usr/local/mongodb/logs –port=27017] exists; copied to temporary file [/usr/local/mongodb/logs –port=27017.2015-08-30T06-18-34]
child process started successfully, parent exiting
[root@iZ28dbijkuzZ bin]# ./mongo
MongoDB shell version: 2.2.6
connecting to: test
show dbs;#没有认证用户不能使用
Sun Aug 30 14:19:18 uncaught exception: listDatabases failed:{ “errmsg” : “need to login”, “ok” : 0 }
use admin
switched to db admin
db.auth(‘sa’,’passwd’)#认证用户
1#认证成功,0为失败
show tables;
system.indexes
system.users
show dbs;
admin 0.203125GB
imooc 0.203125GB
imooc_2 0.203125GB
local (empty)use imooc
switched to db imooc
db.addUser(‘imooc_test’,’imooc_passwd’,false)#只对imooc表有用
{
“user” : “imooc_test”,
“readOnly” : false,
“pwd” : “c6e2cd4c8a2b45e2e4a20a8bd3f54224”,
“_id” : ObjectId(“55e2a10efc42e24491138b81”)
}
exit;
[root@iZ28dbijkuzZ bin]# ./mongo -uimooc_test -pimooc_passwd
MongoDB shell version: 2.2.6
connecting to: test
Error: { errmsg: “auth fails”, ok: 0.0 }
Sun Aug 30 14:22:35 uncaught exception: login failed
exception: login failed
[root@iZ28dbijkuzZ bin]# ./mongo imooc -uimooc_test -pimooc_passwd
MongoDB shell version: 2.2.6
connecting to: imoocdb.removeUser(‘sa’)#删除用户
db.addUser(‘root’,’123456’,false)#超级权限应为在admin数据库下
{
“user” : “root”,
“readOnly” : false,
“pwd” : “34e5772aa66b703a319641d42a47d696”,
“_id” : ObjectId(“55e2a1def1c99756226b4a4a”)
}
exit
bye
[root@iZ28dbijkuzZ bin]# ./mongo
MongoDB shell version: 2.2.6
connecting to: test
use admin
switched to db admin
db.auth(‘sa’,’passwd’)
Error: { errmsg: “auth fails”, ok: 0.0 }
0
db.auth(‘root’,’123456’)
1
db.imooc.changeUserPassword(‘imooc_test’,’12345’)#修改密码但是在我这里无效可能版本问题
Sun Aug 30 14:36:34 TypeError: db.imooc.changeUserPassword is not a function (shell):1
db.changeUserPassword(‘imooc_test’,’12345’)
Sun Aug 30 14:37:43 TypeError: db.changeUserPassword is not a function (shell):1
db.system.users.find();#不能查看超级用户
{ “_id” : ObjectId(“55e2a10efc42e24491138b81”), “user” : “imooc_test”, “readOnly” : false, “pwd” : “c6e2cd4c8a2b45e2e4a20a8bd3f54224” }
db.addUser(‘imooc_test’,’12345’)#当用户名重复就是修改用户密码
{
“_id” : ObjectId(“55e2a10efc42e24491138b81”),
“user” : “imooc_test”,
“readOnly” : false,
“pwd” : “504eb9d7135bc294d1a9e6affd17d145”
}
db.system.users.find();#查看所有用户
{ “_id” : ObjectId(“55e2a10efc42e24491138b81”), “user” : “imooc_test”, “readOnly” : false, “pwd” : “504eb9d7135bc294d1a9e6affd17d145” }db.auth(‘imooc_test’,’imooc_passwd’)
Error: { errmsg: “auth fails”, ok: 0.0 }
0
db.auth(‘imooc_test’,’12345’)#密码已更改
1