分类: LINUX
2014-07-10 11:01:37
# mongo # 连接数据库,默认无需密码就能连接
MongoDB shell version: 2.4.10
connecting to: test
> use admin #切换到admin数据库
switched to db admin
> db.addUser('root1','123456') #添加root1用户,密码为123456
{
"user" : "root1",
"readOnly" : false,
"pwd" : "25e1e6f5ead26a08231ef6103254830d",
"_id" : ObjectId("53bdfe776326c2433e6bdb05")
}
> db.auth('root1','123456') #用户名和密码验证,正确返回"1"。
1
> show dbs #查看当前数据库
admin 0.203125GB
local 0.078125GB
[root@node1 ~]# mongo admin --eval "db.addUser('root2','qwe123');"
MongoDB shell version: 2.4.10
connecting to: admin
{
"user" : "root2",
"readOnly" : false,
"pwd" : "5b55d943fac8a8130fe07e24be50b365",
"_id" : ObjectId("53be00017a3cacf0ba1fec75")
}
[root@node1 ~]#
[root@node1 ~]# mongo admin --quiet --eval "db.auth('root2','qwe123');"
1
[root@node1 ~]#
--quiet: 不显示提示信息,如下把这项去掉,会提示一些信息:
[root@node1 ~]# mongo admin --eval "db.auth('root2','qwe123');"
MongoDB shell version: 2.4.10
connecting to: admin
1
[root@node1 ~l]#