Mongodb3.4分片搭建

1.前言

找了很多書,發現對分片的搭建還是有模糊,所以這一次記一下。

2.搭建環境

一共四臺主機

ip 職能
192.168.0.124 config、mongos
192.168.0.125 share1
192.168.0.126 share2
192.168.0.127 share3

他們的結構是這樣的


是很簡單的一個架構。

3.開始配置

  • 先配置三臺share2,我用的是mongodb3.4,在0.125、0.126、0.127三臺主機上敲以下命令
[root@linux]#mkdir /share/shaer125
[root@linux]#mongod -shardsvr -port 27017 -dbpath=/share/share125 -logpath=/share/share125.log
  • 記得要把125給替換一下,啓動不成功請自己去排查原因
  • 然後在config和mongos服務器下敲以下命令,首先先配置管理配置服務器。
mkdir /data/config
mongod --configsvr -port 20000 --dbpath=/data/config --logpath=/data/config.log

  • 然後配置我們的mongos服務器,就是主控端
mongos -configdb 192.168.0.124:20000 --pot 27019 --logpath=/data/mongos.log

  • 到目前爲止,我們四臺服務器的mongodb都已經啓動成功了,然後要鏈接mongos,去將分片連接起來,開啓一個mongo窗口,連接124.然後執行添加分片命令
sh.addShard('192.168.0.125:27017')
sh.addShard('192.168.0.126:27017')
sh.addShard('192.168.0.127:27017')
  • 然後執行db.printShardingStatus();可發現,已經添加了分片機
--- Sharding Status --- 
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("585ba088d6d7ae8dc748cb21")
}
  shards:
	{  "_id" : "shard0000",  "host" : "192.168.0.125:27017" }
	{  "_id" : "shard0001",  "host" : "192.168.0.126:27017" }
	{  "_id" : "shard0002",  "host" : "192.168.0.127:27017" }
  active mongoses:
	"3.2.11" : 1
  balancer:
	Currently enabled:  yes
	Currently running:  no
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours: 
		No recent migrations

  • 然後我們要給我們的集合添加數據庫和聚合了。
sh.enableSharding("testdb");
{"ok":1}
sh.shardCollection("testdb.testcollection",{testkey:1})
  • 以上就是首先分片testdb數據庫,然後對這個數據庫中的testcollection這個聚合中的testkey鍵值添加分片的參數。
  • 執行完以後,使用db.printShardingStatus();查看
--- Sharding Status --- 
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("585ba088d6d7ae8dc748cb21")
}
  shards:
	{  "_id" : "shard0000",  "host" : "192.168.0.125:27017" }
	{  "_id" : "shard0001",  "host" : "192.168.0.126:27017" }
	{  "_id" : "shard0002",  "host" : "192.168.0.127:27017" }
  active mongoses:
	"3.2.11" : 1
  balancer:
	Currently enabled:  yes
	Currently running:  no
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours: 
		No recent migrations
  databases:
	{  "_id" : "testdb",  "primary" : "shard0000",  "partitioned" : true }
		testdb.testcolleciton
			shard key: { "testkey" : 1 }
			unique: false
			balancing: true
			chunks:
				shard0000	1
			{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 

到這裏就基本的配置完畢,後期還要設置索引。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章