【MongoDB】配置篇(Linux环境)

学习笔记 2020/08/18 MongoDB

二、配置

    使用一台机器创建3个副本集,端口地址分别为27017、27018、27019

    新建数据库存放目录

cd /usr/local/mongodb
mkdir data


创建3个副本集数据的存放目录

cd data/
mkdir db1 db2 db3


创建日志目录,和创建数据存放目录类似

cd ../
mkdir logs
cd logs/
mkdir log1 log2 log3


配置config文件

cd /usr/local/mongodb/data
touch mdb1.conf mdb2.conf mdb3.conf
vim mdb1.conf
#数据库路径
dbpath=/usr/local/mongodb/data/db1
#日志输出文件路径
logpath=/usr/local/mongodb/logs/log1/log.log
#错误日志采用追加模式
logappend=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#端口号 默认为27017
port=27017
#允许远程访问
bind_ip=0.0.0.0
#开启子进程
fork=true
#开启认证,必选先添加用户
#auth=true
#keyFile=/usr/local/mongodb/data/keyfile

# 设置副本集名称,在各个配置文件中,其值必须相同
replSet=rs0


修改 mdb2.conf

vim mdb2.conf
#数据库路径
dbpath=/usr/local/mongodb/data/db1
#日志输出文件路径
logpath=/usr/local/mongodb/logs/log1/log.log
#错误日志采用追加模式
logappend=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#端口号
port=27018
#允许远程访问
bind_ip=0.0.0.0
#开启子进程
fork=true
#开启认证,必选先添加用户
#auth=true
#keyFile=/usr/local/mongodb/data/keyfile

# 设置副本集名称,在各个配置文件中,其值必须相同
replSet=rs0


修改 mdb3.conf

vim mdb3.conf
#数据库路径
dbpath=/usr/local/mongodb/data/db1
#日志输出文件路径
logpath=/usr/local/mongodb/logs/log1/log.log
#错误日志采用追加模式
logappend=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#端口号
port=27019
#允许远程访问
bind_ip=0.0.0.0
#开启子进程
fork=true
#开启认证,必选先添加用户
#auth=true
#keyFile=/usr/local/mongodb/data/keyfile

# 设置副本集名称,在各个配置文件中,其值必须相同
replSet=rs0


启动mongodb

mongod -f /usr/local/mongodb/data/mdb1.conf


启动成功

\"3.png\"/


同样方式启动其他配置

mongod -f /usr/local/mongodb/data/mdb2.conf
mongod -f /usr/local/mongodb/data/mdb3.conf


登陆mongo查看

mongo 127.0.0.1:27017

添加副本集

config={
  "_id":"rs0",
    members:[
        {_id:0,host:"127.0.0.1:27017"},        
        {_id:1,host:"127.0.0.1:27018"},
        {_id:2,host:"127.0.0.1:27019"}]
    }  
rs.initiate(config)
rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-08-18T06:29:33.166Z"),
        "myState" : 1,
        "term" : NumberLong(8),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1597732166, 1),
                        "t" : NumberLong(8)
                },
                "lastCommittedWallTime" : ISODate("2020-08-18T06:29:26.436Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1597732166, 1),
                        "t" : NumberLong(8)
                },
                "readConcernMajorityWallTime" : ISODate("2020-08-18T06:29:26.436Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1597732166, 1),
                        "t" : NumberLong(8)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1597732166, 1),
                        "t" : NumberLong(8)
                },
                "lastAppliedWallTime" : ISODate("2020-08-18T06:29:26.436Z"),
                "lastDurableWallTime" : ISODate("2020-08-18T06:29:26.436Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1597732166, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-08-18T06:23:46.374Z"),
                "electionTerm" : NumberLong(8),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1597730897, 1),
                        "t" : NumberLong(6)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-08-18T06:23:46.417Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-08-18T06:23:47.422Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "127.0.0.1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 364,
                        "optime" : {
                                "ts" : Timestamp(1597732166, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDate" : ISODate("2020-08-18T06:29:26Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1597731826, 1),
                        "electionDate" : ISODate("2020-08-18T06:23:46Z"),
                        "configVersion" : 1,
                        "configTerm" : 8,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "127.0.0.1:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 356,
                        "optime" : {
                                "ts" : Timestamp(1597732166, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1597732166, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDate" : ISODate("2020-08-18T06:29:26Z"),
                        "optimeDurableDate" : ISODate("2020-08-18T06:29:26Z"),
                        "lastHeartbeat" : ISODate("2020-08-18T06:29:32.446Z"),
                        "lastHeartbeatRecv" : ISODate("2020-08-18T06:29:31.574Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "127.0.0.1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 8
                },
                {
                        "_id" : 2,
                        "name" : "127.0.0.1:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 349,
                        "optime" : {
                                "ts" : Timestamp(1597732166, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1597732166, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDate" : ISODate("2020-08-18T06:29:26Z"),
                        "optimeDurableDate" : ISODate("2020-08-18T06:29:26Z"),
                        "lastHeartbeat" : ISODate("2020-08-18T06:29:32.446Z"),
                        "lastHeartbeatRecv" : ISODate("2020-08-18T06:29:31.577Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "127.0.0.1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 8
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1597732166, 1),
                "signature" : {
                        "hash" : BinData(0,"kTbyJGGHuloxCSsCbBvKW3vEwcY="),
                        "keyId" : NumberLong("6861832669135634436")
                }
        },
        "operationTime" : Timestamp(1597732166, 1)
}


副本集设置成功!

本文地址:https://www.stayed.cn/item/241

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。