mongosniff
mongosniff提供了对数据库实时活动的低级别操作跟踪和嗅探视图。可以将mongosniff认为是专为MongoDB定制的,类似于tcpdump用于TCP/IP网络流量分析。mongosniff常用于驱动开发。 注意:mongosniff需要libpcap,并且只对类Unix系统可用。 相对于mongosniff,Wireshark,一个流行的网络嗅探工具,可用于侦测和解析MongoDB线协议。
语法
下面的命令连接到运行在localhost的27017和27018上的mongod或mongos:
mongosniff --source NET lo 27017 27018
下面的命令只记录运行在localhost的27018上的mongod或mongos的无效的BSON对象,用于驱动开发和问题跟踪:
mongosniff --objcheck --source NET lo 27018
mongodump
在Mongodb中我们使用mongodump命令来备份MongoDB数据。该命令可以导出所有数据到指定目录中。
mongodump命令可以通过参数指定导出的数据量级转存的服务器。
语法
mongodump命令脚本语法如下:
>mongodump -h dbhost -d dbname -o dbdirectory
- h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
- d:需要备份的数据库实例,例如:test
- o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。
实例
在本地使用 27017 启动你的mongod服务。打开命令提示符窗口,进入MongoDB安装目录的bin目录输入命令mongodump:
>mongodump
执行以上命令后,客户端会连接到ip为 127.0.0.1 端口号为 27017 的MongoDB服务上,并备份所有数据到 bin/dump/ 目录中。命令输出结果如下:
mongodump 命令可选参数列表如下所示:
| 语法 | 描述 | 实例 |
|---|---|---|
| mongodump --host HOST_NAME --port PORT_NUMBER | 该命令将备份所有MongoDB数据 | mongodump --host w3cschool.cc --port 27017 |
| mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY | mongodump --dbpath /data/db/ --out /data/backup/ | |
| mongodump --collection COLLECTION --db DB_NAME | 该命令将备份指定数据库的集合。 | mongodump --collection mycol --db test |
mongorestore
mongodb使用 mongorerstore 命令来恢复备份的数据。
语法
mongorestore命令脚本语法如下:
>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
- h:MongoDB所在服务器地址
- d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
- --directoryperdb:备份数据所在位置,例如:c:\data\dump\test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧!
- --drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦! 接下来我们执行以下命令:
>mongorestore
执行以上命令输出结果如下:
mongoimport
Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。具体使用如下所示:
[root@localhost mongodb]# ./bin/mongoimport --help
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f name,age
--fieldFile arg file with fields names - 1 per line
--ignoreBlanks if given, empty fields in csv and tsv will be ignored
--type arg type of file to import. default: json (json,csv,tsv)
--file arg file to import from; if not specified stdin is used
--drop drop collection first
--headerline CSV,TSV only - use first line as headers
--upsert insert or update objects that already exist
--upsertFields arg comma-separated fields for the query part of the
upsert. You should make sure this is indexed
--stopOnError stop importing at first error rather than continuing
--jsonArray load a json array, not one item per line. Currently
limited to 4MB.
参数说明:
- h:指明数据库宿主机的IP
- u:指明数据库的用户名
- p:指明数据库的密码
- d:指明数据库的名字
- c:指明collection的名字
- f:指明要导入那些列
示例:
先删除students中的数据,并验证
> db.students.remove()
> db.students.find()
然后再导入上面导出的students.dat文件中的内容
[root@localhost mongodb]# ./bin/mongoimport -d test -c students students.dat
connected to: 127.0.0.1
imported 9 objects
[root@localhost mongodb]#
参数说明:
- d:指明数据库名,本例中为test
- c:指明collection名,本例中为students
- students.dat:导入的文件名
- 查询students集合中的数据
> db.students.find()
{ "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
>
证明数据导入成功
上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式,具体如下所示:
先删除数据
> db.students.remove()
> db.students.find()
再导入之前导出的students_csv.dat文件
[root@localhost mongodb]# ./bin/mongoimport -d test -c students --type csv --headerline --file students_csv.dat
connected to: 127.0.0.1
imported 10 objects
[root@localhost mongodb]#
参数说明:
- type:指明要导入的文件格式
- headerline:指明第一行是列名,不需要导入
- file:指明要导入的文件 查询students集合,验证导入是否成功:
> db.students.find()
{ "_id" : ObjectId("503266029355c632cd118ad8"), "classid" : 1, "name" : "kobe", "age" : 20 }
{ "_id" : ObjectId("503266029355c632cd118ad9"), "classid" : 1, "name" : "nash", "age" : 23 }
{ "_id" : ObjectId("503266029355c632cd118ada"), "classid" : 2, "name" : "james", "age" : 18 }
{ "_id" : ObjectId("503266029355c632cd118adb"), "classid" : 2, "name" : "wade", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adc"), "classid" : 2, "name" : "bosh", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118add"), "classid" : 2, "name" : "allen", "age" : 25 }
{ "_id" : ObjectId("503266029355c632cd118ade"), "classid" : 1, "name" : "howard", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adf"), "classid" : 1, "name" : "paul", "age" : 22 }
{ "_id" : ObjectId("503266029355c632cd118ae0"), "classid" : 2, "name" : "shane", "age" : 24 }
>
说明已经导入成功。
mongoexport
Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体用法如下所示:
[root@localhost mongodb]# ./bin/mongoexport --help
Export MongoDB data to CSV, TSV or JSON files.
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for
sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f
name,age
--fieldFile arg file with fields names - 1 per line
-q [ --query ] arg query filter, as a JSON string
--csv export to csv instead of json
-o [ --out ] arg output file; if not specified, stdout is used
--jsonArray output to a json array rather than one object per
line
-k [ --slaveOk ] arg (=1) use secondaries for export if available, default
true
参数说明:
- h:指明数据库宿主机的IP
- u:指明数据库的用户名
- p:指明数据库的密码
- d:指明数据库的名字
- c:指明collection的名字
- f:指明要导出那些列
- o:指明到要导出的文件名
- q:指明导出数据的过滤条件
实例
test库中存在着一个students集合,集合中数据如下:
db.students.find() { "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" } { "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" } { "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" } { "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" } { "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" } { "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" } { "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" } { "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" } { "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
由上可以看出文档中存在着3个字段:classid、age、name 1.直接导出数据到文件中
[root@localhost mongodb]# ./bin/mongoexport -d test -c students -o students.dat
connected to: 127.0.0.1
exported 9 records
命令执行完后使用ll命令查看,发现目录下生成了一个students.dat的文件 -rw-r--r-- 1 root root 869 Aug 21 00:05 students.dat 查看该文件信息,具体信息如下:
[root@localhost mongodb]# cat students.dat
{ "_id" : { "$oid" : "5031143350f2481577ea81e5" }, "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : { "$oid" : "5031144a50f2481577ea81e6" }, "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : { "$oid" : "5031145a50f2481577ea81e7" }, "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : { "$oid" : "5031146a50f2481577ea81e8" }, "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : { "$oid" : "5031147450f2481577ea81e9" }, "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : { "$oid" : "5031148650f2481577ea81ea" }, "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : { "$oid" : "5031149b50f2481577ea81eb" }, "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : { "$oid" : "503114a750f2481577ea81ec" }, "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : { "$oid" : "503114cd50f2481577ea81ed" }, "classid" : 2, "age" : 24, "name" : "shane" }
参数说明:
- d:指明使用的库,本例中为test
- c:指明要导出的集合,本例中为students
- o:指明要导出的文件名,本例中为students.dat 从上面的结果可以看出,我们在导出数据时没有显示指定导出样式 ,默认导出了JSON格式的数据。如果我们需要导出CSV格式的数据,则需要使用--csv参数,具体如下所示:
[root@localhost mongodb]# ./bin/mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat
connected to: 127.0.0.1
exported 9 records
[root@localhost mongodb]# cat students_csv.dat
classid,name,age
1.0,"kobe",20.0
1.0,"nash",23.0
2.0,"james",18.0
2.0,"wade",19.0
2.0,"bosh",19.0
2.0,"allen",25.0
1.0,"howard",19.0
1.0,"paul",22.0
2.0,"shane",24.0
[root@localhost mongodb]#
参数说明:
- csv:指明要导出为csv格式
- f:指明需要导出classid、name、age这3列的数据 由上面结果可以看出,mongoexport成功地将数据根据csv格式导出到了students_csv.dat文件中。
bsondump
将 bson 格式的文件转储为 json 格式的数据
mongorestore
MongoDB数据恢复工具, mongodb使用 mongorerstore 命令来恢复备份的数据。
语法
mongorestore命令脚本语法如下:
>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
-
h:MongoDB所在服务器地址
-
d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
--directoryperdb: 备份数据所在位置,例如:c:\data\dump\test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧!
--drop: 恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!
接下来我们执行以下命令:
>mongorestore
执行以上命令输出结果如下:
mongod.exe
MongoDB服务启动工具, 我们可以通过mongod --help查看mongod的所有参数说明,以下是各参数的中文解释。 基本配置参数:
| 参数 | 说明 |
|---|---|
| --quiet | 安静输出 |
| --port arg | 指定服务端口号,默认端口27017 |
| --bind_ip arg | 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP |
| --logpath arg | 指定MongoDB日志文件,注意是指定文件不是目录 |
| --logappend | 使用追加的方式写日志 |
| --pidfilepath arg | PID File 的完整路径,如果没有设置,则没有PID文件 |
| --keyFile arg | 集群的私钥的完整路径,只对于Replica Set 架构有效 |
| --unixSocketPrefix arg | UNIX域套接字替代目录,(默认为 /tmp) |
| --fork | 以守护进程的方式运行MongoDB,创建服务器进程 |
| --auth | 启用验证 |
| --cpu | 定期显示CPU的CPU利用率和iowait |
| --dbpath arg | 指定数据库路径 |
| --diaglog arg | diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads |
| --directoryperdb | 设置每个数据库将被保存在一个单独的目录 |
| --journal | 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里 |
| --journalOptions arg | 启用日志诊断选项 |
| --ipv6 | 启用IPv6选项 |
| --jsonp | 允许JSONP形式通过HTTP访问(有安全影响) |
| --maxConns arg | 最大同时连接数 默认2000 |
| --noauth | 不启用验证 |
| --nohttpinterface | 关闭http接口,默认关闭27018端口访问 |
| --noprealloc | 禁用数据文件预分配(往往影响性能) |
| --noscripting | 禁用脚本引擎 |
| --notablescan | 不允许表扫描 |
| --nounixsocket | 禁用Unix套接字监听 |
| --nssize arg (=16) | 设置信数据库.ns文件大小(MB) |
| --objcheck | 在收到客户数据,检查的有效性, |
| --profile arg | 档案参数 0=off 1=slow, 2=all |
| --quota | 限制每个数据库的文件数,设置默认为8 |
| --quotaFiles arg | # number of files allower per db, requires --quota |
| --rest | 开启简单的rest API |
| --repair | 修复所有数据库run repair on all dbs |
| --repairpath arg | 修复库生成的文件的目录,默认为目录名称dbpath |
| --slowms arg (=100) | value of slow for profile and console log |
| --smallfiles | 使用较小的默认文件 |
| --syncdelay arg (=60) | 数据写入磁盘的时间秒数(0=never,不推荐) |
| --sysinfo | 打印一些诊断系统信息 |
| --upgrade | 如果需要升级数据库 |
Replicaton 参数
| 参数 | 说明 |
|---|---|
| --fastsync | 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步 |
| --autoresync | 如果从库与主库同步数据差得多,自动重新同步, |
| --oplogSize arg | 设置oplog的大小(MB) |
主/从参数
| 参数 | 说明 |
|---|---|
| --master | 主库模式 |
| --slave | 从库模式 |
| --source arg | 从库 端口号 |
| --only arg | 指定单一的数据库复制 |
| --slavedelay arg | 设置从库同步主库的延迟时间 |
Replica set(副本集)选项
| 参数 | 说明 |
|---|---|
| --replSet arg | 设置副本集名称 |
Sharding(分片)选项
| 参数 | 说明 |
|---|---|
| --configsvr | 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb |
| --shardsvr | 声明这是一个集群的分片,默认端口27018 |
| --noMoveParanoia | 关闭偏执为moveChunk数据保存 |
示例
./mongod -shardsvr -replSet shard1 -port 16161 -dbpath /data/mongodb/data/shard1a -oplogSize 100 -logpath /data/mongodb/logs/shard1a.log -logappend -fork -rest
上述参数都可以写入 mongod.conf 配置文档里例如:
dbpath = /data/mongodb
logpath = /data/mongodb/mongodb.log
logappend = true
port = 27017
fork = true
auth = true
mongostat
mongostat是mongodb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态,并输出。如果你发现数据库突然变慢或者有其他问题的话,你第一手的操作就考虑采用mongostat来查看mongo的状态。
启动你的Mongod服务,进入到你安装的MongoDB目录下的bin目录, 然后输入mongostat命令,如下所示:
D:\set up\mongodb\bin>mongostat
以上命令输出结果如下:
mongofiles - GridFS 管理工具,可实现二制文件的存取
mongotop
跟踪一个MongoDB的实例,查看哪些大量的时间花费在读取和写入数据 mongotop也是mongodb下的一个内置工具,mongotop提供了一个方法,用来跟踪一个MongoDB的实例,查看哪些大量的时间花费在读取和写入数据。 mongotop提供每个集合的水平的统计数据。默认情况下,mongotop返回值的每一秒。
启动你的Mongod服务,进入到你安装的MongoDB目录下的bin目录, 然后输入mongotop命令,如下所示:
D:\set up\mongodb\bin>mongotop
以上命令执行输出结果如下:
带参数实例
E:\mongodb-win32-x86_64-2.2.1\bin>mongotop 10
后面的10是参数 ,可以不使用,等待的时间长度,以秒为单位,mongotop等待调用之间。通过的默认mongotop返回数据的每一秒。
E:\mongodb-win32-x86_64-2.2.1\bin>mongotop --locks
报告每个数据库的锁的使用中,使用mongotop - 锁,这将产生以下输出:
输出结果字段说明:
- ns: 包含数据库命名空间,后者结合了数据库名称和集合。
- db: 包含数据库的名称。名为 . 的数据库针对全局锁定,而非特定数据库。
- total: mongod花费的时间工作在这个命名空间提供总额。
- read: 提供了大量的时间,这mongod花费在执行读操作,在此命名空间。
- write: 提供这个命名空间进行写操作,这mongod花了大量的时间。