MongoDB 整库导入

1,262 阅读2分钟

「这是我参与2022首次更文挑战的第27天,活动详情查看:2022首次更文挑战

背景

兄弟部门扔了个文件夹过来,说是mongo导出的部分数据,需要测试一下。

文件夹里大概长这样:

image.png

我表示现在的前端可真不好干。。。

启动MongoDB

既然是测试,那么本地使用docker起一个MongoDB是最方便的,编写docker-compose.yml如下:

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

启动命令:docker-compose up -d --no-recreate

查看MongoDB

可以用mongo-express,打开http://localhost:8081/

image.png

或者安装一个MongoDB Compass

image.png

将数据库文件传入docker 容器

首先查看docker container id,运行docker container ls

image.png 可以使用docker cp命令,docker cp from containerId:to,将文件赋值到目标路径。

进入docker container

控制台输入命令: docker exec -it containerid bash,进入docker container

image.png

检查文件是否已经全部传输完成

进入文件所在目录,通过ls命令查看文件是否已经传过来;通过ls -l | grep "^-" | wc -l命令检查文件数量是否和源文件夹一直。

使用mongorestore导入数据

mongorestore能够创建数据库,并将整个文件夹导入,命令为mongorestore -d db_name 文件夹目录

  • --db=<database>, -d=<database>指定将数据还原到哪个数据库,如果不存在则会创建它

如果命令运行成功,会看到输出* document(s) restored successfully. 0 document(s) failed to restore.*

通过Compass,可以看到我们导入的新数据库:

image.png

常见报错及解决办法

  • 运行命令mongorestore -d db_test ./example,将example中的文件导入db_test库,报错error reading database: (Unauthorized) command listCollections requires authentication

    image.png 需要增加--uri参数:

    mongorestore -d db_test ./example --uri="mongodb://username:password@host:port"
    
  • 运行命令mongorestore -d db_test ./example --uri="mongodb://xxx:xxx@127.0.0.1:27017",报错unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed

    image.png 需要对--uri增加?authSource=admin

    mongorestore -d db_test ./example --uri="mongodb://***:***@host:port/?authSource=admin"