MongoDB 导入导出和数据迁移

·  阅读 1379

迁移需求

现有测试服务器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 文档

分类:
后端
标签:
分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改