mongodb導(dǎo)出導(dǎo)入-創(chuàng)新互聯(lián)

數(shù)據(jù)庫都分為冷備份與熱備份,所謂的冷備份就是停庫,然后把數(shù)據(jù)拷貝;熱備份就是使用相應(yīng)工具,在數(shù)據(jù)庫運(yùn)行時(shí)做備份。每種庫都會(huì)有自己的熱備份工具,mongodb也不例外。

專注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)廣昌免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

mongodb的備份主要有熱備和冷備。

一,冷備:

停止mongodb服務(wù),直接copy數(shù)據(jù)目錄;啟動(dòng)mongodb時(shí),可以用--dbpath指定自己設(shè)置的數(shù)據(jù)庫存儲(chǔ)目錄。

二,熱備:

用mongodump來做MongoDB的庫或表級(jí)別的熱備份。

認(rèn)識(shí)一下mongodump,

[mongo@tstdb-25-220 mongodb]$ bin/mongodump --help Usage:   mongodump <options> Export the content of a running server into .bson files. Specify a database with -d and a collection with -c to only dump that database or collection. See http://docs.mongodb.org/manual/reference/program/mongodump/ for more information. general options:       --help                                                print usage       --version                                             print the tool version and exit verbosity options:   -v, --verbose=<level>                                     more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric                                                             value, e.g. --verbose=N)       --quiet                                               hide all log output connection options:   -h, --host=<hostname>                                     mongodb host to connect to (setname/host1,host2 for replica sets)       --port=<port>                                         server port (can also use --host hostname:port) authentication options:   -u, --username=<username>                                 username for authentication   -p, --password=<password>                                 password for authentication       --authenticationDatabase=<database-name>              database that holds the user's credentials       --authenticationMechanism=<mechanism>                 authentication mechanism to use namespace options:   -d, --db=<database-name>                                  database to use   -c, --collection=<collection-name>                        collection to use query options:   -q, --query=                                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'       --queryFile=                                          path to a file containing a query filter (JSON)       --readPreference=<string>|<json>                      specify either a preference name or a preference json object       --forceTableScan                                      force a table scan output options:   -o, --out=<directory-path>                                output directory, or '-' for stdout (defaults to 'dump')       --gzip                                                compress archive our collection output with Gzip       --repair                                              try to recover documents from damaged data files (not supported by all storage engines)       --oplog                                               use oplog for taking a point-in-time snapshot       --archive=<file-path>                                 dump as an archive to the specified path. If flag is specified without a value, archive is written to                                                             stdout       --dumpDbUsersAndRoles                                 dump user and role definitions for the specified database       --excludeCollection=<collection-name>                 collection to exclude from the dump (may be specified multiple times to exclude additional collections)       --excludeCollectionsWithPrefix=<collection-prefix>    exclude all collections from the dump that have the given prefix (may be specified multiple times to                                                             exclude additional prefixes)   -j, --numParallelCollections=                             number of collections to dump in parallel (4 by default)

看下導(dǎo)出過程

[mongo@tstdb-25-220 mongodb]$ bin/mongodump -h 172.16.25.220 -d sykdb -o /usr/local/mongodb/backup 2016-10-31T09:41:03.521+0800    writing sykdb.system.indexes to  2016-10-31T09:41:03.522+0800    done dumping sykdb.system.indexes (2 documents) 2016-10-31T09:41:03.523+0800    writing sykdb.system.profile to  2016-10-31T09:41:03.523+0800    writing sykdb.sykdb to  2016-10-31T09:41:03.523+0800    writing sykdb.table_syk to  2016-10-31T09:41:03.524+0800    done dumping sykdb.system.profile (2 documents) 2016-10-31T09:41:03.524+0800    done dumping sykdb.sykdb (2 documents) 2016-10-31T09:41:03.524+0800    done dumping sykdb.table_syk (1 document)

導(dǎo)出后的文件如下:

[mongo@tstdb-25-220 mongodb]$ cd backup/ [mongo@tstdb-25-220 backup]$ ll 總用量 4 drwxr-xr-x. 2 mongo dbmon 4096 10月 31 09:41 sykdb [mongo@tstdb-25-220 backup]$ cd sykdb/ [mongo@tstdb-25-220 sykdb]$ ll 總用量 28 -rw-r--r--. 1 mongo dbmon   75 10月 31 09:41 sykdb.bson -rw-r--r--. 1 mongo dbmon   83 10月 31 09:41 sykdb.metadata.json -rw-r--r--. 1 mongo dbmon  136 10月 31 09:41 system.indexes.bson -rw-r--r--. 1 mongo dbmon 1529 10月 31 09:41 system.profile.bson -rw-r--r--. 1 mongo dbmon   55 10月 31 09:41 system.profile.metadata.json -rw-r--r--. 1 mongo dbmon   36 10月 31 09:41 table_syk.bson -rw-r--r--. 1 mongo dbmon   87 10月 31 09:41 table_syk.metadata.json

下面看一下導(dǎo)入(導(dǎo)出與導(dǎo)入可以看做是備份與恢復(fù),所有的備份都是為了恢復(fù)而做)

[mongo@tstdb-25-220 mongodb]$ bin/mongorestore --help Usage:   mongorestore <options> <directory or file to restore> Restore backups generated with mongodump to a running server. Specify a database with -d to restore a single database from the target directory, or use -d and -c to restore a single collection from a single .bson file. See http://docs.mongodb.org/manual/reference/program/mongorestore/ for more information. general options:       --help                                      print usage       --version                                   print the tool version and exit verbosity options:   -v, --verbose=<level>                           more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g.                                                   --verbose=N)       --quiet                                     hide all log output connection options:   -h, --host=<hostname>                           mongodb host to connect to (setname/host1,host2 for replica sets)       --port=<port>                               server port (can also use --host hostname:port) authentication options:   -u, --username=<username>                       username for authentication   -p, --password=<password>                       password for authentication       --authenticationDatabase=<database-name>    database that holds the user's credentials       --authenticationMechanism=<mechanism>       authentication mechanism to use namespace options:   -d, --db=<database-name>                        database to use   -c, --collection=<collection-name>              collection to use input options:       --objcheck                                  validate all objects before inserting       --oplogReplay                               replay oplog for point-in-time restore       --oplogLimit=<seconds>[:ordinal]            only include oplog entries before the provided Timestamp       --archive=<filename>                        restore dump from the specified archive file.  If flag is specified without a value, archive is read from stdin       --restoreDbUsersAndRoles                    restore user and role definitions for the given database       --dir=<directory-name>                      input directory, use '-' for stdin       --gzip                                      decompress gzipped input restore options:       --drop                                      drop each collection before import       --writeConcern=<write-concern>              write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}'                                                   (defaults to 'majority')       --noIndexRestore                            don't restore indexes       --noOptionsRestore                          don't restore collection options       --keepIndexVersion                          don't update index version       --maintainInsertionOrder                    preserve order of documents during restoration   -j, --numParallelCollections=                   number of collections to restore in parallel (4 by default)       --numInsertionWorkersPerCollection=         number of insert operations to run concurrently per collection (1 by default)       --stopOnError                               stop restoring if an error is encountered on insert (off by default)       --bypassDocumentValidation                  bypass document validation

--oplogReplay這個(gè)參數(shù),大家注意一下,mongodb的導(dǎo)出導(dǎo)入可以做的基本時(shí)間點(diǎn)的恢復(fù)

我們?cè)賗nsert一行,然后測(cè)試一下:

moe:PRIMARY> db.sykdb.insert ({"job":"dba"})

然后導(dǎo)入:

[mongo@tstdb-25-220 mongodb]$ bin/mongorestore -h 172.16.25.220 -d sykdb backup/sykdb 2016-10-31T10:28:21.049+0800    building a list of collections to restore from backup/sykdb dir 2016-10-31T10:28:21.051+0800    reading metadata for sykdb.sykdb from backup/sykdb/sykdb.metadata.json 2016-10-31T10:28:21.051+0800    reading metadata for sykdb.table_syk from backup/sykdb/table_syk.metadata.json 2016-10-31T10:28:21.051+0800    reading metadata for sykdb.system.profile from backup/sykdb/system.profile.metadata.json 2016-10-31T10:28:21.051+0800    restoring sykdb.sykdb from backup/sykdb/sykdb.bson 2016-10-31T10:28:21.051+0800    no indexes to restore 2016-10-31T10:28:21.052+0800    finished restoring sykdb.system.profile (0 documents) 2016-10-31T10:28:21.052+0800    restoring sykdb.table_syk from backup/sykdb/table_syk.bson 2016-10-31T10:28:21.086+0800    restoring indexes for collection sykdb.sykdb from metadata 2016-10-31T10:28:21.086+0800    restoring indexes for collection sykdb.table_syk from metadata 2016-10-31T10:28:21.086+0800    finished restoring sykdb.sykdb (2 documents) 2016-10-31T10:28:21.086+0800    finished restoring sykdb.table_syk (1 document) 2016-10-31T10:28:21.087+0800    done

查詢一下,看數(shù)據(jù)是否回來了

moe:PRIMARY> db.sykdb.find().limit(100); { "_id" : ObjectId("5816ac26d37a70860b091c1f"), "job" : "dba" } { "_id" : ObjectId("5813041a56c55d778812e689"), "name" : "syk" } { "_id" : ObjectId("5813065356c55d778812e68a"), "loc" : "beijing" }

介紹一對(duì)命令:mongoexport/mongoimport

[mongo@tstdb-25-220 mongodb]$ bin/mongoexport -h 172.16.25.220 -d sykdb -c table_syk -o table_syk.dat 2016-10-31T11:43:40.893+0800    connected to: 172.16.25.220 2016-10-31T11:43:40.894+0800    exported 1 record

查看導(dǎo)出來的文件:

[mongo@tstdb-25-220 mongodb]$ strings table_syk.dat  {"_id":{"$oid":"581313a856c55d778812e68b"},"name":"syk"} [mongo@tstdb-25-220 mongodb]$ file table_syk.dat  table_syk.dat: ASCII text

看來這個(gè)工具相當(dāng)于其他數(shù)據(jù)庫中的邏輯導(dǎo)出

把表清空

moe:PRIMARY> db.table_syk.drop()

導(dǎo)入

[mongo@tstdb-25-220 mongodb]$ bin/mongoimport -h 172.16.25.220 -d sykdb -c table_syk table_syk.dat 2016-10-31T11:45:22.499+0800    connected to: 172.16.25.220 2016-10-31T11:45:22.540+0800    imported 1 document

數(shù)據(jù)回來了。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前題目:mongodb導(dǎo)出導(dǎo)入-創(chuàng)新互聯(lián)
瀏覽地址:http://www.muchs.cn/article26/shojg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、服務(wù)器托管網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站制作品牌網(wǎng)站建設(shè)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)