下载文件 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.2.tgz
安装到三台机器
172.26.239.38:27010
172.26.239.34:27010
172.26.239.37:27010

mongodb.conf配置文件

dbpath=/www/server/mongodb2/data
logpath=/www/server/mongodb2/log/mongodb.log
fork=true
logappend=true
bind_ip=0.0.0.0
port=27010
wiredTigerCacheSizeGB = 1

replSet=hdt-mongo

执行 ./mongod -config ../mongodb.conf --auth输出以下内容视为成功

2022-02-14T14:15:17.189+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 7198
child process started successfully, parent exiting

登录master机器

配置节点信息

conf=
    {
    "_id" : "hdt-mongo",
    "members" : [
        { "_id" : 0, "host" : "172.26.239.38:27010",priority:1 },
        { "_id" : 1, "host" : "172.26.239.34:27010",priority:2 },
        { "_id" : 2, "host" : "172.26.239.37:27010","arbiterOnly":true }
        ]
    }

初始化节点

rs.initiate(conf)

查看members中的 "stateStr" : "PRIMARY",是否有三种不同的状态

创建用户

use admin;
 db.createUser({
     "user": "platform",
     "pwd": "platform",
     "roles": [ { "role": "dbOwner", "db": "activity_platform" } ]
})

命令行登录

./mongo "mongodb://platform:platform@172.26.239.38:27010,172.26.239.34:27010/activity_platform?authSource=admin&replicaSet=hdt-mongo&readPreference=secondaryPreferred"

PRIMARY插入数据测试

db.test.insert({"name":"张三","sex":"男","age":22,"userid":1001,"createdatetime":new Date()})

登录SECONDARY读取测试

如果通过shell访问mongo,要在secondary进行查询。会出现如下错误:
imageSet:SECONDARY> db.fs.files.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
有两种方法实现从机的查询:
第一种方法: db.getMongo().setSlaveOk();
第二种方法:rs.slaveOk();

db.test.find();

数据恢复

./mongorestore -h127.0.0.1:27010 -uplatform -p platform -d activity_platform  --authenticationDatabase platform  /www/server/backup/mongodb/activity_platform
最后修改:2022 年 07 月 07 日 07 : 00 PM
如果觉得我的文章对你有用,请随意赞赏