想建设个人网站去那里建设个人网站备案资料
分享自己mongodb导出导入经验。将一个数据库数据备份,导入到另一个数据库。
 mongodb的导入导出工具有版本限制,过旧的版本是不支持导入导出的。mongodb 4.2以后版本支持比较好。mongodb 3.4以前完全不支持。
1,下载
mongodb的导入导出需要自己下载工具,是由官方提供的。下载前根据自己服务器版本下载对应工具
 linux查看服务器版本命令:
dmidecode -t 1
 
mongodb的导入导出需要自己下载工具,是由官方提供的,下载地址如下:
https://www.mongodb.com/try/download/bi-connector
 
2,官方中文文档
https://www.mongodb.com/zh-cn/docs/database-tools/mongoexport/mongoexport-examples/
 
3,操作记录
服务器系统centos7 选择下载mongodb-database-tools-rhel70-x86_64-100.10.0.tgz
 上传服务器/root目录并解压,创建导出目录
  tar -zxvf mongodb-database-tools-rhel70-x86_64-100.10.0.tgzmkdir /root/mongoexport
 
cd到bin目录:
cd mongodb-database-tools-rhel70-x86_64-100.10.0/bin
 
我选择远程带密码的方式备份数据库,这样工具可以与mongodb数据库不在同一服务器上。
 如果在同一服务器可以不带ip和账号密码。
部分引用名称解释
db 数据库
 collection 表
mongodump全部备份
在bin目录执行备份语句
./mongodump --host=<ip> --port=27017--username=<username> --password=<password> --authenticationDatabase=<authenticationDatabase>  --db=<db> --out=/root/mongoexport/mongodump-2024-09-26
 
mongorestore 恢复数据
./mongorestore --host=<ip> --port=27017--username=<username> --password=<password> --authenticationDatabase=<authenticationDatabase>  --db=<newdb>  /root/mongoexport/mongodump-2024-09-26/<db>
 
mongoexport 单表导出方式
URL方式远程导出
 authSource和authenticationDatabase是一个值
./mongoexport --uri='mongodb://<username>:<password>@<ip>:27017/<db>?authSource=<authSource>' --collection=<collection> --out=/root/mongoexport/<collection>.json
 
mongoimport单表导入方式
URL方式远程导入
./mongoimport --uri='mongodb://<username>:<password>@<ip>:27017/<db>?authSource=<authSource>' --db=<db> --file==/root/mongoexport/<collection>.json
 
4 遇到过的问题
    1,版本mongodb 3.4 不支持导入导出,建议升级版本2,导出需要足够存储空间,确定服务器存储资源充沛。
