【数据库系列教程】MongoDB 4.2配置主从复制

一、环境介绍

主机名 IP地址 软件
db-master.linux.com 192.168.140.13 MongoDB 4.2
db-slave.linux.com 192.168.140.14 MongoDB 4.2

二、部署MongoDB 4.2

1.配置MongoDB Yum

[root@db-master ~]# vim /etc/yum.repos.d/mongodb.repo

[mongodb42]
name=mongodb42
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://pgp.mongodb.com/server-4.2.asc

2.把Yum源拷贝给另外一台机器

[root@db-master ~]# rsync -av /etc/yum.repos.d/*.repo root@192.168.140.14:/etc/yum.repos.d/

3.安装MongoDB Server

[root@db-master ~]# yum install -y mongodb-org
[root@db-slave ~]# yum install -y mongodb-org

4.修改MongoDB主库配置文件

[root@db-master ~]# vim /etc/mongod.conf
#配置文件并不完整,仅展示修改部分
net:
  port: 27017
  bindIp: 0.0.0.0	#修改监听地址为所有地址
  
#在文件末尾添加以下内容
replication:	#配置副本集
  replSetName: "rs1"	#设置副本集名
  oplogSizeMB: 2048	#设置oplog日志的大小

5.修改MongoDB从库配置文件

[root@db-slave ~]# vim /etc/mongod.conf
#配置文件并不完整,仅展示修改部分
net:
  port: 27017
  bindIp: 0.0.0.0	#修改监听地址为所有地址
  
#在文件末尾添加以下内容
replication:	#配置副本集
  replSetName: "rs1"	#设置副本集名,需要和主库名字一样

6.启动MongoDB数据库

[root@db-master ~]# mongod -f /etc/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 2209
child process started successfully, parent exiting

[root@db-slave ~]# mongod -f /etc/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 1968
child process started successfully, parent exiting

三、配置主从连接

1.登录MongoDB主库

[root@db-master ~]# mongo --host 127.0.0.1 --port 27017
  • --host 指定数据库连接地址
  • --port 指定数据库连接端口

2.在主库初始化复制集

> rs.initiate()

{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "db-master.linux.com:27017",
	"ok" : 1
}

3.加入从库到主库

rs.add("从库地址:端口")
rs1:OTHER> rs.add("192.168.140.14:27017")
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1689596493, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1689596493, 1)
}

4.查看Master主节点信息

rs1:PRIMARY> rs.isMaster()

{
	"hosts" : [
		"db-master.linux.com:27017",
		"192.168.140.14:27017"
	],
	"setName" : "rs1",
	"setVersion" : 2,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "db-master.linux.com:27017",	#主库地址
	"me" : "db-master.linux.com:27017",
	"electionId" : ObjectId("7fffffff0000000000000001"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1689596607, 1),
			"t" : NumberLong(1)
		},
		"lastWriteDate" : ISODate("2023-07-17T12:23:27Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2023-07-17T12:23:27.615Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"connectionId" : 1,
	"minWireVersion" : 0,
	"maxWireVersion" : 8,
	"readOnly" : false,
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1689596607, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1689596607, 1)
}

5.查看各个节点信息

rs1:PRIMARY> rs.status()

{
	"set" : "rs1",
	"date" : ISODate("2023-07-17T12:24:34.793Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1689596487, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2023-07-17T12:21:27.338Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1689596667, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1689596667, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2023-07-17T12:24:27.353Z"),
		"lastDurableWallTime" : ISODate("2023-07-17T12:24:27.353Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1689596487, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1689596487, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2023-07-17T12:20:37.325Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1689596437, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 1,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"newTermStartDate" : ISODate("2023-07-17T12:20:37.333Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2023-07-17T12:20:37.337Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "db-master.linux.com:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",	#活跃节点,就是主节点
			"uptime" : 416,
			"optime" : {
				"ts" : Timestamp(1689596667, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-07-17T12:24:27Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1689596437, 2),
			"electionDate" : ISODate("2023-07-17T12:20:37Z"),
			"configVersion" : 2,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.140.14:27017",
			"health" : 1,
			"state" : 0,
			"stateStr" : "STARTUP",	#备份节点
			"uptime" : 181,
			"optime" : {
				"ts" : Timestamp(0, 0),
				"t" : NumberLong(-1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(0, 0),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
			"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
			"lastHeartbeat" : ISODate("2023-07-17T12:24:33.619Z"),
			"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : -2
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1689596667, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1689596667, 1)
}