内容纲要

windows

需要先创建好 --dbpath--logpath 目录

@echo off
:: 启动 mongodb
cd /d "%~dp0\mongodb-win32-x86_64-2012plus-4.2.3\bin"
mongod --replSet "rs0" --bind_ip 127.0.0.1 --dbpath "d:\rs0\rps1" --logpath "d:\rs0\rps1-log\mongodb-rs0-rps1.log" --port 3001
cd /d "%~dp0"
@echo off
:: 启动 mongodb
cd /d "%~dp0\mongodb-win32-x86_64-2012plus-4.2.3\bin"
mongod --replSet "rs0" --bind_ip 127.0.0.1 --dbpath "d:\rs0\rps2" --logpath "d:\rs0\rps2-log\mongodb-rs0-rps2.log" --port 3002
cd /d "%~dp0"
@echo off
:: 启动 mongodb
cd /d "%~dp0\mongodb-win32-x86_64-2012plus-4.2.3\bin"
mongod --replSet "rs0" --bind_ip 127.0.0.1 --dbpath "d:\rs0\rps3" --logpath "d:\rs0\rps3-log\mongodb-rs0-rps3.log" --port 3003
cd /d "%~dp0"

启动三个节点后,需要 mongo shell 到一个节点上配置

mongo 127.0.0.1:3001

这里最好用 hostname 注册,以便迁移

rs.initiate({
    "_id" : "rs0",
    "members" : [
        {
            "_id" : 0,
            "host" : "127.0.0.1:3001",
            "priority": 99
        },
        {
            "_id" : 1,
            "host" : "127.0.0.1:3002"
        },
        {
            "_id" : 2,
            "host" : "127.0.0.1:3003"
        }
    ]
});

测试数据

for(var i = 0; i < 10000; i ++) {
    db.col.insert({title: 'MongoDB Demo',
        description: 'MongoDB Demo' + i,
        by: 'Demo' + i,
        url: 'http://www.test.com',
        tags: ['mongodb', 'database', 'NoSQL'],
        likes: 100 + i
    });
}

linux

./mongod --dbpath /opt/mongodb-replset/rs0/rps1 --logpath /opt/mongodb-replset/rs0/rps1-log/mongodb-rs0-rps1.log --fork --port 3001 --replSet "rs0" 
./mongod --dbpath /opt/mongodb-replset/rs0/rps2 --logpath /opt/mongodb-replset/rs0/rps2-log/mongodb-rs0-rps2.log --fork --port 3002 --replSet "rs0" 
./mongod --dbpath /opt/mongodb-replset/rs0/rps3 --logpath /opt/mongodb-replset/rs0/rps3-log/mongodb-rs0-rps3.log --fork --port 3003 --replSet "rs0" 

发表评论

电子邮件地址不会被公开。 必填项已用*标注