mongodb安裝最新版本並恢復成相同的舊環境

轉身淚傾城發表於2016-05-13
需求方說有一個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 


完成!

相關文章