Mongodb中replicaset的搭建以及部署
-- mongodb replicaset的搭建
1 download the install package
wget (地址不一定對,自己去官網下在最新的版本)
2 uncompress the package
tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37017
tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37018
tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37019
tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37020
3 config the 4 conf
[root@mongo mongodb]# vim /etc/mongodb/37017.conf
dbpath = /db/db1
port = 37017
rest = true
fork = true
logpath = /etc/mongodb/mongodb17.log
logappend = true
replSet = sso-resti
#keyFile = /home/mongo/keyfile
auth = true
4 Start primary server
/usr/local/mongodb37017/bin/mongod -f /etc/mongodb/37017.conf
5 Start second server
/usr/local/mongodb37018/bin/mongod -f /etc/mongodb/37018.conf
/usr/local/mongodb37019/bin/mongod -f /etc/mongodb/37019.conf
/usr/local/mongodb37020/bin/mongod -f /etc/mongodb/37020.conf
6 config 在primary上面的mongo shell控制檯上面run如下command, 建立primary-second 關係
config = {_id: 'sso-resti', members: [
{_id: 0, host: '127.0.0.1:37017'},
{_id: 1, host: '127.0.0.1:37018'},
{_id: 2, host: '127.0.0.1:37019'},
{_id: 3, host: '127.0.0.1:37020'}
]};
rs.initiate(config);
rs.status();
報錯:
Fri May 3 16:04:23 [rsSync] replSet initial sync pending
Fri May 3 16:04:23 [rsSync] replSet syncing to: 127.0.0.1:37017
Fri May 3 16:04:23 [rsSync] replauthenticate: no user in local.system.users to use for authentication
repl:
Fri May 3 16:04:23 [rsSync] replSet initial sync couldn't connect to 127.0.0.1:37017
1 建立一個key檔案,裡面是隨機字串,chmod 600 key,然後在四個conf檔案裡都開啟keyFile引數,指向這個key檔案
2 你也可以將admin庫檔案複製到其他的資料目錄試試,停機copy
我試了第一個方案,搞定
但是又報新的錯誤:
"errmsg" : "syncThread: 13257 cmdline oplogsize (100) different than existing (14179) see:"
註釋掉 oplogSize=100引數
延遲比較optimeDate就行了
7 成功狀態
PRIMARY> rs.status();
{
"set" : "sso-resti",
"date" : ISODate("2013-05-03T08:40:46Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:37017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1367567307000,
"i" : 1
},
"optimeDate" : ISODate("2013-05-03T07:48:27Z"),
"self" : true
},
{
"_id" : 1,
"name" : "127.0.0.1:37018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1231,
"optime" : {
"t" : 1367567307000,
"i" : 1
},
"optimeDate" : ISODate("2013-05-03T07:48:27Z"),
"lastHeartbeat" : ISODate("2013-05-03T08:40:45Z"),
"pingMs" : 0
},
{
"_id" : 2,
"name" : "127.0.0.1:37019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1052,
"optime" : {
"t" : 1367567307000,
"i" : 1
},
"optimeDate" : ISODate("2013-05-03T07:48:27Z"),
"lastHeartbeat" : ISODate("2013-05-03T08:40:44Z"),
"pingMs" : 0
},
{
"_id" : 3,
"name" : "127.0.0.1:37020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1062,
"optime" : {
"t" : 1367567307000,
"i" : 1
},
"optimeDate" : ISODate("2013-05-03T07:48:27Z"),
"lastHeartbeat" : ISODate("2013-05-03T08:40:45Z"),
"pingMs" : 0
}
],
"ok" : 1
}
8 insert data and test
PRIMARY> use test;
switched to db test
PRIMARY> db.tim.save({"":1});
PRIMARY> db.tim.save({"a":2});
PRIMARY> db.tim.find();
{ "_id" : ObjectId("51837ab1c77389f37f74792d"), "" : 1 }
{ "_id" : ObjectId("51837ac0c77389f37f74792e"), "a" : 2 }
PRIMARY>
SECONDARY> show collections;
Fri May 3 16:54:09 uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }
SECONDARY>
SECONDARY> db.getMongo().setSlaveOk()
not master and slaveok=false
SECONDARY> db.tim.find();
{ "_id" : ObjectId("51837ab1c77389f37f74792d"), "" : 1 }
{ "_id" : ObjectId("51837ac0c77389f37f74792e"), "a" : 2 }
SECONDARY>
primary庫和secondary庫上資料一致了,OK.
PS: 感謝網友Mr.Sharp的大力協助。沒有他我不能如此順利部署完畢。
[2013-08-02 續補]
以上的例子是沒有設立primary與secondary的切換監控伺服器arbiter的,補充一個初始化的時候設立arbiter監控的配置,如下:
config = {_id: 'rpl', members: [
{_id: 0, host: '10.100.10.19:27017'},
{_id: 1, host: '10.100.10.19:27018'},
{_id: 2, host: '10.100.10.19:27019', arbiterOnly: true},
]};
rs.initiate(config);
rs.status();
{
"set" : "rpl",
"date" : ISODate("2013-08-02T02:57:09Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.100.10.19:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 66247,
"optime" : {
"t" : 1375349303000,
"i" : 2995
},
"optimeDate" : ISODate("2013-08-01T09:28:23Z"),
"lastHeartbeat" : ISODate("2013-08-02T02:57:08Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "10.100.10.19:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1375349303000,
"i" : 2995
},
"optimeDate" : ISODate("2013-08-01T09:28:23Z"),
"self" : true
},
{
"_id" : 2,
"name" : "10.100.10.19:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 66247,
"optime" : {
"t" : 0,
"i" : 0
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2013-08-02T02:57:08Z"),
"pingMs" : 0
}
],
"ok" : 1
}
PRIMARY>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26230597/viewspace-1102132/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb replicaSet 模式搭建MongoDB模式
- 【Mongodb】ReplicaSet的選舉策略之三MongoDB
- Mongodb在replicaset的secondary上補建索引MongoDB索引
- redis 叢集搭建以及redislive監測部署Redis
- ubuntu部署mongodbUbuntuMongoDB
- mongodb的安裝以及使用MongoDB
- k8s中Pod、ReplicaSet、Deployment、Service的概念(轉)K8S
- 淺入Kubernetes(7):應用部署例項,Deployment、Service、ReplicaSet
- Docker 搭建 MongoDBDockerMongoDB
- mongodb分片搭建MongoDB
- MongoDB部署副本集MongoDB
- Mongodb叢集部署MongoDB
- MongoDB、Redis 以及 Memcache 的區別MongoDBRedis
- MongoDB 副本集搭建MongoDB
- 搭建MongoDB副本集MongoDB
- MongoDB 分片的原理、搭建、應用MongoDB
- 小丸子學MongoDB系列之——部署MongoDB副本集MongoDB
- 伺服器部署前端&node專案(包括阿里雲伺服器、nginx以及mongoDB 的配置)伺服器前端阿里NginxMongoDB
- 搭建高可用MongoDB叢集(一):配置MongoDBMongoDB
- MongoDB 分片叢集搭建MongoDB
- Docker 搭建叢集 MongoDBDockerMongoDB
- MongoDB 搭建複製集MongoDB
- Docker 搭建MongoDB環境DockerMongoDB
- mongodb複製集搭建MongoDB
- 搭建MongoDB分片叢集MongoDB
- Tokumx 代替 Mongodb 群集部署MongoDB
- MongoDB 副本集的原理、搭建、應用MongoDB
- Linux下的Mongodb部署應用梳理LinuxMongoDB
- lisp 中的 【,@】 與 【·】 以及【‘】 以及【 。,】Lisp
- SpringBoot專案在linux系統中的部署(直接部署、外接tomcat中部署)以及配置httpsSpring BootLinuxTomcatHTTP
- 快速掌握mongoDB(一)——mongoDB安裝部署和常用shell命令MongoDB
- MongoDB 中的鎖分析MongoDB
- MongoDB 中的事務MongoDB
- Ceph 架構以及部署架構
- mongodb 4.0副本集搭建MongoDB
- 【Mongodb】 可複製集搭建MongoDB
- MongoDB高可用叢集搭建MongoDB
- MongoDB 6.0.3副本集搭建MongoDB