MongoDB复制集环境搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB复制集环境搭建相关的知识,希望对你有一定的参考价值。
环境介绍:
192.168.1.250 主 port=27408
192.168.1.250 仲裁 port=27409
192.168.1.251 备 port=27408
[[email protected] ~]# tar xvf mongodb-linux-x86_64-2.6.10.tgz
[[email protected] ~]# mkdir -p /export/mongodb
[[email protected] ~]# mkdir -p /export/mongodb/bin
[[email protected] ~]# mkdir -p /export/mongodb/conf
[[email protected] ~]# mkdir -p /export/mongodb/log
[[email protected] ~]# mkdir -p /export/mongodb/data
[[email protected] bin]# cd /root/mongodb-linux-x86_64-2.6.10/bin
[[email protected] bin]# cp /root/mongodb-linux-x86_64-2.6.10/bin/* /export/mongodb/bin/
[[email protected] bin]# vi /export/mongodb/conf/mongod.conf
port=27408 dbpath=/export/mongodb/data logpath=/export/mongodb/log/mongod.log fork=true logappend=true keyFile=/export/mongodb/key/mongod nohttpinterface=true replSet=shard1 [[email protected] bin]# vi /export/mongodb/conf/arbiter.conf port=27409 dbpath=/export/mongodb/arbiter logpath=/export/mongodb/log/arbiter.log fork=true logappend=true keyFile=/export/mongodb/key/arbiter nohttpinterface=true replSet=shard1
keyfile文件包括:
mongod,arbiter
创建一个生成keyfile的脚本
vi create_key.sh
cat /dev/urandom | LC_ALL=C tr -dc "[:alnum:]" | fold -w 10 |head -1 >/tmp/key.txt keystring=`cat /tmp/key.txt` echo $keystring >/export/mongodb/key/mongod echo $keystring >/export/mongodb/key/arbiter chmod 600 /export/mongodb/key/*
启动服务器在主
[[email protected] ~]#/export/mongodb/bin/mongod -f /export/mongodb/conf/mongod.conf
[[email protected] ~]# /export/mongodb/bin/mongod -f /export/mongodb/conf/arbiter.conf
在从
[[email protected] ~]#/export/mongodb/bin/mongod -f /export/mongodb/conf/mongod.conf
>config={_id:‘shard1‘,members:[{_id:0,host:‘192.168.1.248:27408‘},{_id:1,host:‘192.168.1.249:27408‘},{_id:2,host:‘192.168.1.248:27409‘,arbiterOnly:true}]}
>rs.initiate(config)
初始化rs.initiate(config),config是之前定义的名
主备库配置好后,备库查询
shard1:SECONDARY> use test switched to db test shard1:SECONDARY> db.t1.find() error: { "$err" : "not master and slaveOk=false", "code" : 13435 } shard1:SECONDARY> rs.slaveOk() shard1:SECONDARY> db.t1.find() { "_id" : ObjectId("5704c11d3e0651733bfdea23"), "x" : 1 }
rs.stauts()可以看状态,health:1代表健康,stateStr谁是我们的仲裁
想让主库降级成从库,rs.stepDown()
本文出自 “岁伏” 博客,请务必保留此出处http://suifu.blog.51cto.com/9167728/1853478
以上是关于MongoDB复制集环境搭建的主要内容,如果未能解决你的问题,请参考以下文章
mongodb复制集(Replica sets)+分片(Sharding)环境搭建