mongodb简述

1、下载安装mongodb

1 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz     # 下载
2 tar -zxvf mongodb-linux-x86_64-3.0.6.tgz                                  # 解压
3 mv  mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb-3.0.6                     # 将解压包拷贝到指定目录
4 ln -s /usr/local/mongodb-3.0.6/ /usr/local/mongodb                # 创建软链接5 export PATH=/usr/local/mongodb:/bin:$PATH    # 添加PATH变量6 mkdir -p /data/mongo_db                 #创建数据库目录(启动指定--dbpath,或写于配置文件)

2、配置文件

[root@vm-centos7 bin]# mkdir /usr/local/mongodb/etc/

[root@vm-centos7 bin]# cat /usr/local/mongodb/etc/mongod.conf
#数据库数据存放目录
dbpath=/data/mongo_db/

#数据库日志存放目录
logpath=/var/log/mongodb/mongo.log

#以追加的方式记录日志
logappend = true

#端口号 默认为27017
port=27017

#以后台方式运行进程
fork=true

#开启用户认证
auth=true

#关闭http接口,默认关闭http端口访问
nohttpinterface=false

#mongodb所绑定的ip地址
#bind_ip = 127.0.0.1
bind_ip = 0.0.0.0

#启用日志文件,默认启用
journal=true

#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true

3、启动mongdb

mongod -f /usr/local/mongodb/etc/mongod.conf

4、连接并创建管理员

[root@vm-centos7 bin]# mongo
MongoDB shell version: 3.0.6
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
>
>
>
> use admin
switched to db admin
> db
admin
> db.createUser({user:'linxu',pwd:'123456',roles:[{role:'userAdminAnyDatabase',db:'admin'}]})
Successfully added user: {
    "user" : "linxu",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
>
> exit
bye
# 命令行测试
mongo -ulinxu -p '123456' 127.0.0.1:27017/admin
# mongo内部测试
[root@vm-centos7 bin]# mongo MongoDB shell version: 3.0.6 connecting to: test > > > db test > > show dbs 2017-05-20T00:20:02.610+0800 E QUERY Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }", "code" : 13 } at Error (<anonymous>) at Mongo.getDBs (src/mongo/shell/mongo.js:47:15) at shellHelper.show (src/mongo/shell/utils.js:630:33) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47 > > use admin switched to db admin > > db admin > > show dbs 2017-05-20T00:20:13.923+0800 E QUERY Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }", "code" : 13 } at Error (<anonymous>) at Mongo.getDBs (src/mongo/shell/mongo.js:47:15) at shellHelper.show (src/mongo/shell/utils.js:630:33) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47 > > db admin > > db.auth('linxu','123456') 1 > > show dbs admin 0.078GB local 0.078GB

5、创建普通用户

[root@vm-centos7 bin]# mongo -ulinxu -p '123456' 127.0.0.1:27017/admin
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27017/admin
>
>
> db
admin
>
> show dbs
admin  0.078GB
local  0.078GB
>
> use mydb
switched to db mydb
>
> db
mydb
>
> db.createUser({user:'nick',pwd:'123456',roles:[{role:'readWrite',db:'mydb'}]})
Successfully added user: {
    "user" : "nick",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "mydb"
        }
    ]
}
>
> db.auth('nick','123456')
1
>

6、查看所有存在的用户

> use admin
switched to db admin
>
> db
admin
>
> db.auth('linxu','123456')
1
>
> db.system.users.find()
{ "_id" : "admin.linxu", "user" : "linxu", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "G1odVR9h/KMPQrOm/OgoXA==", "storedKey" : "IxV+sHbetC0D6y6o8nu+QuI81MQ=", "serverKey" : "07e+MxR1ZEM5BwsfKKLa5S6bIc4=" } }, "roles" : [{ "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "mydb.nick", "user" : "nick", "db" : "mydb", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "q4/PoG2mrvBNc0WV7HIuyA==", "storedKey" : "TDKzZyJasK2D0xlRwlmewl87NQA=", "serverKey" : "lphcSnGgYUHMgBQ2aY+H8U7DAIk=" } }, "roles" : [ { "role" : "readWrite", "db" : "mydb" } ] }
>
>

 

posted @ 2017-05-19 17:18  xu.lin  阅读(152)  评论(0编辑  收藏  举报