Linux下安装MongoDB&&MongoDB集群

Linux下安装MongoDB&&MongoDB集群

1.环境准备

1.1整体结构-拓扑图

手工制图,请不要吐槽,谢谢!!!

1.2资源清单-虚拟机$$安装包

parthostnameipos
masternode4192.168.171.128centos7.6
slavenode5192.168.171.129centos7.6
slavenode6192.168.171.130centos7.6

MongoDB下载位置:https://www.mongodb.com/download-center/community
我下载的最新的稳定版//我下载的最新的稳定版。版本如↑图。

2.实施

2.1安装启动DB

//三台DB操作一致,这里只展示一台的安装启动。

root@node6~$>tar xf mongodb-linux-x86_64-rhel70-4.2.3.tgz -C /opt/
root@node6~$>cd /opt/
root@node6/opt$>mv mongodb-linux-x86_64-rhel70-4.2.3/ mongodb
root@node6/opt$>cd mongodb/
root@node6/opt/mongodb$>vi mongo.conf
	

## //mongo.conf内容:

	dbpath=/opt/mongodb/data  //DB数据位置
	logpath=/opt/mongodb/mongo.log  //日志位置
	port=27017  //监听端口
	bind_ip=192.168.171.128  //监听ip
	fork=true  //后台启动
	
root@node6/opt/mongodb$>mkdir data
root@node6/opt/mongodb$>./bin/mongod -f mongo.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 1671
child process started successfully, parent exiting

//备注:DB的数据位置需要自行创建,不建可能会报错;日志位置不用创建,创建可能会报错。

2.2主节点配置

//主节点建立节点信息并插入数据,以供后续验证。

root@node4/opt/mongodb$>./bin/mongo --host 192.168.171.128 --port 27017
MongoDB shell version v4.2.3
connecting to: mongodb://192.168.171.128:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0fa24b0d-ee2c-456f-9580-6a5c0446f9b9") }
MongoDB server version: 4.2.3
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

Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> rs.initiate({_id:"test",members:[{_id:0,host:"192.168.171.128"},{_id:1,host:"192.168.171.129"},{_id:2,host:"192.168.171.130"}]})  //配置节点信息
test:PRIMARY> rs.status();  //查看节点信息
test:PRIMARY> rs.status(); //查看节点状态
{
	"set" : "test",
	"date" : ISODate("2020-03-17T09:02:52.307Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1584435762, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-03-17T09:02:42.906Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1584435762, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-03-17T09:02:42.906Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1584435762, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1584435762, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-03-17T09:02:42.906Z"),
		"lastDurableWallTime" : ISODate("2020-03-17T09:02:42.906Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1584435712, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1584435712, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-03-17T07:02:51.558Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1584428561, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-03-17T07:02:52.440Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-03-17T07:02:52.993Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.171.128:27017",
			"health" : 1,
			"state" : 1, 

## //主节点状态为PRIMARY

			"stateStr" : "PRIMARY",     
			"uptime" : 9991,
			"optime" : {
				"ts" : Timestamp(1584435762, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-03-17T09:02:42Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1584428571, 1),
			"electionDate" : ISODate("2020-03-17T07:02:51Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.171.129:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 7210,
			"optime" : {
				"ts" : Timestamp(1584435762, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1584435762, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-03-17T09:02:42Z"),
			"optimeDurableDate" : ISODate("2020-03-17T09:02:42Z"),
			"lastHeartbeat" : ISODate("2020-03-17T09:02:51.548Z"),
			"lastHeartbeatRecv" : ISODate("2020-03-17T09:02:50.672Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.171.128:27017",
			"syncSourceHost" : "192.168.171.128:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.171.130:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 7210,
			"optime" : {
				"ts" : Timestamp(1584435762, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1584435762, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-03-17T09:02:42Z"),
			"optimeDurableDate" : ISODate("2020-03-17T09:02:42Z"),
			"lastHeartbeat" : ISODate("2020-03-17T09:02:51.549Z"),
			"lastHeartbeatRecv" : ISODate("2020-03-17T09:02:50.679Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.171.128:27017",
			"syncSourceHost" : "192.168.171.128:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1584435762, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1584435762, 1)
test:PRIMARY> use node
switched to db node
test:PRIMARY> db.user.insert({"id":2});
WriteResult({ "nInserted" : 1 })
test:PRIMARY> db.user.find()
{ "_id" : ObjectId("5e708f6cff87fe80dbdd0827"), "id" : 2 }
test:PRIMARY> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
node    0.000GB

2.3从节点配置

//两台从节点配置一致,这里只展示一台。配置完数据验证。

root@node5/opt/mongodb$>./bin/mongo -host 192.168.171.129 -port 27017
MongoDB shell version v4.2.3
connecting to: mongodb://192.168.171.129:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0dfc9a85-85f7-4c8c-a9fd-e7d34c7068fb") }
MongoDB server version: 4.2.3
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

Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> rs.slaveOk()
test:SECONDARY> show dbs
test:SECONDARY> use node
switched to db node
test:SECONDARY> db.user.find()
{ "_id" : ObjectId("5e708f6cff87fe80dbdd0827"), "id" : 2 }

2.4主节点宕机,检查目前节点状态

//主节点查看集群信息的时候node4("_id" : 0)的状态为:PRIMARY。

root@node4/opt/mongodb$>ps aux | grep mongod
root       1830  0.5 17.1 1934288 82108 ?       Sl   14:16   1:00 ./bin/mongod -f mongo.conf
root      61730  0.0  0.2 112724   988 pts/0    S+   17:09   0:00 grep --color=auto mongod
root@node4/opt/mongodb$>kill 1830
root@node4/opt/mongodb$>ps aux | grep mongod
root      61735  0.0  0.2 112724   992 pts/0    S+   17:09   0:00 grep --color=auto mongod
root@node4/opt/mongodb$>./bin/mongod -f mongo.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 61740
child process started successfully, parent exiting
root@node4/opt/mongodb$>./bin/mongo --host 192.168.171.128 --port 27017
MongoDB shell version v4.2.3
connecting to: mongodb://192.168.171.128:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("85306530-c418-4323-8607-85fb381e914d") }
MongoDB server version: 4.2.3

Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

test:SECONDARY> rs.status();
{
	"set" : "test",
	"date" : ISODate("2020-03-17T09:09:38.313Z"),
	"myState" : 2,
	"term" : NumberLong(2),
	"syncingTo" : "192.168.171.130:27017",
	"syncSourceHost" : "192.168.171.130:27017",
	"syncSourceId" : 2,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1584436171, 1),
			"t" : NumberLong(2)
		},
		"lastCommittedWallTime" : ISODate("2020-03-17T09:09:31.940Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1584436171, 1),
			"t" : NumberLong(2)
		},
		"readConcernMajorityWallTime" : ISODate("2020-03-17T09:09:31.940Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1584436171, 1),
			"t" : NumberLong(2)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1584436171, 1),
			"t" : NumberLong(2)
		},
		"lastAppliedWallTime" : ISODate("2020-03-17T09:09:31.940Z"),
		"lastDurableWallTime" : ISODate("2020-03-17T09:09:31.940Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1584436142, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1584436142, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.171.128:27017",
			"health" : 1,
			"state" : 2,
			

## //原主节点现在状态为SECONDARY

			"stateStr" : "SECONDARY",
			"uptime" : 13,
			"optime" : {
				"ts" : Timestamp(1584436171, 1),
				"t" : NumberLong(2)
			},
			"optimeDate" : ISODate("2020-03-17T09:09:31Z"),
			"syncingTo" : "192.168.171.130:27017",
			"syncSourceHost" : "192.168.171.130:27017",
			"syncSourceId" : 2,
			"infoMessage" : "",
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.171.129:27017",
			"health" : 1,
			"state" : 1,  
			"stateStr" : "PRIMARY",
			"uptime" : 11,
			"optime" : {
				"ts" : Timestamp(1584436171, 1),
				"t" : NumberLong(2)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1584436171, 1),
				"t" : NumberLong(2)
			},
			"optimeDate" : ISODate("2020-03-17T09:09:31Z"),
			"optimeDurableDate" : ISODate("2020-03-17T09:09:31Z"),
			"lastHeartbeat" : ISODate("2020-03-17T09:09:37.851Z"),
			"lastHeartbeatRecv" : ISODate("2020-03-17T09:09:37.682Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1584436151, 1),
			"electionDate" : ISODate("2020-03-17T09:09:11Z"),
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.171.130:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 11,
			"optime" : {
				"ts" : Timestamp(1584436171, 1),
				"t" : NumberLong(2)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1584436171, 1),
				"t" : NumberLong(2)
			},
			"optimeDate" : ISODate("2020-03-17T09:09:31Z"),
			"optimeDurableDate" : ISODate("2020-03-17T09:09:31Z"),
			"lastHeartbeat" : ISODate("2020-03-17T09:09:37.851Z"),
			"lastHeartbeatRecv" : ISODate("2020-03-17T09:09:36.882Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.171.129:27017",
			"syncSourceHost" : "192.168.171.129:27017",
			"syncSourceId" : 1,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1584436171, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1584436171, 1)
}

//主节点宕机之后其中一个从节点自动上升为主节点。

3.埋坑

//我在MongoDB安装和集群配置的过程中发现一些问题,在此感谢各位大佬提供的宝贵资料给与我莫大的帮助。在这里我写出一些水坑,希望大家借鉴的时候避免入坑。

3.1最新版本不支持主从架构

从网上搜来的资料看,目前可以肯定的一点是4.0版本以上启动的时候如果加上一些关于主从配置的信息就会报错。

root@node4/opt/mongodb$>./bin/mongo --host 192.168.171.128 --port 27017
MongoDB shell version v4.2.3
connecting to: mongodb://192.168.171.128:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-03-17T17:21:53.860+0800 E  QUERY    [js] Error: couldn't connect to server 192.168.171.128:27017, connection attempt failed: SocketException: Error connecting to 192.168.171.128:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-03-17T17:21:53.862+0800 F  -        [main] exception: connect failed
2020-03-17T17:21:53.862+0800 E  -        [main] exiting with code 1
root@node4/opt/mongodb$>./bin/mongod --dbpath=/opt/mongodb/data --port=27017 --master
2020-03-17T17:25:22.825+0800 F  CONTROL  [main] Master/slave replication is no longer supported

解决办法:
1.在建立mongo.conf时不要加入:master=true
2.启动是不加“–master”参数

**后续会持续更新文章,感谢大家的支持。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值