Published on

Mongodb 数据迁移

Authors
  • avatar
    Name
    Shelton Ma
    Twitter

由于原有数据库为Mongodb 3.4, 很多驱动已不再支持, 使用docker能方便对应版本

1. 执行记录

# ubuntu下安装docker
cat /etc/os-release

# 更新包索引
sudo apt-get update

# The Ubuntu package docker actually refers to a GUI application, not the beloved DevOps tool we've come out to look for.
# sudo apt-get install -y docker
curl -sSL https://get.docker.com/ | sudo sh
sudo apt-get install docker.io

# 下载mongo3.4
docker pull  m.daocloud.io/docker.io/mongo:3.4.24

# 启动mongodb, 挂载外部目录
docker run --name mongodb -d -p 27017:27017 -v /root/mongodb_migration:/data/db m.daocloud.io/docker.io/mongo:3.4.24

## 导出数据
docker exec -it mongodb mongodump --uri="mongodb://localhost:27017" --collection=projects --out=/data/db/backup

docker exec -it mongodb mongodump --uri="mongodb://user:password@xx:27017" --collection=projects --out=/data/db/backup

## 下载mongodb7
docker pull  m.daocloud.io/docker.io/mongo
docker run --name mongodb7 -d -p 27018:27018 -v /root/mongodb7:/data/db m.daocloud.io/docker.io/mongo

## 导入数据
docker exec -it mongodb7 mongorestore --uri="mongodb://user:password@localhost:27017node2d-dev?replicaSet=mgset-xx5&authSource=admin" /data/db/backup/db_name

# mongoshell
docker run -it mongo7 mongosh "mongodb://user:password@localhost:27017node2d-dev?replicaSet=mgset-xx5&authSource=admin"