创建MongoDB副本集

下载mongodb

cd /home/zlfzy/tmp
wget http://app-zlf.oss-cn-hangzhou.aliyuncs.com/mongodb-linux-x86_64-rhel62-3.2.0.tgz

解压到目录,设置软链接

tar xf mongodb-linux-x86_64-rhel62-3.2.0.tgz -C /home/zlfzy/app
ln -s /home/zlfzy/app/mongodb-linux-x86_64-rhel62-3.2.0/ /home/zlfzy/app/mongodb

设置mongodb环境变量,确保mongo、mongod等命令可用

export PATH=/home/zlfzy/app/mongodb/bin:$PATH:$HOME/bin
source /etc/profile

配置副本集

cd /home/zlfzy/app/mongodb
mkdir -p 27017/{data,log}
mkdir -p 27018/{data,log}
mkdir -p 27019/{data,log}
touch 27017/mongo.conf

logpath=/home/zlfzy/app/mongodb/27017/log/mongo.log
 
dbpath=/home/zlfzy/app/mongodb/27017/data
 
bind_ip=127.0.0.1   #若允许外网访问,需删掉此项
 
logappend=true
 
fork=true
 
port=27017

同理创建27018,27019的配置文件

启动mongodb

mongod --config /home/zlfzy/app/mongodb/27017/mongo.conf --replSet set1
mongod --config /home/zlfzy/app/mongodb/27018/mongo.conf --replSet set1
mongod --config /home/zlfzy/app/mongodb/27019/mongo.conf --replSet set1

配置副本集:
连接27017

mongo --port 27017

定义变量:

>conf = {_id:"set1",members:[{_id:0,host:"127.0.0.1:27017"}]}
{
    "_id" : "set1",
    "members" : [
        {
            "_id" : 0,
            "host" : "127.0.0.1:27017"
        }
    ]
}

>rs.initiate(conf)
{ "ok" : 1 }

将本实例设置成master:

set1:PRIMARY> db.isMaster() 
{
    "hosts" : [
        "127.0.0.1:27017"
    ],
    "setName" : "set1",
    "setVersion" : 1,
    "ismaster" : true,
    "secondary" : false,
    "primary" : "127.0.0.1:27017",
    "me" : "127.0.0.1:27017",
    "electionId" : ObjectId("591d60530000000000000001"),
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 1000,
    "localTime" : ISODate("2017-05-18T08:50:45.869Z"),
    "maxWireVersion" : 4,
    "minWireVersion" : 0,
    "ok" : 1
}

将27018、27019添加到副本集:

set1:PRIMARY> rs.add("127.0.0.1:27018")
{ "ok" : 1 }
set1:PRIMARY> rs.add("127.0.0.1:27019")
{ "ok" : 1 }

登录任一台查看副本集状态:

set1:PRIMARY> rs.status()
{
    "set" : "set1",
    "date" : ISODate("2017-05-18T08:51:16.780Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [
        {
            "_id" : 0,
            "name" : "127.0.0.1:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 206,
            "optime" : {
                "ts" : Timestamp(1495097463, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-05-18T08:51:03Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1495097427, 2),
            "electionDate" : ISODate("2017-05-18T08:50:27Z"),
            "configVersion" : 3,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "127.0.0.1:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 21,
            "optime" : {
                "ts" : Timestamp(1495097463, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-05-18T08:51:03Z"),
            "lastHeartbeat" : ISODate("2017-05-18T08:51:15.822Z"),
            "lastHeartbeatRecv" : ISODate("2017-05-18T08:51:14.825Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "127.0.0.1:27017",
            "configVersion" : 3
        },
        {
            "_id" : 2,
            "name" : "127.0.0.1:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 12,
            "optime" : {
                "ts" : Timestamp(1495097463, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-05-18T08:51:03Z"),
            "lastHeartbeat" : ISODate("2017-05-18T08:51:15.822Z"),
            "lastHeartbeatRecv" : ISODate("2017-05-18T08:51:14.861Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "127.0.0.1:27017",
            "configVersion" : 3
        }
    ],
    "ok" : 1
}

修改默认数据引擎:
mongo3.2默认引擎为WiredTiger,若需要使用其他引擎,可在启动时指定:

mongod --storageEngine mmapv1 --config mongo.conf --replSet set1

正常关闭mongodb

mongod --shutdown --dbpath=/home/zlfzy/app/mongod/mongo/27018/

强制kill掉mongo,再启动时会报错:
ERROR: child process failed, exited with error number 100
需先修复:

mongod -f mongo.conf --repair

然后再启动

添加新评论