【MongoDB】Mongo4.0单实例以及副本集搭建(后附生产配置文件)

1.Mongo DB单实例部署方式

1.MongoDB 单实例部署
#1.安装必须模块
yum install libcurl openssl

#2.下载mongo DB安装包
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.22.tgz

#3.根据配置文件创建所需文件夹 
mkdir -p /data/mongodb/{dbdata,log,run,conf}/
  
#4.解压mongodb 压缩包到对应文件
tar -xvf https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.22.tgz -C /data/mongodb/mongodb_4_22

#5.将mongo 相关命令加入到环境变量或者复制到/usr/bin/
vim /etc/profile
或者
cp /data/mongodb/mongodb_4_22/bin/* /usr/bin/

#6.调整mongo的配置文件
文章末尾有生产配置

#7.生成keyfile
openssl rand -base64 741 >> keyfile

#8.控制权限
chmod 600 /data/mongodb/conf/keyfile

#7.启动mongo db并验证
mongod --config mongodb.conf
mongo <ip>:port/admin -u -p

nohup mongod --config /data/mongodb/mongo01/conf/mongodb01.conf &

mongo --port 31001  
mongo localhost:31001/admin -uroot -p

#8.需要用户的话,创建用户
use admin

db.createUser(
{
user:"username",
pwd:"userpass",
roles:[{role:"root",db:"admin"}]
}
)

rs.auth('','')

2.MongoDB ReplicaSet搭建

#1.首先准备三个mongodb单节点实例,具体操作如上

#2.传入配置文件
cfg={_id :"3dk_test",members:[{ _id: 0, host: "IP:31001",priority: 3 },{ _id: 1, host: "IP:31002",priority: 1 },{ _id: 2, host: "IP:31003",priority: 1}]}

#3.初始化
rs.initiate(cfg)

#4.查看状态
rs.status()

3.重新初始化Mongo DB集群

#我们经常会遇到需要重新初始化集群的需求,比如配置错了,做法如下:
#1.修改配置
cfg={_id :"3dk_test",members:[{ _id: 0, host: "IP:31001",priority: 3 },{ _id: 1, host: "IP:31002",priority: 1 },{ _id: 2, host: "IP:31003",priority: 1}]}

#2.重新初始化集群,primary上执行
rs.initiate(cfg)

#3.查看集群状态
rs.status()

4.MongoDB 4.0 版本live配置文件参考

storage:
    dbPath: "/data/mongodb/dbdata"
    engine: "wiredTiger"
    directoryPerDB: true
    syncPeriodSecs: 60    #调用fsync函数刷新数据的时间间隔
    journal:
        enabled: true      #类似于MySQL redo
    wiredTiger:
        engineConfig:
            cacheSizeGB: 2
            directoryForIndexes: true
        collectionConfig:
            blockCompressor: snappy
        indexConfig:
            prefixCompression: true

systemLog:
    destination: file
    path: "/data/mongodb/log/mongod.log"
    logAppend: true
    logRotate: reopen    #避免日志丢失但是logAppend: true必须开启
    timeStampFormat: iso8601-utc #iso8601-local测试对比

processManagement:
    fork: false
    pidFilePath: "/data/mongodb/run/mongod.pid"

operationProfiling:
    slowOpThresholdMs: 100
    mode: "slowOp"

replication:
    oplogSizeMB: 10240
    replSetName: "cluster_name"

net:
    port: 31001
    wireObjectCheck: false
	bindIP: 0.0.0.0

security:
    keyFile: "/data/mongodb/conf/keyfile"
    authorization: "enabled"
    clusterAuthMode: "keyFile"

5.参考链接

mongo官方配置解释
mongo重新初始化RS

你可能感兴趣的:(MongoDB,mongodb,数据库,nosql,mongo,4.0)