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

简介:
需求方说有一个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 


完成!
相关文章
|
11月前
|
NoSQL Ubuntu MongoDB
在Ubuntu 22.04上安装MongoDB 6.0的步骤
这些步骤应该可以在Ubuntu 22.04系统上安装MongoDB 6.0。安装过程中,如果遇到任何问题,可以查阅MongoDB的官方文档或者Ubuntu的相关帮助文档,这些资源通常提供了解决特定问题的详细指导。
1024 18
|
NoSQL MongoDB Python
【Python】已完美解决(MongoDB安装报错)Service ‘MongoDB Server (MongoDB)’ (MongoDB) failed tostart
【Python】已完美解决(MongoDB安装报错)Service ‘MongoDB Server (MongoDB)’ (MongoDB) failed tostart
2117 1
|
存储 NoSQL MongoDB
Docker中安装MongoDB并配置数据、日志、配置文件持久化。
现在,你有了一个运行在Docker中的MongoDB,它拥有自己的小空间,对高楼大厦的崩塌视而不见(会话丢失和数据不持久化的问题)。这个MongoDB的数据、日志、配置文件都会妥妥地保存在你为它精心准备的地方,天旋地转,它也不会失去一丁点儿宝贵的记忆(即使在容器重启后)。
1908 4
|
NoSQL MongoDB 微服务
微服务2——MongoDB单机部署1——下载安装
本指南介绍在Windows系统上安装和启动MongoDB的步骤。首先,从官网下载适用于32位或64位系统的预编译二进制包,选择稳定版(y为偶数)。解压后创建数据目录`data/db`,可通过命令行参数(如`mongod --dbpath=..\data\db`)或配置文件启动服务。配置文件需注意转义字符与空格使用,支持自定义日志路径、端口等参数。将bin目录加入环境变量可简化启动操作。
549 0
微服务2——MongoDB单机部署1——下载安装
|
存储 NoSQL Linux
微服务2——MongoDB单机部署4——Linux系统中的安装启动和连接
本节主要介绍了在Linux系统中安装、启动和连接MongoDB的详细步骤。首先从官网下载MongoDB压缩包并解压至指定目录,接着创建数据和日志存储目录,并配置`mongod.conf`文件以设定日志路径、数据存储路径及绑定IP等参数。之后通过配置文件启动MongoDB服务,并使用`mongo`命令或Compass工具进行连接测试。此外,还提供了防火墙配置建议以及服务停止的两种方法:快速关闭(直接杀死进程)和标准关闭(通过客户端命令安全关闭)。最后补充了数据损坏时的修复操作,确保数据库的稳定运行。
945 0
|
NoSQL Ubuntu Linux
Linux平台安装MongoDB
10月更文挑战第11天
760 5
|
NoSQL Shell MongoDB
Mac OSX 平台安装 MongoDB
10月更文挑战第11天
697 4
|
NoSQL Shell MongoDB
Windows 平台安装 MongoDB
10月更文挑战第10天
788 0
Windows 平台安装 MongoDB
|
存储 NoSQL MongoDB
docker安装Mongodb
这篇文章详细说明了如何使用Docker安装MongoDB,包括拉取镜像、创建并运行容器、数据持久化存储以及访问容器内的MongoDB服务的具体步骤。
1595 1
|
JavaScript NoSQL 前端开发

推荐镜像

更多