MongoDB 导入导出和数据迁移 |
您所在的位置:网站首页 › mongodb导入数据库 › MongoDB 导入导出和数据迁移 |
迁移需求
现有测试服务器A 和 测试服务器 B,需要实现从测试服务器A向测试服务器B进行mongoDB 数据库的迁移。 可以使用 mongoDB 的导出工具 mongoexport 和导入工具 mongoimport 实现。 官方英文文档链接 mongoDB mongoexport ,mongoDB mongoimport 数据导出mongoexport 参数项 general options: --help print usage --version print the tool version and exit verbosity options: -v, --verbose= 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= mongodb host to connect to (setname/host1,host2 for replica sets) --port= server port (can also use --host hostname:port) authentication options: -u, --username= username for authentication -p, --password= password for authentication --authenticationDatabase= database that holds the user's credentials --authenticationMechanism= authentication mechanism to use namespace options: -d, --db= database to use -c, --collection= collection to use output options: -f, --fields=[,]* comma separated list of field names (required for exporting CSV) e.g. -f "name,age" --fieldFile= file with field names - 1 per line --type= the output format, either json or csv (defaults to 'json') -o, --out= 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= query filter, as a JSON string, e.g., '{x:{$gt:1}}' --queryFile= path to a file containing a query filter (JSON) -k, --slaveOk allow secondary reads if available (default true) --readPreference=| specify either a preference name or a preference json object --forceTableScan force a table scan (do not use $snapshot) --skip= number of documents to skip --limit= limit the number of documents to export --sort= 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= 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= mongodb host to connect to (setname/host1,host2 for replica sets) --port= server port (can also use --host hostname:port) authentication options: -u, --username= username for authentication -p, --password= password for authentication --authenticationDatabase= database that holds the user's credentials --authenticationMechanism= authentication mechanism to use namespace options: -d, --db= database to use -c, --collection= collection to use input options: -f, --fields=[,]* comma separated list of field names, e.g. -f name,age --fieldFile= file with field names - 1 per line --file= 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= 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 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=[,]* comma-separated fields for the query part of the upsert --writeConcern= 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. 即不要将 mongoimport 和 mongoexport 用于完整实例生产备份,因为它们无法可靠地捕获数据类型信息。使用 mongodump 和 mongorestore ,如“MongoDB系统的备份策略”中所述,以实现此类功能。 mongoDB mongodump 文档 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |