MongoDB 搭建复制集

下载

https://blog.csdn.net/ko0491/article/details/108764916

建立3个配置文件

  • mongo_27017.conf
#数据目录
dbpath=/data/mongo/data/server1
#端口号
port=27017
bind_ip=0.0.0.0
#以后台运行
fork=true
# 日志路径
logpath = /data/mongo/logs/server1.log
# 追加
logappend = true
# 授权
auth=false
#复制集
replSet=myCluster
  • mongo_27018.conf
#数据目录
dbpath=/data/mongo/data/server2
#端口号
port=27018
bind_ip=0.0.0.0
#以后台运行
fork=true
# 日志路径
logpath = /data/mongo/logs/server2.log
# 追加
logappend = true
# 授权
auth=false
#复制集
replSet=myCluster
  • mongo_27019.conf
#数据目录
dbpath=/data/mongo/data/server3
#端口号
port=27019
bind_ip=0.0.0.0
#以后台运行
fork=true
# 日志路径
logpath = /data/mongo/logs/server3.log
# 追加
logappend = true
# 授权
auth=false
#复制集
replSet=myCluster

在data目录下建立相就的目录

分别启动


./mongod -f ../mongo_27017.conf
./mongod -f ../mongo_27018.conf
./mongod -f ../mongo_27019.conf

分别连接

./mongo --host=192.168.181.135 --port=27017
./mongo --host=192.168.181.135 --port=27018
./mongo --host=192.168.181.135 --port=27019

在任意一个节点

rs.initiate({
    _id: "myCluster",
    members: [{
        _id: 0,
        host: "192.168.181.135:27017",
        "priority":10
        
    },{
        _id: 1,
        host: "192.168.181.135:27018"
    },{
        _id: 2,
        host: "192.168.181.135:27019"
    }]
})


  • 查看状态
    每个节点
    rs.status()
rs.status()

myCluster:PRIMARY> rs.status()
{
	"set" : "myCluster",
	"date" : ISODate("2020-09-27T10:19:14.279Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1601201950, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableRecoveryTimestamp" : Timestamp(1601201950, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1601201950, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.181.135:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 988,
			"optime" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T10:19:10Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1601201409, 1),
			"electionDate" : ISODate("2020-09-27T10:10:09Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.181.135:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 555,
			"optime" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T10:19:10Z"),
			"optimeDurableDate" : ISODate("2020-09-27T10:19:10Z"),
			"lastHeartbeat" : ISODate("2020-09-27T10:19:13.397Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T10:19:13.653Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.181.135:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 555,
			"optime" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601201950, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T10:19:10Z"),
			"optimeDurableDate" : ISODate("2020-09-27T10:19:10Z"),
			"lastHeartbeat" : ISODate("2020-09-27T10:19:13.397Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T10:19:13.653Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1601201950, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1601201950, 1)
}
myCluster:PRIMARY> 

27017
在这里插入图片描述
27018

在这里插入图片描述
27019
在这里插入图片描述

在这里插入图片描述

节点的动态增删

增加节点
rs.add("192.168.181.135:37019")
删除slave 节点
rs.remove("192.168.181.135:37019")

调整复制集配置

var conf = rs.conf()
// 将0号节点的优先级调整为10
conf.members[0].priority = 10;
// 将1号节点调整为hidden节点
conf.members[1].hidden = true;
// hidden节点必须配置{priority: 0}
conf.members[1].priority = 0;
// 应用以上调整
rs.reconfig(conf);

在这里插入图片描述

其它配置

# /usr/local/mongodb/mongodb.conf
systemLog:
  destination: file
  path: /usr/local/mongodb/log/mongodb.log # log path
  logAppend: true
storage:
  dbPath: /usr/local/mongodb/data/ # data directory
net:
  bindIp: 0.0.0.0
  port: 27017 # port
  maxIncomingConnections: 500
replication:
  enableMajorityReadConcern: true
  replSetName: rs0
processManagement:
  fork: true
  pidFilePath: /usr/local/mongodb/run/mongodb.pid

测试

myCluster:PRIMARY> use local
switched to db local
myCluster:PRIMARY> show tables
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log

在这里插入图片描述

  • 插入数据
myCluster:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
myCluster:PRIMARY> use myresume
switched to db myresume
myCluster:PRIMARY> db.my_test.insert({name:'zhangsan',age:18})
WriteResult({ "nInserted" : 1 })
myCluster:PRIMARY> db.my_test.find()
{ "_id" : ObjectId("5f706974ada3cc994b4d8e41"), "name" : "zhangsan", "age" : 18 }
myCluster:PRIMARY> show tables;
my_test

从库查看

rs.slaveOk()

不然不能查

查看从节点,都有

  • 从节点插入失败
myCluster:SECONDARY> db.my_test.insert({name:"lisi",age:33})
WriteCommandError({
	"operationTime" : Timestamp(1601203170, 1),
	"ok" : 0,
	"errmsg" : "not master",
	"code" : 10107,
	"codeName" : "NotMaster",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1601203170, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
})
myCluster:SECONDARY> 


有仲裁节点复制集搭建

_id 整数 _id:0 复制集中的标示
host 字符串 host:“主机:端口” 节点主机名
arbiterOnly 布尔值 arbiterOnly:true 是否为仲裁(裁判)节点
priority(权重)
整数 priority=0|1 默认1,是否有资格变成主节点,取值范围0-1000,0永远不
会变成主节点
hidden 布尔值 hidden=true|false,0|1 隐藏,权重必须为0,才可以设置
votes 整数 votes= 0|1 投票,是否为投票节点,0 不投票,1投票
slaveDelay 整数 slaveDelay=3600 从库的延迟多少秒
buildIndexes 布尔值 buildIndexes=true|false,0|1 主库的索引,从库也创建,_id索引无效

和上面的配置步骤相同 只是增加了 一个特殊的仲裁节点

注入节点 执行 rs.addArb(“IP:端口”);
rs.addArb(“192.168.181.135:27020”)

或者重新配置

只能在主节点下进行

var cfg ={
 _id: "myCluster",
 "protocolVersion" : 1,
    members: [{
        _id: 0,
        host: "192.168.181.135:27017",
		priority:10
    },{
        _id: 1,
        host: "192.168.181.135:27018",
		priority:1
    },{
        _id: 2,
        host: "192.168.181.135:27019",
		priority:5
    },{
        _id: 3,
        host: "192.168.181.135:27020",
		arbiterOnly:true
	}]
}
rs.reconfig(cfg)

  • 查看
myCluster:PRIMARY> rs.status()
{
	"set" : "myCluster",
	"date" : ISODate("2020-09-27T11:12:20.987Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1601205137, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableRecoveryTimestamp" : Timestamp(1601205131, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1601205131, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.181.135:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 4174,
			"optime" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T11:12:17Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1601201409, 1),
			"electionDate" : ISODate("2020-09-27T10:10:09Z"),
			"configVersion" : 4,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.181.135:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 3742,
			"optime" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T11:12:17Z"),
			"optimeDurableDate" : ISODate("2020-09-27T11:12:17Z"),
			"lastHeartbeat" : ISODate("2020-09-27T11:12:19.912Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T11:12:19.918Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 4
		},
		{
			"_id" : 2,
			"name" : "192.168.181.135:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 3742,
			"optime" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1601205137, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-09-27T11:12:17Z"),
			"optimeDurableDate" : ISODate("2020-09-27T11:12:17Z"),
			"lastHeartbeat" : ISODate("2020-09-27T11:12:19.912Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T11:12:19.922Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.181.135:27017",
			"syncSourceHost" : "192.168.181.135:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 4
		},
		{
			"_id" : 3,
			"name" : "192.168.181.135:27020",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 3,
			"lastHeartbeat" : ISODate("2020-09-27T11:12:19.914Z"),
			"lastHeartbeatRecv" : ISODate("2020-09-27T11:12:19.938Z"),
			"pingMs" : NumberLong(1),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 4
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1601205137, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1601205137, 1)
}

©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页