mongodb创建用户操作

[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: imooc

db.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值