mongodb安装最新版本并恢复成相同的旧环境

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:
需求方说有一个mongo节点挂掉了,现在需要按照原来的配置重新搭建mongodb,并换成mongodb最新版本: 

当前的环境: 
[root@mongodb1 ~]# ps -ef|grep mongo 
avahi     2502     1  0 Sep24 ?        00:24:33 avahi-daemon: running [mongodb1.local] 
root      2848     1  2 Sep24 ?        2-00:59:16 /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongod -shardsvr -port 29017 -dbpath /home/mongodb/data/shard1t -oplogSize 10000 -logpath /home/mongodb/data/shard1t.log -fork --rest 
root      2862     1  9 Sep24 ?        7-21:26:42 /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongod -shardsvr -port 29018 -dbpath /home/mongodb/data1/shard2t -oplogSize 10000 -logpath /home/mongodb/data/shard2t.log -fork --rest  ---上面两个是mongo的两个分片 
root      2893     1  0 Sep24 ?        18:14:21 /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongod --configsvr --dbpath /home/mongodb/data/configt/ --port 27000 -logpath /home/mongodb/data/configt.log --fork    ---这个是configs 
root      2912     1  3 Sep24 ?        2-13:06:11 /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongos --configdb 192.168.12.220:27000 -port 45000 -chunkSize 1024 --logpath /home/mongodb/data/mongost.log -fork   ---这个肯定是mongos了 
root     23851  1890  0 14:13 pts/0    00:00:00 ./mongo 127.0.0.1:45000 
root     24368 24349  0 14:20 pts/1    00:00:00 grep mongo 

同时可以看到数据的存放目录 
/home/mongodb/data/shard1t   ---29017 
/home/mongodb/data1/shard2t  ---29018 

[root@mongodb1 ~]# df -ah 
文件系统       容量  已用  可用 已用%% 挂载点 
/dev/sdc2              97G   70G   22G  76% / 
proc                     0     0     0   -  /proc 
sysfs                    0     0     0   -  /sys 
devpts                   0     0     0   -  /dev/pts 
tmpfs                  63G     0   63G   0% /dev/shm 
/dev/sdc1             194M   32M  153M  17% /boot 
/dev/sdc4             790G  501G  250G  67% /home/mongodb/data     ----需求方要求这个目录下放1个分片 
/dev/md127            1.8T  924G  817G  54% /home/mongodb/data1    ----需求方要求这个目录下放2个分片 
none                     0     0     0   -  /proc/sys/fs/binfmt_misc 
sunrpc                   0     0     0   -  /var/lib/nfs/rpc_pipefs 

根据要求,我们可以这样建立: 
/home/mongodb/data/shard1t   ---29017 
/home/mongodb/data1/shard2t  ---29018 
/home/mongodb/data1/shard2n  ---29019   ---这个目录是之前那个挂掉的分片使用的,我们可以继续沿用 


[root@mongodb1 ~]# /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongo 192.168.12.220:45000/admin  ---进入mongos 
MongoDB shell version: 2.4.9 
connecting to: 192.168.12.220:45000/admin 
mongos> show dbs; 
admin (empty) 
config 0.109375GB 
vds 937.4501953125GB    --这个肯定是存放数据的数据库了,进去看看表 
mongos> use vds;     ---进入vds 
switched to db vds 
mongos> show collections; --查看到有以下表,需求方只要求创建分片的两个表,并建好跟之前一样的索引 
count_temp_58ADE9E6 
count_temp_6BFFE2C0 
count_temp_C83251CB 
count_temp_FB2BA5B8 
system.indexes 
t_url_count 
t_url_event 
tteemp1 
ttemp1 
ttemp2 
ttemp3 
week_url_count_v1_49 
mongos> db.printShardingStatus()  --查看分片信息 
--- Sharding Status --- 
  sharding version: { 
"_id" : 1, 
"version" : 3, 
"minCompatibleVersion" : 3, 
"currentVersion" : 4, 
"clusterId" : ObjectId("53c884e667bfb0785736b7cb") 

  shards: 
{  "_id" : "shard0000",  "host" : "192.168.12.220:29017" } 
{  "_id" : "shard0001",  "host" : "192.168.12.220:29018" } 
  databases: 
{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" } 
{  "_id" : "vds",  "partitioned" : true,  "primary" : "shard0000" } 
vds.t_url_count 
shard key: { "add_date" : 1, "sub_url_hash" : 1 } 
chunks: 
shard0000 76 
shard0001 225 
too many chunks to print, use verbose if you want to force print 
vds.t_url_event 
shard key: { "source" : 1, "add_date" : 1 } 
chunks: 
shard0001 4097 
shard0000 3554 
too many chunks to print, use verbose if you want to force print 
{  "_id" : "test",  "partitioned" : false,  "primary" : "shard0000" } 

mongos> db.t_url_count.findOne()  ---查看表t_url_count结构和数据 

"_id" : ObjectId("53dfb4ccffee1a542522ae6f"), 
"add_date" : 20140805, 
"count" : 1, 
"new_flag" : true, 
"sample_flag" : "1", 
"sample_md5" : "1CF2AE74144AFE0AF5DF2B0D506C101F", 
"source" : "hebei", 
"sub_url" : "117.27.243.80/bcs.apk.r1.91rb.com/data/upload/apkres/2014/7_31/22/com.baidu.music.lebo_100550.apk", 
"sub_url_hash" : "A84C6BC402F9667FE262FDC8F7E20E69", 
"url_domain" : "117.27.243.80", 
"url_domain_hash" : "4B57142D262F97AB347DC5C762DCBE20", 
"virus_flag" : "0", 
"virusname" : "virusname_init" 


mongos> db.t_url_event.findOne() 

"_id" : ObjectId("53c88772ffee1a1f14072e41"), 
"add_date" : ISODate("2014-07-18T10:33:22.200Z"), 
"source" : "guizhou", 
"url_domain" : "gdown.baidu.com", 
"sub_url_hash" : "F5A3A0FFB5052F4E46F1B8153AD59F84", 
"dstip" : "119.84.87.12", 
"sub_url" : "gdown.baidu.com/data/wisegame/1ea2ac084e55bc09/tengxunshipin_5905.apk", 
"url" : "gdown.baidu.com/data/wisegame/1ea2ac084e55bc09/tengxunshipin_5905.apk", 
"protoid" : 304, 
"dport" : 38108, 
"srcip" : "114.139.222.229", 
"sport" : 80, 
"url_domain_hash" : "B7B23135EE9C320C84DCC1C68B8641E6", 
"utime" : "2014-07-09 14:31:10" 


找到参数文件配置: 


下载最新版本: 
https://www.mongodb.org/downloads#production 
[root@mongodb1 mongodb]# tar -zxvf mongodb-linux-x86_64-3.2.0.tgz 
mongodb-linux-x86_64-3.2.0/README 
mongodb-linux-x86_64-3.2.0/THIRD-PARTY-NOTICES 
mongodb-linux-x86_64-3.2.0/MPL-2 
mongodb-linux-x86_64-3.2.0/GNU-AGPL-3.0 
mongodb-linux-x86_64-3.2.0/bin/mongodump 
mongodb-linux-x86_64-3.2.0/bin/mongorestore 
mongodb-linux-x86_64-3.2.0/bin/mongoexport 
mongodb-linux-x86_64-3.2.0/bin/mongoimport 
mongodb-linux-x86_64-3.2.0/bin/mongostat 
mongodb-linux-x86_64-3.2.0/bin/mongotop 
mongodb-linux-x86_64-3.2.0/bin/bsondump 
mongodb-linux-x86_64-3.2.0/bin/mongofiles 
mongodb-linux-x86_64-3.2.0/bin/mongooplog 
mongodb-linux-x86_64-3.2.0/bin/mongoperf 
mongodb-linux-x86_64-3.2.0/bin/mongod 
mongodb-linux-x86_64-3.2.0/bin/mongos 
mongodb-linux-x86_64-3.2.0/bin/mongo 


关闭旧的环境: 
[root@mongodb1 ~]# /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongo localhost:45000/admin   --关闭mongos 
MongoDB shell version: 2.4.9 
connecting to: localhost:45000/admin 
mongos> use admin 
switched to db admin 
mongos> db.shutdownServer() 
Thu Dec 17 15:04:56.195 DBClientCursor::init call() failed 
server should be down... 
Thu Dec 17 15:04:56.198 trying reconnect to localhost:45000 
Thu Dec 17 15:04:56.199 reconnect localhost:45000 failed couldn't connect to server localhost:45000 


[root@mongodb1 mongodb]# /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongo localhost:27000   --关闭configs 
MongoDB shell version: 2.4.9 
connecting to: localhost:27000/test 
Server has startup warnings: 
Thu Sep 24 09:20:24.653 [initandlisten] 
Thu Sep 24 09:20:24.654 [initandlisten] ** WARNING: You are running on a NUMA machine. 
Thu Sep 24 09:20:24.654 [initandlisten] **          We suggest launching mongod like this to avoid performance problems: 
Thu Sep 24 09:20:24.654 [initandlisten] **              numactl --interleave=all mongod [other options] 
Thu Sep 24 09:20:24.654 [initandlisten] 
configsvr> use admin 
switched to db admin 
configsvr> db.shutdownServer(); 
Thu Dec 17 15:06:46.561 DBClientCursor::init call() failed 
server should be down... 
Thu Dec 17 15:06:46.563 trying reconnect to localhost:27000 
Thu Dec 17 15:06:46.563 reconnect localhost:27000 failed couldn't connect to server localhost:27000 


root@mongodb1 mongodb]# /home/mongodb/mongodb-linux-x86_64-2.4.9/bin/mongo localhost:29017  --相应关闭两个分片 
MongoDB shell version: 2.4.9 
connecting to: localhost:29017/test 
Server has startup warnings: 
Thu Sep 24 09:19:22.531 [initandlisten] 
Thu Sep 24 09:19:22.531 [initandlisten] ** WARNING: You are running on a NUMA machine. 
Thu Sep 24 09:19:22.531 [initandlisten] **          We suggest launching mongod like this to avoid performance problems: 
Thu Sep 24 09:19:22.531 [initandlisten] **              numactl --interleave=all mongod [other options] 
Thu Sep 24 09:19:22.531 [initandlisten] 
> use admin 
switched to db admin 
> db.shutdownServer(); 
Thu Dec 17 15:07:27.355 DBClientCursor::init call() failed 
server should be down... 
Thu Dec 17 15:07:27.358 trying reconnect to localhost:29017 
Thu Dec 17 15:07:27.359 reconnect localhost:29017 failed couldn't connect to server localhost:29017 


[root@mongodb1 mongodb]# ps -ef|grep mongo 
avahi     2502     1  0 Sep24 ?        00:24:33 avahi-daemon: running [mongodb1.local] 
root     27675 24811  0 15:08 pts/3    00:00:00 grep mongo 


下面可以开始删除原始数据了 

删除数据: 
cd /home/mongodb/data/shard1t    rm -fr * 
cd /home/mongodb/data1/shard2t   rm -fr * 
cd /home/mongodb/data1/shard2n   rm -fr * 


删除日志: 
cd /home/mongodb/data/  rm -f *log* 

删除配置目录下内容: 
cd /home/mongodb/data/configt/ 



可以重新搭建新环境了: 

启动configs: 
/home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod --configsvr --dbpath /home/mongodb/data/configt/ --port 27000 -logpath /home/mongodb/data/configt.log --fork 

启动mongos: 
/home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongos --configdb 192.168.12.220:27000 -port 45000 -chunkSize 1024 --logpath /home/mongodb/data/mongost.log -fork   

启动三个mongod分片: 
/home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod -shardsvr -port 29017 -dbpath /home/mongodb/data/shard1t -oplogSize 10000 -logpath /home/mongodb/data/shard1t.log -fork --rest 
/home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod -shardsvr -port 29018 -dbpath /home/mongodb/data1/shard2t -oplogSize 10000 -logpath /home/mongodb/data/shard2t.log -fork --rest 
/home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod -shardsvr -port 29019 -dbpath /home/mongodb/data1/shard2n -oplogSize 10000 -logpath /home/mongodb/data/shard2n.log -fork --rest 


连接到mongos添加三个分片: 
[root@mongodb1 data]# /home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongo 192.168.12.220:45000/admin 
MongoDB shell version: 3.2.0 
connecting to: 192.168.12.220:45000/admin 
mongos> 
mongos> db.runCommand({"addshard":"192.168.12.220:29017"}); 
{ "shardAdded" : "shard0000", "ok" : 1 } 
mongos> db.runCommand({"addshard":"192.168.12.220:29018"}); 
{ "shardAdded" : "shard0001", "ok" : 1 } 
mongos> db.runCommand({"addshard":"192.168.12.220:29019"}); 
{ "shardAdded" : "shard0002", "ok" : 1 } 

mongos> db.printShardingStatus()  --添加分片成功 
--- Sharding Status --- 
  sharding version: { 
"_id" : 1, 
"minCompatibleVersion" : 5, 
"currentVersion" : 6, 
"clusterId" : ObjectId("56726310c437b898a5bd5655") 

  shards: 
{  "_id" : "shard0000",  "host" : "192.168.12.220:29017" } 
{  "_id" : "shard0001",  "host" : "192.168.12.220:29018" } 
{  "_id" : "shard0002",  "host" : "192.168.12.220:29019" } 
  active mongoses: 
"3.2.0" : 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: 
  
创建库: 
mongos>use vds     ---直接use dbname就可以创建库,但必须保证库里有表,不然空库就被自动删除了。 

创建表: 
mongos> db.t_url_count.insert(  

"_id" : ObjectId("53dfb4ccffee1a542522ae6f"), 
"add_date" : 20140805, 
"count" : 1, 
"new_flag" : true, 
"sample_flag" : "1", 
"sample_md5" : "1CF2AE74144AFE0AF5DF2B0D506C101F", 
"source" : "hebei", 
"sub_url" : "117.27.243.80/bcs.apk.r1.91rb.com/data/upload/apkres/2014/7_31/22/com.baidu.music.lebo_100550.apk", 
"sub_url_hash" : "A84C6BC402F9667FE262FDC8F7E20E69", 
"url_domain" : "117.27.243.80", 
"url_domain_hash" : "4B57142D262F97AB347DC5C762DCBE20", 
"virus_flag" : "0", 
"virusname" : "virusname_init" 
}) 

mongos> db.t_url_event.insert({"_id" : ObjectId("53c88772ffee1a1f14072e41"), 
"add_date" : ISODate("2014-07-18T10:33:22.200Z"), 
"source" : "guizhou", 
"url_domain" : "gdown.baidu.com", 
"sub_url_hash" : "F5A3A0FFB5052F4E46F1B8153AD59F84", 
"dstip" : "119.84.87.12", 
"sub_url" : "gdown.baidu.com/data/wisegame/1ea2ac084e55bc09/tengxunshipin_5905.apk", 
"url" : "gdown.baidu.com/data/wisegame/1ea2ac084e55bc09/tengxunshipin_5905.apk", 
"protoid" : 304, 
"dport" : 38108, 
"srcip" : "114.139.222.229", 
"sport" : 80, 
"url_domain_hash" : "B7B23135EE9C320C84DCC1C68B8641E6", 
"utime" : "2014-07-09 14:31:10"}) 
WriteResult({ "nInserted" : 1 }) 

查看表: 
mongos> show collections; 
t_url_count 
t_url_event 


创建索引: 
mongos> use vds; 
switched to db vds 
mongos> db.t_url_event.ensureIndex({"source":1, "add_date":1}) 

"raw" : { 
"192.168.12.220:29017" : { 
"createdCollectionAutomatically" : false, 
"numIndexesBefore" : 1, 
"numIndexesAfter" : 2, 
"ok" : 1 

}, 
"ok" : 1 

mongos> db.t_url_count.ensureIndex({"add_date":1, "sub_url_hash":1}) 

"raw" : { 
"192.168.12.220:29017" : { 
"createdCollectionAutomatically" : false, 
"numIndexesBefore" : 1, 
"numIndexesAfter" : 2, 
"ok" : 1 

}, 
"ok" : 1 



设置允许库分片: 
mongos> use admin 
switched to db admin 
mongos> db.runCommand({"enablesharding":"vds"}); 
{ "ok" : 1 } 


添加表到分片: 
mongos> use admin 
switched to db admin 
mongos> db.runCommand({"shardcollection":"vds.t_url_count","key":{"add_date":1, "sub_url_hash":1}}) 
{ "collectionsharded" : "vds.t_url_count", "ok" : 1 } 
mongos> db.runCommand({"shardcollection":"vds.t_url_event","key":{"source":1, "add_date":1}}) 
{ "collectionsharded" : "vds.t_url_event", "ok" : 1 } 

查看最后结果: 
mongos> db.printShardingStatus() 
--- Sharding Status --- 
  sharding version: { 
"_id" : 1, 
"minCompatibleVersion" : 5, 
"currentVersion" : 6, 
"clusterId" : ObjectId("56726310c437b898a5bd5655") 

  shards: 
{  "_id" : "shard0000",  "host" : "192.168.12.220:29017" } 
{  "_id" : "shard0001",  "host" : "192.168.12.220:29018" } 
{  "_id" : "shard0002",  "host" : "192.168.12.220:29019" } 
  active mongoses: 
"3.2.0" : 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" : "vds",  "primary" : "shard0000",  "partitioned" : true } 
vds.t_url_count 
shard key: { "add_date" : 1, "sub_url_hash" : 1 } 
unique: false 
balancing: true 
chunks: 
shard0000 1 
{ "add_date" : { "$minKey" : 1 }, "sub_url_hash" : { "$minKey" : 1 } } -->> { "add_date" : { "$maxKey" : 1 }, "sub_url_hash" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 
vds.t_url_event 
shard key: { "source" : 1, "add_date" : 1 } 
unique: false 
balancing: true 
chunks: 
shard0000 1 
{ "source" : { "$minKey" : 1 }, "add_date" : { "$minKey" : 1 } } -->> { "source" : { "$maxKey" : 1 }, "add_date" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 
{  "_id" : "t_url_event",  "primary" : "shard0001",  "partitioned" : true } 
{  "_id" : "t_url_count",  "primary" : "shard0001",  "partitioned" : true } 



[root@mongodb1 bin]# ps -ef|grep mongo 
root     27963     1  0 15:23 ?        00:00:11 /home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod --configsvr --dbpath /home/mongodb/data/configt/ --port 27000 -logpath /home/mongodb/data/configt.log --fork 
root     27990     1  0 15:23 ?        00:00:03 /home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongos --configdb 192.168.12.220:27000 -port 45000 -chunkSize 1024 --logpath /home/mongodb/data/mongost.log -fork 
root     28062     1  0 15:24 ?        00:00:11 /home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod -shardsvr -port 29017 -dbpath /home/mongodb/data/shard1t -oplogSize 10000 -logpath /home/mongodb/data/shard1t.log -fork --rest 
root     28094     1  0 15:25 ?        00:00:10 /home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod -shardsvr -port 29018 -dbpath /home/mongodb/data1/shard2t -oplogSize 10000 -logpath /home/mongodb/data/shard2t.log -fork --rest 
root     28117     1  0 15:25 ?        00:00:10 /home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongod -shardsvr -port 29019 -dbpath /home/mongodb/data1/shard2n -oplogSize 10000 -logpath /home/mongodb/data/shard2n.log -fork --rest 
root     28401 24811  0 15:34 pts/3    00:00:00 /home/mongodb/mongodb-linux-x86_64-3.2.0/bin/mongo 192.168.12.220:45000/admin 
root     28645 24834  0 15:45 pts/4    00:00:00 grep mongo 


完成!
相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
5月前
|
开发框架 NoSQL JavaScript
mongoDB入门教程四:安装Node+express环境支撑
mongoDB入门教程四:安装Node+express环境支撑
49 0
|
4月前
|
存储 JSON 分布式计算
MongoDB【部署 01】mongodb最新版本6.0.5安装部署配置使用及mongodb-shell1.8.0安装使用(云盘分享安装文件)
MongoDB【部署 01】mongodb最新版本6.0.5安装部署配置使用及mongodb-shell1.8.0安装使用(云盘分享安装文件)
153 0
|
5月前
|
NoSQL MongoDB 数据库
mongoDB入门教程一:下载安装和环境配置、连接运行
mongoDB入门教程一:下载安装和环境配置、连接运行
205 0
|
5月前
|
存储 NoSQL Linux
Linux下安装MongoDB
Linux下安装MongoDB
95 0
|
4月前
|
NoSQL MongoDB 数据安全/隐私保护
|
1天前
|
存储 分布式计算 NoSQL
MongoDB的简介和安装(在服务器上)
MongoDB的简介和安装(在服务器上)
8 0
|
1月前
|
NoSQL MongoDB Docker
docker安装MongoDB
docker安装MongoDB
21 0
|
2月前
|
NoSQL 关系型数据库 MySQL
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
不同系统下进行MySQL安装、MongoDB安装、Redis安装【2月更文挑战第5天】
444 5
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
|
3月前
|
存储 JSON NoSQL
【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
【1月更文挑战第26天】【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
|
3月前
|
存储 NoSQL MongoDB
学习如何使用 Python 连接 MongoDB: PyMongo 安装和基础操作教程
Python 需要一个 MongoDB 驱动程序来访问 MongoDB 数据库。我将使用 MongoDB 驱动程序 PyMongo 建议您使用 PIP 来安装 PyMongo。PIP 很可能已经安装在您的 Python 环境中。将命令行导航到 PIP 的位置,然后键入以下内容:
93 1