(一)数据备份、恢复与迁移管理
1、备份全部数据库
mkdir testbak
cd testbak
mongodump
cd testbak
mongodump
说明:默认备份目录及数据文件格式为./dump/[databasename]/[collectionname].bson
2、备份指定数据库
mongodump -d pagedb
2、备份指定数据库
mongodump -d pagedb
说明:备份数据库pagedb中的数据。
3、备份一个数据库中的某个集合
mongodump -d pagedb -c page
说明:备份数据库pagedb的page集合。
4、恢复全部数据库
cd testbak
mongorestore --drop
mongorestore --drop
说明:将备份的所有数据库恢复到数据库,--drop指定恢复数据之前删除原来数据库数据,否则会造成回复后的数据中数据重复。
5、恢复某个数据库的数据
cd testbak
mongorestore -d pagedb --drop
说明:将备份的pagedb的数据恢复到数据库。
mongorestore -d pagedb --drop
说明:将备份的pagedb的数据恢复到数据库。
6、恢复某个数据库的某个集合的数据
cd testbak
mongorestore -d pagedb -c page --drop
说明:将备份的pagedb的的page集合的数据恢复到数据库。
mongorestore -d pagedb -c page --drop
说明:将备份的pagedb的的page集合的数据恢复到数据库。
7、向MongoDB导入数据
mongoimport -d pagedb -c page --type csv --headerline --drop < csvORtsvFile.csv
说明:将文件csvORtsvFile.csv的数据导入到pagedb数据库的page集合中,使用cvs或tsv文件的列名作为集合的列名。需要注意的是,使用--headerline选项时,只支持csv和tsv文件。
--type支持的类型有三个:csv、tsv、json
其他各个选项的使用,可以查看帮助:
--type支持的类型有三个:csv、tsv、json
其他各个选项的使用,可以查看帮助:
mongoimport --help options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) -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 -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.
8、从向MongoDB导出数据
mongoexport -d pagedb -c page -q {} -f _id,title,url,spiderName,pubDate --csv > pages.csv
说明:将pagedb数据库中page集合的数据导出到pages.csv文件,其中各选项含义:
-f 指定cvs列名为_id,title,url,spiderName,pubDate
-q 指定查询条件
其他各个选项的使用,可以查看帮助:
说明:将pagedb数据库中page集合的数据导出到pages.csv文件,其中各选项含义:
-f 指定cvs列名为_id,title,url,spiderName,pubDate
-q 指定查询条件
其他各个选项的使用,可以查看帮助:
mongoexport --help options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) -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 -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
注意:
如果上面的选项-q指定一个查询条件,需要使用单引号括起来,如下所示:
mongoexport -d page -c Article -q '{"spiderName": "mafengwoSpider"}' -f _id,title,content,images,publishDate,spiderName,url --jsonArray > mafengwoArticle.txt
否则,就会出现下面的错误:
ERROR: too many positional options
(二)远程连接管理
1、基于mongo实现远程连接mongo -u admin -p admin 192.168.0.197:27017/pagedb
通过mongo实现连接,可以非常灵活的选择参数选项,参看命令帮助,如下所示:
mongo --help MongoDB shell version: 1.8.3 usage: mongo [options] [db address] [file names (ending in .js)] db address can be: foo foo database on local machine 192.169.0.5/foo foo database on 192.168.0.5 machine 192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999 options: --shell run the shell after executing files --nodb don't connect to mongod on startup - no 'db address' arg expected --quiet be less chatty --port arg port to connect to --host arg server to connect to --eval arg evaluate javascript -u [ --username ] arg username for authentication -p [ --password ] arg password for authentication -h [ --help ] show this usage information --version show version information --verbose increase verbosity --ipv6 enable IPv6 support (disabled by default)
2、基于MongoDB支持的javascript实现远程连接
当你已经连接到一个远程的MongoDB数据库服务器(例如,通过mongo连接到192.168.0.184),现在想要在这个会话中连接另一个远程的数据库服务器(192.168.0.197),可以执行如下命令:
> var x = new Mongo('192.168.0.197:27017') > var ydb = x.getDB('pagedb'); > use ydb switched to db ydb > db ydb > ydb.page.findOne() { "_id" : ObjectId("4eded6a5bf3bfa0014000003"), "content" : "巴黎是浪漫的城市,可是...", "pubdate" : "2006-03-19", "title" : "巴黎:从布鲁塞尔赶到巴黎", "url" : "http://france.bytravel.cn/Scenery/528/cblsegdbl.html" }
上述通过MongoDB提供的JavaScript脚本,实现对另一个远程数据库服务器进行连接,操作指定数据库pagedb的page集合。
如果启用了安全认证模式,可以在获取数据库连接实例时,指定认证账号,例如:
> var x = new Mongo('192.168.0.197:27017') > var ydb = x.getDB('pagedb', 'shirdrn', '(jkfFS$343$_\=\,.F@3'); > use ydb switched to db ydb