Centos下安裝MongoDB複製集

在一臺Centos下模擬安裝MongoDB的複製集mongodb

準備三份配置文件:shell

mongod.confapp

bind_ip=0.0.0.0
port = 27017
dbpath = /usr/local/mongo/data/
logpath = /usr/local/mongo/log/mongod.log
fork=true
logappend=true
replSet=myMongoSet

mongod2.confui

bind_ip=0.0.0.0
port = 27018
dbpath = /usr/local/mongo/data2/
logpath = /usr/local/mongo/log2/mongod.log
fork=true
logappend=true
replSet=myMongoSet

mongod3.confthis

bind_ip=0.0.0.0
port = 27019
dbpath = /usr/local/mongo/data3/
logpath = /usr/local/mongo/log3/mongod.log
fork=true
logappend=true
replSet=myMongoSet

進入bin目錄下分別啓動三臺mongo實例spa

./mongod -f ../conf/mongod.conf 
./mongod -f ../conf/mongod2.conf 
./mongod -f ../conf/mongod3.conf

查看進程,驗證三臺MongoDB實例是否啓動成功rest

[root@192 conf]# ps -ef | grep mongod
root        559 130632  0 14:53 pts/1    00:00:00 grep --color=auto mongod
root     130957      1  0 14:37 ?        00:00:04 ./mongod -f ../conf/mongod.conf
root     130986      1  0 14:37 ?        00:00:04 ./mongod -f ../conf/mongod2.conf
root     131014      1  0 14:37 ?        00:00:04 ./mongod -f ../conf/mongod3.conf

說明三臺MongoDB實例已經啓動成功code

鏈接第一臺mongoserver

./mongo 192.168.15.31:27017/admin

準備初始化實例進程

> config={_id:"myMongoSet",members:[{_id:0,host:"192.168.15.31:27017"},{_id:1,host:"192.168.15.31:27018"},{_id:2,host:"192.168.15.31:27019"}]}
{
	"_id" : "myMongoSet",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.15.31:27017"
		},
		{
			"_id" : 1,
			"host" : "192.168.15.31:27018"
		},
		{
			"_id" : 2,
			"host" : "192.168.15.31:27019"
		}
	]
}

定義config內容爲集羣的實例信息

> rs.initiate(config)

初始化複製集

執行後輸出以下:

{
	"ok" : 1,
	"operationTime" : Timestamp(1517640358, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1517640358, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

說明MongoDB的複製集建立成功

查看複製集狀態

myMongoSet:SECONDARY> rs.status()
{
	"set" : "myMongoSet",
	"date" : ISODate("2018-02-03T06:46:09.449Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1517640358, 1),
			"t" : NumberLong(-1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1517640358, 1),
			"t" : NumberLong(-1)
		}
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.15.31:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 546,
			"optime" : {
				"ts" : Timestamp(1517640358, 1),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("2018-02-03T06:45:58Z"),
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1517640368, 1),
			"electionDate" : ISODate("2018-02-03T06:46:08Z"),
			"configVersion" : 1,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "192.168.15.31:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 10,
			"optime" : {
				"ts" : Timestamp(1517640358, 1),
				"t" : NumberLong(-1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1517640358, 1),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("2018-02-03T06:45:58Z"),
			"optimeDurableDate" : ISODate("2018-02-03T06:45:58Z"),
			"lastHeartbeat" : ISODate("2018-02-03T06:46:08.931Z"),
			"lastHeartbeatRecv" : ISODate("2018-02-03T06:46:05.733Z"),
			"pingMs" : NumberLong(0),
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.15.31:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 10,
			"optime" : {
				"ts" : Timestamp(1517640358, 1),
				"t" : NumberLong(-1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1517640358, 1),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("2018-02-03T06:45:58Z"),
			"optimeDurableDate" : ISODate("2018-02-03T06:45:58Z"),
			"lastHeartbeat" : ISODate("2018-02-03T06:46:08.932Z"),
			"lastHeartbeatRecv" : ISODate("2018-02-03T06:46:05.734Z"),
			"pingMs" : NumberLong(0),
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1517640358, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1517640368, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

驗證數據同步:

鏈接27017實例:

myMongoSet:PRIMARY>use stu
myMongoSet:PRIMARY> db.stuinfo.insert({"name":"張三","age":12,"address":"山東"})

查詢添加的內容:

myMongoSet:PRIMARY> db.stuinfo.find()
{ "_id" : ObjectId("5a755b6a4dfb8ddaa17bad20"), "name" : "張三", "age" : 12, "address" : "山東" }

進入27018實例:

myMongoSet:SECONDARY> db.stuinfo.find()
Error: error: {
	"operationTime" : Timestamp(1517641210, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1517641210, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

查看信息,可能會報如上問題,這是由於MongoDB的salve默認是不容許讀寫的。 解決方法:

myMongoSet:SECONDARY> rs.slaveOk()

查看信息是否同步:

myMongoSet:SECONDARY> use stu
switched to db stu
myMongoSet:SECONDARY> db.stuinfo.find()
{ "_id" : ObjectId("5a755b6a4dfb8ddaa17bad20"), "name" : "張三", "age" : 12, "address" : "山東" }

鏈接實例27019,執行一樣的操做。 因而可知,數據信息能夠正確同步, 到此爲止,MongoDB的複製集搭建成功

驗證主從切換:

如今主節點是27017節點,咱們殺掉該進程

[root@192 bin]# ps -ef | grep mongod
root        659 130632  0 15:05 pts/1    00:00:00 grep --color=auto mongod
root     130957      1  0 14:37 ?        00:00:08 ./mongod -f ../conf/mongod.conf
root     130986      1  0 14:37 ?        00:00:08 ./mongod -f ../conf/mongod2.conf
root     131014      1  0 14:37 ?        00:00:08 ./mongod -f ../conf/mongod3.conf
[root@192 bin]# kill 130957
[root@192 bin]# ps -ef | grep mongod
root        674 130632  0 15:05 pts/1    00:00:00 grep --color=auto mongod
root     130986      1  0 14:37 ?        00:00:08 ./mongod -f ../conf/mongod2.conf
root     131014      1  0 14:37 ?        00:00:08 ./mongod -f ../conf/mongod3.conf

該進程已經被殺掉

而後鏈接27018節點

[root@192 bin]# ./mongo 192.168.15.31:27018
MongoDB shell version v3.6.2
connecting to: mongodb://192.168.15.31:27018/test
MongoDB server version: 3.6.2
Server has startup warnings: 
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] 
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] 
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] 
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] 
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-02-03T14:37:09.007+0800 I CONTROL  [initandlisten] 
myMongoSet:PRIMARY>

能夠看出,27018節點已經成爲主節點

而後從新開啓27017節點

[root@192 bin]# ./mongod -f ../conf/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 703
child process started successfully, parent exiting

鏈接27017節點

[root@192 bin]# ./mongo 192.168.15.31:27017
MongoDB shell version v3.6.2
connecting to: mongodb://192.168.15.31:27017/test
MongoDB server version: 3.6.2
Server has startup warnings: 
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] 
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] 
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] 
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] 
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-02-03T15:07:54.488+0800 I CONTROL  [initandlisten] 
myMongoSet:SECONDARY>

27017節點已經成爲從節點

相關文章
相關標籤/搜索