1. 程式人生 > >MongoDB 匯入匯出和資料遷移

MongoDB 匯入匯出和資料遷移

遷移需求

現有測試伺服器A 和 測試伺服器 B,需要實現從測試伺服器A向測試伺服器B進行mongoDB 資料庫的遷移。
可以使用 mongoDB 的匯出工具 mongoexport 和匯入工具 mongoimport 實現。
官方英文文件連結 mongoDB mongoexportmongoDB mongoimport

資料匯出

mongoexport 引數項

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

output options:
  -f, --fields=<field>[,<field>]*                 comma separated list of field names (required
                                                  for exporting CSV) e.g. -f "name,age"
      --fieldFile=<filename>                      file with field names - 1 per line
      --type=<type>                               the output format, either json or csv (defaults
                                                  to 'json')
  -o, --out=<filename>                            output file; if not specified, stdout is used
      --jsonArray                                 output to a JSON array rather than one object
                                                  per line
      --pretty                                    output JSON formatted to be human-readable

querying options:
  -q, --query=<json>                              query filter, as a JSON string, e.g.,
                                                  '{x:{$gt:1}}'
      --queryFile=<filename>                      path to a file containing a query filter (JSON)
  -k, --slaveOk                                   allow secondary reads if available (default
                                                  true)
      --readPreference=<string>|<json>            specify either a preference name or a
                                                  preference json object
      --forceTableScan                            force a table scan (do not use $snapshot)
      --skip=<count>                              number of documents to skip
      --limit=<count>                             limit the number of documents to export
      --sort=<json>                               sort order, as a JSON string, e.g. '{x:1}'
      --assertExists                              if specified, export fails if the collection
                                                  does not exist (false)

匯出集合為 .csv 檔案

mongoexport --db users --collection contacts --csv --fieldFile fields.txt --out /opt/backups/contacts.csv

匯出集合為 .json 檔案

mongoexport --db sales --collection contacts --out contacts.json --journal

mongoimport 引數選項

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:
  -f, --fields=<field>[,<field>]*                 comma separated list of field names, e.g. -f
                                                  name,age
      --fieldFile=<filename>                      file with field names - 1 per line
      --file=<filename>                           file to import from; if not specified, stdin is
                                                  used
      --headerline                                use first line in input source as the field
                                                  list (CSV and TSV only)
      --jsonArray                                 treat input source as a JSON array
      --type=<type>                               input format to import: json, csv, or tsv
                                                  (defaults to 'json')

ingest options:
      --drop                                      drop collection before inserting documents
      --ignoreBlanks                              ignore fields with empty values in CSV and TSV
      --maintainInsertionOrder                    insert documents in the order of their
                                                  appearance in the input source
  -j, --numInsertionWorkers=<number>              number of insert operations to run concurrently
                                                  (defaults to 1)
      --stopOnError                               stop importing at first insert/upsert error
      --upsert                                    insert or update objects that already exist
      --upsertFields=<field>[,<field>]*           comma-separated fields for the query part of
                                                  the upsert
      --writeConcern=<write-concern-specifier>    write concern options e.g. --writeConcern
                                                  majority, --writeConcern '{w: 3, wtimeout: 500,
                                                  fsync: true, j: true}' (defaults to 'majority')
      --bypassDocumentValidation                  bypass document validation

mongoimport 匯入 .csv 檔案

mongoimport --db users --collection contacts --type csv --file /opt/backups/contacts.csv

mongoimport 匯入 .json 檔案

mongoimport --collection contacts --file contacts.json --journal

伺服器例項

進入找到伺服器A的 mongodb 安裝目錄下
例如:cd /usr/local/mongodb/bin

資料匯出:
./mongoexport -d DataBaseName -c CollectionName -o bak.dat

其中,DataBaseName 為資料庫名稱,CollectionName 為集合名稱,bak.dat 為匯出後的名稱

匯出後的bak.dat將在 mongoexport所在的目錄下。
例如:

./mongoexport -d user -c guset -o guset.dat

將資料庫 user 下的集合 guset 匯出到 mongoexport 所在的目錄下,並將其命名為 guset.dat

匯入資料

移動匯出的資料檔案到另外一臺伺服器的mongo 目錄下

sudo mv /tmp/bak.dat /db/mongo/bin

注:bak.dat 問原來伺服器匯出的資料檔案。
進入 mongoDB 安裝目錄。

cd /db/mongo/bin

使用

./mongoimport -h 127.0.0.1:port -u xxx -p xxx-d DataBaseName -c CollectionName bak.dat

其中,DataBaseName 為資料庫名稱,CollectionName 為集合名稱,bak.dat 為匯入的集合

例項操作

./mongoimport -h 127.0.0.1:27017 -u user -p user -d guset -c guset bak.dat

操作結束。

注意 ⚠️

其中有寫到

Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore as described in “Backup Strategies for MongoDB Systems” for this kind of functionality.

即不要將 mongoimportmongoexport 用於完整例項生產備份,因為它們無法可靠地捕獲資料型別資訊。使用 mongodumpmongorestore ,如“MongoDB系統的備份策略”中所述,以實現此類功能。
mongoDB mongodump 文件