主机环境:
192.168.0.103 主节点
192.168.0.103 副本节点
192.168.0.104 副本节点
下载地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.23.tgz
tar xf mongodb-linux-x86_64-3.4.23.tgz -C /home/wanzixiang/app/
cd /home/wanzixiang/app/
mv mongodb-linux-x86_64-3.4.23 mongodb-3.4.23
cd mongodb-3.4.23
mkdir {data,logs}
vi bin/mongodb.conf
dbpath=/home/wanzixiang/app/mongodb-3.4.23/data
logpath=/home/wanzixiang/app/mongodb-3.4.23/logs/mongodb.log
port=27017
fork=true
nohttpinterface=true
分别三台服务器上启动mongodb:
/home/wanzixiang/app/mongodb-3.4.23/bin/mongod --replSet repset -f /home/wanzixiang/app/mongodb-3.4.23/bin/mongodb.conf
各个服务器查看,都已经启动:
ps -ef|grep mongodb
wanzixi+ 130 1 2 12:23 ? 00:00:00 mongod --replSet repset -f /home/wanzixiang/app/mongodb-3.4.23/bin/mongodb.conf
在三台机器上任意一台机器登陆mongodb:
/home/wanzixiang/app/mongodb-3.4.23/bin/mongo
MongoDB shell version v3.4.23
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.23
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-11-13T12:23:50.028+0800 I CONTROL [initandlisten]
2019-11-13T12:23:50.028+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-11-13T12:23:50.028+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-11-13T12:23:50.028+0800 I CONTROL [initandlisten]
>
使用admin数据库
> use admin
switched to db admin
定义副本集配置变量,这里的_id:”repset”和上面命令参数--replSet repset保持一致
> config = { _id:"repset", members:[{_id:0,host:"192.168.0.103:27017"},{_id:1,host:"192.168.0.104:27017"},{_id:2,host:"192.168.0.105:27017"}]}
{
"_id" : "repset",
"members" : [
{
"_id" : 0,
"host" : "192.168.2.104:27017"
},
{
"_id" : 1,
"host" : "192.168.2.105:27017"
},
{
"_id" : 2,
"host" : "192.168.2.106:27017"
}
]
}
初始化副本集群
> rs.initiate(config);
{ "ok" : 1 }
查看集群节点的状态
repset:SECONDARY> rs.status();
{
"set" : "repset",
"date" : ISODate("2019-11-13T05:20:42.827Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.2.104:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 350,
"optime" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-13T05:20:34Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1573622392, 1),
"electionDate" : ISODate("2019-11-13T05:19:52Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.2.105:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 60,
"optime" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-13T05:20:34Z"),
"optimeDurableDate" : ISODate("2019-11-13T05:20:34Z"),
"lastHeartbeat" : ISODate("2019-11-13T05:20:42.459Z"),
"lastHeartbeatRecv" : ISODate("2019-11-13T05:20:42.065Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.2.104:27017",
"syncSourceHost" : "192.168.2.104:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.2.106:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 60,
"optime" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1573622434, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-13T05:20:34Z"),
"optimeDurableDate" : ISODate("2019-11-13T05:20:34Z"),
"lastHeartbeat" : ISODate("2019-11-13T05:20:42.459Z"),
"lastHeartbeatRecv" : ISODate("2019-11-13T05:20:42.055Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.2.104:27017",
"syncSourceHost" : "192.168.2.104:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
repset:PRIMARY>
测试集群功能
主节点连接到终端:
mongo 127.0.0.1
repset:PRIMARY>
连接test数据库
repset:PRIMARY> use test;
switched to db test
往testdb表里插入数据
repset:PRIMARY> db.testdb.insert({"test1":"testval1"})
WriteResult({ "nInserted" : 1 })
在副本节点连接查询
mongo 192.168.2.105:27017
repset:SECONDARY> use test
switched to db test
repset:SECONDARY> show tables;
2019-11-13T13:42:44.349+0800 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
}
提示无法读取数据
mongodb默认是从主节点读写数据,副本节点上不允许读,设置副本节点可读
repset:SECONDARY> db.getMongo().setSlaveOk();
然后就可以查询复制过来的数据了
repset:SECONDARY> show tables;
testdb
repset:SECONDARY> db.testdb.find();
{ "_id" : ObjectId("5dcb95faf5b9cc19c47022b1"), "test1" : "testval1" }