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

[size=small]需求方说有一个mongo节点挂掉了,现在需要按照原来的配置重新搭建mongodb,并换成mongodb最新版本:

当前的环境:
# 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

# 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 ---这个目录是之前那个挂掉的分片使用的,我们可以继续沿用


# /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
# 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


关闭旧的环境:
# /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
>

# /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
>

# 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添加三个分片:
# /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 }



# 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


完成![/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值