mongodb 数据备份导入导出

来源:互联网 发布:书生软件 百度百科 编辑:程序博客网 时间:2024/05/15 07:53

(1)mongoDB导出(mongoexport).

可以通过运行命令:./mongoexport --help

./mongoexport --helpoptions:  --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
参数说明。

例如我导出数据库中的一张表。

./mongoexport -h 127.0.0.1  --port 27017  -d upload -c video_encode_history -u uts -p u.l7a@bj  -f mid,createTime,imgHttpPath -q '{vtype:"1",status:30,imgHttpPath:{"$ne":null}}' --csv -o  history.txt


(2)mongodb 导入工具 mongodimport

运行帮助命令:./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.

导出命令:/usr/local/mongodb/bin/mongoimport --username=uts --password=password -h 127.0.0.1 --port 27017 -d upload  -c c_cloud video.json 

远程连接到其他mongoDB机器上:

/usr/local/mongodb/bin/mongo --username=uts --password=u.l7a@bj host:port/upload

原创粉丝点击