【Mongodb】集群分片环境mongodb的版本升级

生产环境数据量的剧增,鉴于2.6版本优化了存储引擎,提高了写入的速度,特提出了升级Mongo的需求

目前环境中最大的一个集合分为6个片,且每片均有主节点,副节点和仲裁节点,本次升级为不停服务升级,

升级的版本有2.4.10升级至2.6.2版本,特别需要提醒的是升级前请做好备份,升级一旦失败无法回退。

<span style=”font-family: 宋体; mso-ascii-font-family: Cambria; mso-ascii-theme-font: <br />

major-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: major-fareast; <br />
mso-hansi-font-family: Cambria; mso-hansi-theme-font: major-latin;”>

####

1.升级过程中不允许有以下操作

  1. <span class="pln">sh</span><span class="pun">.</span><span class="pln">enableSharding</span><span class="pun">()</span>
  2. sh.shardCollection()

  3. sh.addShard()

  4. <span class="pln">db</span><span class="pun">.</span><span class="pln">createCollection</span><span class="pun">()</span>

  5. <span class="pln">db</span><span class="pun">.</span><span class="pln">collection</span><span class="pun">.</span><span class="pln">drop</span><span class="pun">()</span>
  6. <span class="pln">db</span><span class="pun">.</span><span class="pln">dropDatabase</span><span class="pun">()</span>
  7. <span class="pln">any operation that creates a database</span>
  8. <span class="pln">any other operation that modifies the cluster metadata in any way</span><span class="pun">.</span><span class="typ">See</span><span class="typ">Sharding</span><span class="typ">Reference</span><span class="kwd">for</span><span class="pln"> a complete list of sharding commands</span><span class="pun">.</span><span class="typ">Note</span><span class="pun">,</span><span class="pln"> however</span><span class="pun">,</span><span class="pln"> that not all commands on the </span><span class="typ">Sharding</span><span class="typ">Reference</span><span class="pln"> page modifies the cluster meta</span><span class="pun">-</span><span class="pln">data</span><span class="pun">.</span>

    2,有升级后无法降级

<span style=”font-family: 宋体; mso-ascii-font-family: Cambria; mso-ascii-theme-font: <br />

major-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: major-fareast; <br />
mso-hansi-font-family: Cambria; mso-hansi-theme-font: major-latin;”>

1. 安装,解压到/home/Mongo下,关于Mongo集群环境的搭建详见《【Mongodb】MongoDB分片备份策略与配置迁移示例

2. 建脚本库存放于文件夹script中,脚本库中权限与内容如下



1. <span class="pun">-</span><span class="pln">rwxrwxrwx </span><span class="lit">1</span><span class="typ">Mongo</span><span class="typ">Mongodb</span><span class="lit">179</span><span class="typ">Mar</span><span class="lit">22</span><span class="lit">09</span><span class="pun">:</span><span class="lit">05</span><span class="pln"> startconfig</span><span class="pun">.</span><span class="pln">sh</span>
2.3. <span class="pun">-</span><span class="pln">rwxrwxrwx </span><span class="lit">1</span><span class="typ">Mongo</span><span class="typ">Mongodb</span><span class="lit">191</span><span class="typ">Mar</span><span class="lit">22</span><span class="lit">09</span><span class="pun">:</span><span class="lit">05</span><span class="pln"> startmongos</span><span class="pun">.</span><span class="pln">sh</span>
4.5. <span class="pun">-</span><span class="pln">rwxrwxrwx </span><span class="lit">1</span><span class="typ">Mongo</span><span class="typ">Mongodb</span><span class="lit">169</span><span class="typ">Mar</span><span class="lit">22</span><span class="lit">09</span><span class="pun">:</span><span class="lit">05</span><span class="pln"> startshard1</span><span class="pun">.</span><span class="pln">sh</span>
6.7. <span class="pun">-</span><span class="pln">rwxrwxrwx </span><span class="lit">1</span><span class="typ">Mongo</span><span class="typ">Mongodb</span><span class="lit">169</span><span class="typ">Mar</span><span class="lit">22</span><span class="lit">09</span><span class="pun">:</span><span class="lit">05</span><span class="pln"> startshard2</span><span class="pun">.</span><span class="pln">sh</span>
8.9. <span class="pun">-</span><span class="pln">rwxrwxrwx </span><span class="lit">1</span><span class="typ">Mongo</span><span class="typ">Mongodb</span><span class="lit">169</span><span class="typ">Mar</span><span class="lit">22</span><span class="lit">09</span><span class="pun">:</span><span class="lit">05</span><span class="pln"> startshard3</span><span class="pun">.</span><span class="pln">sh</span>

文件中的内容(此处可以选择配置环境变量)



1. **<span class="pln">startconfig</span><span class="pun">.</span><span class="pln">sh</span>**
2.3. <span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="typ">Mongo</span><span class="pun">/</span><span class="pln">mongodb</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x86_64</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mongod </span><span class="pun">-</span><span class="pln">configsvr </span><span class="pun">-</span><span class="pln">port </span><span class="lit">20000</span><span class="pun">-</span><span class="pln">fork </span><span class="pun">-</span><span class="pln">dbpath </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="typ">Mongo</span><span class="pun">/</span><span class="pln">mongodb</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x86_64</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">config </span><span class="pun">-</span><span class="pln">logpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">dblog</span><span class="pun">/</span><span class="pln">config</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">log </span><span class="pun">--</span><span class="pln">upgrade</span>
4.5. **<span class="pln">startmongos</span><span class="pun">.</span><span class="pln">sh</span>**
6.7. <span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="typ">Mongo</span><span class="pun">/</span><span class="pln">mongodb</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x86_64</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mongos </span><span class="pun">-</span><span class="pln">fork </span><span class="pun">-</span><span class="pln">port </span><span class="lit">30000</span><span class="pun">-</span><span class="pln">configdb </span><span class="str">"192.168.199.133:20000,192.168.199.134:20000,192.168.199.135:20000"</span><span class="pun">-</span><span class="pln">logpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">dblog</span><span class="pun">/</span><span class="pln">mongos</span><span class="pun">/</span><span class="pln">mongos</span><span class="pun">.</span><span class="pln">log</span>
8.9. **<span class="pln">startshard1</span><span class="pun">.</span><span class="pln">sh</span>**
10.11. <span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="typ">Mongo</span><span class="pun">/</span><span class="pln">mongodb</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x86_64</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mongod </span><span class="pun">-</span><span class="pln">fork </span><span class="pun">-</span><span class="pln">shardsvr </span><span class="pun">-</span><span class="pln">replSet shard1 </span><span class="pun">-</span><span class="pln">port </span><span class="lit">10001</span><span class="pun">-</span><span class="pln">dbpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">db</span><span class="pun">/</span><span class="pln">shard1 </span><span class="pun">-</span><span class="pln">logpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">dblog</span><span class="pun">/</span><span class="pln">shard1</span><span class="pun">/</span><span class="pln">shard1</span><span class="pun">.</span><span class="pln">log</span>
12.13. **<span class="pln">startshard2</span><span class="pun">.</span><span class="pln">sh</span>**
14.15. <span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="typ">Mongo</span><span class="pun">/</span><span class="pln">mongodb</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x86_64</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mongod </span><span class="pun">-</span><span class="pln">fork </span><span class="pun">-</span><span class="pln">shardsvr </span><span class="pun">-</span><span class="pln">replSet shard2 </span><span class="pun">-</span><span class="pln">port </span><span class="lit">10002</span><span class="pun">-</span><span class="pln">dbpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">db</span><span class="pun">/</span><span class="pln">shard2 </span><span class="pun">-</span><span class="pln">logpath </span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">dblog</span><span class="pun">/</span><span class="pln">shard2</span><span class="pun">/</span><span class="pln">shard2</span><span class="pun">.</span><span class="pln">log</span>
16.17. **<span class="pln">startshard3</span><span class="pun">.</span><span class="pln">sh</span>**
18.19. <span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="typ">Mongo</span><span class="pun">/</span><span class="pln">mongodb</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x86_64</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mongod </span><span class="pun">-</span><span class="pln">fork </span><span class="pun">-</span><span class="pln">shardsvr </span><span class="pun">-</span><span class="pln">replSet shard3 </span><span class="pun">-</span><span class="pln">port </span><span class="lit">10003</span><span class="pun">-</span><span class="pln">dbpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">db</span><span class="pun">/</span><span class="pln">shard3 </span><span class="pun">-</span><span class="pln">logpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">dblog</span><span class="pun">/</span><span class="pln">shard3</span><span class="pun">/</span><span class="pln">shard3</span><span class="pun">.</span><span class="pln">log</span>

3.停止均衡器(Disable the Balancer).

连接到mongos,执行脚本



1. <span class="pun">./</span><span class="pln"> mongos </span><span class="pun">-</span><span class="pln">port </span><span class="lit">30000</span><span class="com">//30000为路由器端口</span>
2.3. <span class="pln">sh</span><span class="pun">.</span><span class="pln">stopBalancer</span><span class="pun">()</span>

检查是否已经完全停止



1. <span class="pln">use config</span>
2.3. <span class="kwd">while</span><span class="pun">(</span><span class="pln"> sh</span><span class="pun">.</span><span class="pln">isBalancerRunning</span><span class="pun">()</span><span class="pun">)</span><span class="pun">{</span>
4. <span class="pln"> print</span><span class="pun">(</span><span class="str">"waiting..."</span><span class="pun">);</span>
5. <span class="pln"> sleep</span><span class="pun">(</span><span class="lit">1000</span><span class="pun">);</span>
6. <span class="pun">}</span>

4.升级分片元数据(配置服务器)

进入mongod节点,运行升级脚本



1. <span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="typ">Mongo</span><span class="pun">/</span><span class="pln">mongodb</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x86_64</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mongos </span><span class="pun">-</span><span class="pln">configdb </span><span class="str">"192.168.199.133:20000,192.168.199.134:20000,192.168.199.135:20000"</span><span class="pun">-</span><span class="pln">logpath </span><span class="pun">/</span><span class="pln">data1</span><span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">dblog</span><span class="pun">/</span><span class="pln">mongos</span><span class="pun">/</span><span class="pln">updatemongos</span><span class="pun">.</span><span class="pln">log </span><span class="pun">—</span><span class="pln">upgrade</span>

查看updatemongos中日志信息,如果输出以下语句,则表示配置服务器升级成功

upgrade of config
server to v5 successful


Config database is at
version v5


依次停止旧的配置服务器启动新的配置服务器,备注:升级过程中保至少一台配置服务器正常运转

5.升级分片服务(主从节点升级)

停止10001的备份服务,启动新服务

停止10001的仲裁服务,启动新服务

切换10001的主节点到备份服务

连接至主节点 使用切换命令



1. <span class="pln">rs</span><span class="pun">.</span><span class="pln">stepDown</span><span class="pun">()</span>

查看是否切换成功



1. <span class="pln">rs</span><span class="pun">.</span><span class="pln">status</span><span class="pun">()</span>

依次处理10002节点与10003节点

WARNINGDo not upgrade mongod instances until after you have upgraded all
mongos instances.


6.重启主服务,启动Balancer

关闭原来的服务,使用新的脚本启动

下面的脚本启动Balancer



1. <span class="pln">sh</span><span class="pun">.</span><span class="pln">setBalancerState</span><span class="pun">(</span><span class="kwd">true</span><span class="pun">)</span>

7.检查服务是否正常运转

查看版本信息



1. <span class="pln">db</span><span class="pun">.</span><span class="pln">runCommand</span><span class="pun">({</span><span class="str">"buildInfo"</span><span class="pun">:</span><span class="lit">1</span><span class="pun">})</span>




your support will encourage me to continue to create!
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)