mongodb 复制集

转自(林夕木大大 http://www.cnblogs.com/MDK-L/p/4475959.html)


一,节点类型:

a) 主节点:支持读写

b) 从节点:支持读(需设置)

二, 节点环境

主节点:192.168.129.47
mongodb安装目录:/home/en/tool/mongodb-linux-x86_64-2.6.9/
mongodb数据目录:/home/en/njqgd_db
mongodb日志目录:/home/en/njqgd_log
mongodb密匙目录:/home/en/njqgd_key

node.js 程序目录:/home/en/njqgd_node

java  程序目录:/home/en/njqgd_tomcat/apache-tomcat-7.0.61

从节点:192.168.129.48 
mongodb安装目录:/home/en/tool/mongodb-linux-x86_64-2.6.9/
mongodb数据目录:/njqgd_db/db
mongodb日志目录:/njqgd_db/log
mongodb密匙目录:/njqgd_db/key

清空从节点数据目录:rm -rf /njqgd_db/db/* (请确保从节点目录为空,若不为空执行此命令)

1.主节点操作:

复制代码
    1) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin
    2) sudo echo "njqgd db 1" > /home/en/njqgd_key/key
    3) sudo chmod 600 /home/en/njqgd_key/key
    4) sudo ./mongod --dbpath=/home/en/njqgd_db/ --logpath=/home/en/njqgd_log/mongodb.log --port 27017  --replSet njqgd --keyFile /home/en/njqgd_key/key --maxConns=2000 --fork –logappend (此操作需等待约半分钟)
    
    出现如下内容,说明第4步操作成功:
    about to fork child process, waiting until server is ready for connections.
    forked process: XXXX
    child process started successfully, parent exiting

    5) 现在开始进行从节点操作,从节点操作完毕,再进行下一步.
    6) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin
    7) sudo ./mongo
    8) use admin
    9) db.runCommand({"replSetInitiate" : {"_id" : "njqgd","members" : [{"_id" : 0,"host" : "192.168.129.47:27017"},{"_id" : 1,"host" : "192.168.129.48 :27017"}]}});
    出现如下内容,说明第9步操作成功:
    {
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
    }
    10) 所有操作完毕.
复制代码

2.从节点操作:

复制代码
    1) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin
    2) sudo echo "njqgd db 1" > /njqgd_db/key/key (此处 echo 命令后面 双引号内的内容请和主节点保持一致)
    3) sudo chmod 600 /njqgd_db/key/key
    4) sudo ./mongod --dbpath=/njqgd_db/db/ --logpath=/njqgd_db/log/mongodb.log --port 27017  --replSet njqgd/192.168.129.47:27017 --keyFile /njqgd_db/key/key --maxConns=2000 --fork –logappend

    出现如下内容,说明第4步操作成功:
    about to fork child process, waiting until server is ready for connections.
    forked process: XXXX
    child process started successfully, parent exiting

    5) 从节点操作完成,请继续主节点操作第6步.
复制代码

3. 添加数据库操作用户

复制代码
    1) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin
    2) sudo ./mongo
    3) use admin
    4) db.addUser('njqgd','njqgd');
    5) use wqdwxt_njqgd
    6) db.addUser('njqgd','njqgd');
    7) db.test.insert({'name':'test'})
复制代码

 4. 节点主从状态切换

cfg = rs.conf()
cfg.members[0].priority = 2
cfg.members[1].priority = 1
cfg.members[2].priority = 0.5
rs.reconfig(cfg)

节点的 priority 值越大,切换时的优先级越高.设置好从节点的 priority 值后,调用一次 rs.reconfig 操作会导致当前主库中断,因为

需要重新执行主节点竞选,大概几十秒后,新的主节点产生#查看复制集状态.

rs.status()
rs.isMaster()
rs.conf()

 

#查看从库状态
db.printSlaveReplicationInfo()

 

#设置从库可查询
db.getMongo().setSlaveOk()
rs.setSlaveOk()

三. 开启各项服务.

1.开启mongodb

复制代码
1) sudo -s (输入密码)
2) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin
3) ./mongod --dbpath=/home/en/njqgd_db/ --logpath=/home/en/njqgd_log/mongodb.log --port 27017  --replSet njqgd --keyFile /home/en/njqgd_key/key --maxConns=2000 --fork –logappend 
出现如下类似内容说明成功 about to fork child process, waiting until server is ready for connections. forked process: XXXX child process started successfully, parent exiting
复制代码

 

1.1 mongodb手动切换主从

复制代码
1) sudo -s (输入密码)
2) cd /home/en/tool/mongodb-linux-x86_64-2.6.9/bin
3) ./mongo
4) rs.isMaster();
如果出现如下内容
{
        "setName" : "njqgd",
        "setVersion" : 2,
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "192.168.129.47:27017",
                "192.168.129.48 :27017"
        ],
        "primary" : "192.168.129.47:27017",
        "me" : "192.168.129.48:27017",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 1000,
        "localTime" : ISODate("2015-05-09T02:20:08.633Z"),
        "maxWireVersion" : 2,
        "minWireVersion" : 0,
        "ok" : 1
}
 ismaster为true 当前节点即为主节点(就不用进行下面的操作)

5) use admin
6) db.auth('njqgd','njqgd');
7) rs.status();
{
        "set" : "njqgd",
        "date" : ISODate("2015-05-09T02:28:35Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.129.47:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 54073,
                        "optime" : Timestamp(1431138513, 2),
                        "optimeDate" : ISODate("2015-05-09T02:28:33Z"),
                        "electionTime" : Timestamp(1431084460, 1),
                        "electionDate" : ISODate("2015-05-08T11:27:40Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.129.48:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 54059,
                        "optime" : Timestamp(1431138512, 3),
                        "optimeDate" : ISODate("2015-05-09T02:28:32Z"),
                        "lastHeartbeat" : ISODate("2015-05-09T02:28:33Z"),
                        "lastHeartbeatRecv" : ISODate("2015-05-09T02:28:33Z"),
                        "pingMs" : 1,
                        "syncingTo" : "222.186.43.150:27017"
                }
        ],
        "ok" : 1
}
要记住 节点顺序 以便下一步操作
6) cfg = rs.conf() <回车>
 cfg.members[0].priority = 2 <回车> (此处中括号中是节点顺序.从0开始)
cfg.members[1].priority = 1 <回车>
rs.reconfig(cfg) <回车>
节点的 priority 值越大,切换时的优先级越高.设置好从节点的 priority 值后,调用一次 rs.reconfig 操作会导致当前主库中断,因为

需要重新执行主节点竞选,大概几十秒后,新的主节点产生#查看复制集状态.
复制代码

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值