mongoDB 管理

来源:互联网 发布:美工的岗位职责 编辑:程序博客网 时间:2024/04/30 04:11

配置文件(外部配文件置):

mongod --config D:\mongodb\mongodb.config

 mongodb.config文件内容:

 port = 20000
dbpath = D:/mongodb/dbs 
logpath = D:/mongodb/logs.log 
logappend = true

正确停止MongoDB:

mongo -port 20000
use admin
db.shutdownServer();

安全认证:

  1.添加用户

   db.addUser(username, password,[ readOnly=false])

    [ readOnly=false]参数可选,如果为true,用户只有查询权限。

   admin数据库下面添加的用户  被视为超级用户(管理员)。

2.用户认证
 db.auth(username, password)

3.重启服务端,加入 --auth命令选项  开启安全检查

mongod --dbpath D:/mongodb/db  --port 20000 --logpath = D:/mongodb/logs.log --logappend --auth

 例子:

1. 添加一个超级用户:

a.添加用户

>mongo -port 20000
> use admin
switched to db admin
> db.addUser("admin","admin123");
{ "n" : 0, "connectionId" : 4, "err" : null, "ok" : 1 }
{
        "user" : "admin",
        "readOnly" : false,
        "pwd" : "6f09f2f35e3a5e225af66fe3868e4fb7",
        "_id" : ObjectId("50114eb027e4ae802c6bda45")
}
>

b.认证用户

> use admin
switched to db admin
> db.auth("admin","admin123");
1

c.重启服务端 加入--auth参数

登录后不能正常查询

>mongo --port 20000 test
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:20000/test
> db.test.find();
error: {
        "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
        "code" : 10057
}
>

d.使用用户名 密码登录

>mongo --port 20000 admin  -u admin -p
MongoDB shell version: 2.0.4
Enter password:
connecting to: 127.0.0.1:20000/admin
> use test
switched to db test
> db.test.find();
>

2.添加一个普通用户

> use test
switched to db test
> db.addUser("abc","abc123");
{ "n" : 0, "connectionId" : 9, "err" : null, "ok" : 1 }
{
        "user" : "abc",
        "readOnly" : false,
        "pwd" : "30fc2d5cc077f71cc81d3c82afc9d361",
        "_id" : ObjectId("501157ed1d2319c203e2e8d3")

认证:

> db.auth("abc","abc123");
1

用户名 密码登录

mongo --port 20000 test  -u abc -p
MongoDB shell version: 2.0.4
Enter password:
connecting to: 127.0.0.1:20000/test
> db.test.find();

查询用户

 db.system.users.find()

删除用户:

db.system.users.remove({user:"admin"});

限制ip访问:

使用--bindip 参数, 只能被--bindip参数指定的ip地址访问.

如:mongod --bindip localhost

数据文件备份:

>mongodump -port 20000 -d test -u admin -p-d test

备份的数据库名称会在当前目录下创建一个dump目录,用于存放备份出来的文件,当然也可以(-o)指定备份存放的目录。

mongodump -port 20000 -d test -o c:/ -u admin -p

mongodump --help 查看其他选项

数据恢复:

>mongorestore -port 20000 -d test --directoryperdb c:/test/  --drop -u admin -p

--directoryperdb指定备份的目录

--drop 恢复的时候先删除

例:

a,先删除test数据库

> use test;
switched to db test
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
> show dbs;
admin   0.03125GB
local   (empty)

b.恢复

mongorestore -port 20000 -d test --directoryperdb c:/test/  --drop -u admin -p

监控:

1.serverStatus命令

use admin
db.serverStatus();

db.serverStatus();
{
        "host" : "admin:20000",
        "version" : "2.0.4",
        "process" : "mongod",
        "uptime" : 16,
        "uptimeEstimate" : 14,
        "localTime" : ISODate("2012-07-26T13:26:38.375Z"),
        "globalLock" : {
                "totalTime" : 15421875,
                "lockTime" : 0,
                "ratio" : 0,
                "currentQueue" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                },
                "activeClients" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                }
        },
        "mem" : {
                "bits" : 32,
                "resident" : 15,
                "virtual" : 57,
                "supported" : true,
                "mapped" : 0
        },
        "connections" : {
                "current" : 1,
                "available" : 19999
        },
        "extra_info" : {
                "note" : "fields vary by platform",
                "page_faults" : 4032,
                "usagePageFileMB" : 19,
                "totalPageFileMB" : 3871,
                "availPageFileMB" : 2878,
                "ramMB" : 1977
        },
        "indexCounters" : {
                "note" : "not supported on this platform"
        },
        "backgroundFlushing" : {
                "flushes" : 0,
                "total_ms" : 0,
                "average_ms" : 0,
                "last_ms" : 0,
                "last_finished" : ISODate("1970-01-01T00:00:00Z")
        },
        "cursors" : {
                "totalOpen" : 0,
                "clientCursors_size" : 0,
                "timedOut" : 0
        },
        "network" : {
                "bytesIn" : 220,
                "bytesOut" : 261,
                "numRequests" : 3
        },
        "opcounters" : {
                "insert" : 0,
                "query" : 1,
                "update" : 0,
                "delete" : 0,
                "getmore" : 0,
                "command" : 4
        },
        "asserts" : {
                "regular" : 0,
                "warning" : 0,
                "msg" : 0,
                "user" : 0,
                "rollovers" : 0
        },
        "writeBacksQueued" : false,
        "ok" : 1

返回字段说明:  

uptime: 服务器运行时间(秒)。

localTime: 服务器本地时间。

mem: 服务器内存隐身了多少数据,服务器进程的虚拟内存和常驻内存占用情况(单位:MB)。

connections: 当前连接数。

opcounters: 操作统计。

globalLock:表示全局写入锁占用的服务器时间(单位:微妙) ;

backgroundFlushing: 表示后台做了多少次fsync以及用了多少时间。

asserts:统计断言的次数。

2.mongostat.exe

    bin目录下。 

原创粉丝点击