搭建mongodb集群(副本集+分片)

Posted yfceshi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建mongodb集群(副本集+分片)相关的知识,希望对你有一定的参考价值。

完整的搭建mongodb集群(副本集+分片)的样例。。。

准备四台机器,各自是bluejoe1,bluejoe2,bluejoe3,以及bluejoe0

副本集及分片策略确定例如以下:

  • 将创建3个副本集,命名为shard1,shard2,shard3;
  • 以上3个副本集作为3个分片;
  • 每一个副本集包括3个副本(主、辅1、辅2);
  • 副本分开存储,即shard1存在bluejoe1、bluejoe2、bluejoe3上各一份。。。

    以此类推

  • 将创建3个配置库实例,一台机器一个
  • bluejoe0上配置一个mongos(mongos一般能够配置在应用端)
画了一个图:

技术分享

详细操作过程例如以下:
  1. 在bluejoe1上下载mongdb安装包。
  2. 解压至/usr/local/mongdb(注意改名)。
  3. 创建data/db1,db2,db3文件夹,启动3个mongod实例,注意replSet的名字:
    mkdir /usr/local/mongodb/data/db1 /usr/local/mongodb/data/db2 /usr/local/mongodb/data/db3
    nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/db1 --port 10001 --replSet db1 --logpath=/usr/local/mongodb/log/db1.log &
    nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/db2 --port 10002 --replSet db2 --logpath=/usr/local/mongodb/log/db2.log &
    nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/db3 --port 10003 --replSet db3 --logpath=/usr/local/mongodb/log/db3.log &
    

  4. 使用scp命令。将mongodb文件夹复制至bluejoe2和bluejoe3机器,并依照第3步启动每台机器上的3个实例;
  5. 初始化副本:
    mongo bluejoe1:10001
    
    	use admin
    	
    	db.runCommand(
    	{
    		"replSetInitiate":
    		{
    			"_id":"db1",
    			"members":
    			[
    				{
    					"_id":1,
    					"host":"bluejoe1:10001"
    				},
    				{
    					"_id":2,
    					"host":"bluejoe2:10001"
    				},
    				{
    					"_id":3,
    					"host":"bluejoe3:10001"
    				}
    			]
    		}
    	})
    	
    
    mongo bluejoe1:10002
    
    	use admin
    	
    	db.runCommand(
    	{
    		"replSetInitiate":
    		{
    			"_id":"db2",
    			"members":
    			[
    				{
    					"_id":1,
    					"host":"bluejoe1:10002"
    				},
    				{
    					"_id":2,
    					"host":"bluejoe2:10002"
    				},
    				{
    					"_id":3,
    					"host":"bluejoe3:10002"
    				}
    			]
    		}
    	})
    	
    mongo bluejoe1:10003
    
    	use admin
    	
    	db.runCommand(
    	{
    		"replSetInitiate":
    		{
    			"_id":"db3",
    			"members":
    			[
    				{
    					"_id":1,
    					"host":"bluejoe1:10003"
    				},
    				{
    					"_id":2,
    					"host":"bluejoe2:10003"
    				},
    				{
    					"_id":3,
    					"host":"bluejoe3:10003"
    				}
    			]
    		}
    	})

  6. 观察副本集的实时复制特性
    1. 连接上bluejoe1:10001。看到db1:PRIMARY>的字样,说明它是db1的主节点
    2. 创建一张新表。创建几条測试记录
    3. 连接上bluejoe3:10002。看到shard1:SECONDARY>的字样,说明它是shard1的辅助节点
    4. 注意此时无法查询前面新增的记录,会报错not master and slaveOk=false
    5. 回到bluejoe1:10001,设置slaveOk
      db.getMongo().setSlaveOk()
    6. 再回到bluejoe3:10002,就可以看到主节点写入的记录
  • 观察副本集的故障转移特性
    1. 杀掉bluejoe1:10001的进程
    2. 再次连接上bluejoe3:10002。看到shard1:PRIMARY>的字样,说明它已经变成shard1的主节点
    3. 假设这时再启动bluejoe1:10001,则发现它已经变成shard1:SECONDARY>
  • 创建data/configdb。启动配置库实例:
    mkdir /usr/local/mongodb/data/configdb
    nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/configdb --port 20000 --logpath=/usr/local/mongodb/log/configdb.log &

    假设须要关闭mongod服务,可使用--shutdown选项。如:
    /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/configdb --shutdown

    到如今为止应该有12个mongd实例。当中3个为配置库实例,剩下的属于3个副本集。
  • 接下来管理分片,在bluejoe0上启动mongs:

    nohup /usr/local/mongodb/bin/mongos --port 30000 --configdb bluejoe1:20000,bluejoe2:20000,bluejoe3:20000 &
  • 连接上mongos,配置分片信息:
    mongo bluejoe0:30000
    
    mongos> use admin
    switched to db admin
    	
    db.runCommand({"addShard":"db1/bluejoe1:10001"})
    db.runCommand({"addShard":"db2/bluejoe1:10002"})
    db.runCommand({"addShard":"db3/bluejoe1:10003"})
  • 查看分片情况:
    mongos>  db.runCommand({listshards:1})
    {
    	"shards" : [
    		{
    			"_id" : "db1",
    			"host" : "db1/bluejoe1:10001,bluejoe2:10001,bluejoe3:10001"
    		},
    		{
    			"_id" : "db2",
    			"host" : "db2/bluejoe1:10002,bluejoe2:10002,bluejoe3:10002"
    		},
    		{
    			"_id" : "db3",
    			"host" : "db3/bluejoe1:10003,bluejoe2:10003,bluejoe3:10003"
    		}
    	],
    	"ok" : 1
    }
    能够看出,虽然注冊的时候仅仅是提供了副本集的主节点,但mongos已知晓了各辅助节点;

  • 对某个库开启分片功能:
    mongos> db.runCommand({"enablesharding":"test"})
    { "ok" : 1 }
    mongos> db.runCommand({"shardcollection":"test.person","key":{_id:‘hashed‘}})
    { "collectionsharded" : "test.person", "ok" : 1 }
  • 插入測试数据:
    mongos> for(var i=0;i<10;i++){db.person.insert({name:"bluejoe"+i});}
    WriteResult({ "nInserted" : 1 })
  • 查看数据分片存储情况:
    [[email protected] ~]# mongo bluejoe3:10002
    MongoDB shell version: 2.6.5
    connecting to: bluejoe3:10002/test
    shard1:PRIMARY> db.person.find()
    { "_id" : ObjectId("546ff1013a0d0ec68b42a914"), "name" : "bluejoe0" }
    { "_id" : ObjectId("546ff1013a0d0ec68b42a915"), "name" : "bluejoe1" }
    { "_id" : ObjectId("546ff1013a0d0ec68b42a91a"), "name" : "bluejoe6" }
    { "_id" : ObjectId("546ff1013a0d0ec68b42a91b"), "name" : "bluejoe7" }
    { "_id" : ObjectId("546ff1013a0d0ec68b42a91c"), "name" : "bluejoe8" }
    shard1:PRIMARY> exit
    bye
    [[email protected] ~]# mongo bluejoe2:10001
    MongoDB shell version: 2.6.5
    connecting to: bluejoe2:10001/test
    shard2:PRIMARY> db.person.find()
    { "_id" : ObjectId("546ff1013a0d0ec68b42a918"), "name" : "bluejoe4" }
    shard2:PRIMARY> exit
    bye
    [[email protected] ~]# mongo bluejoe3:10001
    MongoDB shell version: 2.6.5
    connecting to: bluejoe3:10001/test
    shard3:PRIMARY> db.person.find()
    { "_id" : ObjectId("546ff1013a0d0ec68b42a916"), "name" : "bluejoe2" }
    { "_id" : ObjectId("546ff1013a0d0ec68b42a917"), "name" : "bluejoe3" }
    { "_id" : ObjectId("546ff1013a0d0ec68b42a919"), "name" : "bluejoe5" }
    { "_id" : ObjectId("546ff1013a0d0ec68b42a91d"), "name" : "bluejoe9" }
    shard3:PRIMARY> 

























  • 以上是关于搭建mongodb集群(副本集+分片)的主要内容,如果未能解决你的问题,请参考以下文章

    Mongodb 副本集+分片集群搭建

    mongodb 3.4 集群搭建:分片+副本集

    MongoDB分片集群搭建

    mongodb 集群搭建 分片+副本集

    mongoDB副本集+分片集群

    MongoDB 高可用集群副本集+分片搭建