数据库
首页 > 数据库> > CentOS7.4下使用docker运行mongodb,对原有mongodb备份及迁移

CentOS7.4下使用docker运行mongodb,对原有mongodb备份及迁移

作者:互联网

一、查看原有mongodb的版本

# mongo --version
MongoDB shell version v3.4.24
git version: 865b4f6a96d0f5425e39a18337105f33e8db504d
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

二、选择相同版本的mongodb的docker镜像

访问 MongoDB 镜像库地址: https://hub.docker.com/_/mongo?tab=tags&page=1

可以通过 Sort by 查看其他版本的 MongoDB,默认是最新版本 mongo:latest。

搜索3.4.24的镜像版本

 

 

三、获取并安装mongo镜像

#拉取镜像
docker pull mongo:3.4.24

#查看镜像
docker images

四、修改配置,启动容器

#1创建映射目录
mkdir -p /data/mongodb/data
mkdir -p /data/mongodb/log
mkdir -p /data/mongodb/conf


#2修改配置
vi /data/mongodb/conf/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/log/mongo.log

# Where and how to store data.
storage:
  dbPath: /data/db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
#processManagement:
#  fork: true  # fork and run in background
#  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

security:  
    authorization: enabled  
#    clusterAuthMode: keyFile  
#    keyFile: /srv/mongodb/keyfile  
#    javascriptEnabled: true 
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:


#3.启动容器
docker run -itd \
--name mongo \
--restart always \
--privileged=true \
-p 27017:27017 \
-v /data/mongodb/data:/data/db \
-v /data/mongodb/log:/data/log \
-v /data/mongodb/conf:/data/configdb \
-e TZ=Asia/Shanghai \
mongo:3.4.24 \
-f /data/configdb/mongod.conf

#查看容器
docker ps

五、创建用户

#1.登录原有服务器上的mongo
mongo

#2.密码校验
use admin
db.auth("joshua317","password");

#3.查看全局所有账户
db.system.users.find().pretty()

#4.查看当前库下的账户
show users

#5.进入新服务的mongo容器
docker exec -it mongo /bin/bash

#6.登录mongo
mongo

#7.创建用户
use admin
db.createUser({user:'joshua317',pwd:'password', roles:[{role:'root', db:'admin'}]})

use chat
db.createUser({user:'joshua317',pwd:'123456', roles:[{role:'readWrite', db:'chat'}]})


db.auth("joshua317","123456");

六、备份原有数据

#1.登录原有服务器上的mongo
mongo
#2.密码校验
use admin;
db.auth("joshua317","password");
#3.查看数据库
show dbs;
exit;

#4.备份,备份后会生成一个文件夹,对文件夹进行打包并传递到新的服务器
mongodump -d chat -o /root/ -u=josua317 --authenticationDatabase admin

七、恢复原有数据

#1.进入新服务的mongo容器
docker exec -it mongo /bin/bash

#恢复数据
mongorestore -d chat /data/db/chat -u josua317 --authenticationDatabase admin

 

标签:mongo,joshua317,mongodb,db,data,docker,CentOS7.4
来源: https://www.cnblogs.com/joshua317/p/16459220.html