ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 数据库及用户的创建

数据库及用户的创建

原创 Linux操作系统 作者:安佰胜 时间:2012-09-04 15:14:37 0 删除 编辑

--创建数据库
--客户端连接monggodb
[root@localhost ~]# mongo
MongoDB shell version: 2.0.7
connecting to: test
--查看当前下的数据库
> show dbs;
local   0.0625GB
test    (empty)
--创建新的数据库只需要直接use就可以
--如果没有进行其他操作,如文档的创建等,当退出用户后数据库会自动删除
> use an
switched to db an
> show dbs;
local   0.0625GB
test    (empty)
--插入数据后数据库被保留
> db.an.people.insert({"name":"anbaisheng"})
> show dbs;
an      0.0625GB
local   0.0625GB
test    (empty)
--切换后仍然保留
> use test
switched to db test
> show dbs;
an      0.0625GB
local   0.0625GB
test    (empty)
--删除数据库
> use an;
switched to db an
> db.dropDatabase()
{ "dropped" : "an", "ok" : 1 }
--------------------------------------------------
--创建用户
--语法
--通过设定readOnly可以创建只读用户
db.addUser(username, password[, readOnly=false])
> db.addUser('an','anbaisheng')
{ "n" : 0, "connectionId" : 5, "err" : null, "ok" : 1 }
{
        "user" : "an",
        "readOnly" : false,
        "pwd" : "662a5774366a242238cb76435f547ae1",
        "_id" : ObjectId("5045a38483b8ed58a458bdcd")
}
--创建用户后将产生collection system.users
--通过查询这个collection也能够获得用户信息
> show collections;
an.people
system.indexes
system.users
> db.system.users.find()
{ "_id" : ObjectId("5045a38483b8ed58a458bdcd"), "user" : "an",
"readOnly" : false, "pwd" : "662a5774366a242238cb76435f547ae1" }
>
--验证用户名密码
--使用db.auth,输入用户名密码,匹配返回1,不匹配返回0
> db.auth('an','anbaisheng')
1
> db.auth('an','an')
0
--没有权限的数据库下也返回0
> show dbs;
admin   (empty)
an      0.0625GB
local   0.0625GB
test    (empty)
> use test
switched to db test
> db.auth('an','an')
0
> db.auth('an','anbaisheng')
0
--更改已有用户密码
> db.addUser('an','an')
{
        "updatedExisting" : true,
        "n" : 1,
        "connectionId" : 5,
        "err" : null,
        "ok" : 1
}
{
        "_id" : ObjectId("5045a38483b8ed58a458bdcd"),
        "user" : "an",
        "readOnly" : false,
        "pwd" : "1ccf8f142ad0600d909bae2404813fdc"
--删除已有用户
> db.system.users.remove({"user":"an"})
> db.system.users.find()
-----------------------------------------------------------------

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13177610/viewspace-742638/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: linux上安装mongodb
请登录后发表评论 登录
全部评论

注册时间:2009-08-26

  • 博文量
    215
  • 访问量
    660342