話說在虛擬機中會報各種錯誤,在真實的環(huán)境中就不會那么痛苦了。 想了一下虛擬機中報錯的原因有。機器卡,處理的時間長就會拋出錯誤 還有虛擬機如果給的空間太小,也會報異常。 此處講講我的大致思路。 第一,選擇三臺服務(wù)器,最好不是虛擬的 第二,設(shè)計三臺
話說在虛擬機中會報各種錯誤,在真實的環(huán)境中就不會那么痛苦了。解壓mongodb文件
主機1 版本號不一樣。我的是2.0.4
[root@mongodb40 ~]# tar zxvfmongodb-linux-x86_64-2.4.7.tgz
[root@mongodb40 ~]# mvmongodb-linux-x86_64-2.4.7 /home/mongodb
主機2
[root@mongodb41~]# tar zxvfmongodb-linux-x86_64-2.4.7.tgz
[root@mongodb41~]# mvmongodb-linux-x86_64-2.4.7 /home/mongodb
主機3
[root@mongodb250 ~]# tar zxvfmongodb-linux-x86_64-2.4.7.tgz
[root@mongodb250~]# mvmongodb-linux-x86_64-2.4.7 /home/mongodb
mongo集群分片實戰(zhàn)
服務(wù)器三臺
主機1(172.16.0.40) 主機2(172.16.0.41) 主機3(172.16.0.250)
第一片(11731) 主 副本 仲裁
第二片(11732) 仲裁 主 副本
第三片(11733) 副本 仲裁 主
30000 configServer configServer config Server
60000 RouteProcess RouteProcess Route Process
創(chuàng)建數(shù)據(jù)庫目錄
主機1(172.16.0.40)配置文件
[root@mongodb115 ~]# mkdir -p/home/data/shard1_1
[root@mongodb115 ~]# mkdir -p/home/data/shard2_1
[root@mongodb115 ~]# mkdir -p/home/data/shard3_1
[root@mongodb115 ~]# mkdir -p/home/data/config #config server目錄
[root@mongodb115 ~]# mkdir -p/home/config #放配置文件
[root@mongodb115 ~]# mkdir -p/home/data/logs
這個地方的所有配置目錄都放在/home/config目錄下,切記切記啊,因為我在這里吃過虧,不知一次。
分片1
[root@mongodb40 config]# catshard1_1.conf
port=11731
dbpath=/home/data/shard1_1
logpath=/home/data/logs/shard1_1.log
directoryperdb=true
logappend=true
replSet=shard1
#bind_ip=172.16.0.40
shardsvr=true
fork=true
pidfilepath=/home/data/shard1_1.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片2
[root@mongodb40 config]# catshard2_1.conf
port=11732
dbpath=/home/data/shard2_1
logpath=/home/data/logs/shard2_1.log
directoryperdb=true
logappend=true
replSet=shard2
#bind_ip=172.16.0.40
shardsvr=true
fork=true
pidfilepath=/home/data/shard2_1.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片3
[root@mongodb40 config]# catshard3_1.conf
port=11733
dbpath=/home/data/shard3_1
logpath=/home/data/logs/shard3_1.log
directoryperdb=true
logappend=true
replSet=shard3
#bind_ip=172.16.0.40
shardsvr=true
fork=true
pidfilepath=/home/data/shard3_1.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
config server 配置文件
[root@mongodb40 config]# catconfig.conf
port=30000
dbpath=/home/data/config
logpath=/home/data/logs/config.log
directoryperdb=true
configsvr=true
logappend=true
#bind_ip=172.16.0.40
fork=true
pidfilepath=/home/data/config.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
RouteProces 配置文件
[root@mongodb40 config]# catmongos.conf
port=60000
logpath=/home/data/logs/mongos.log
logappend=true
configdb=172.16.0.40:30000,172.16.0.41:30000,172.16.0.250:30000
#bind_ip=172.16.0.40
fork=true
pidfilepath=/home/data/mongos.pid
chunkSize=5
maxConns=20000
nohttpinterface=true
主機2(172.16.0.41)配置文件
[root@mongodb114 ~]# mkdir -p/home/data/shard1_2
[root@mongodb114 ~]# mkdir -p/home/data/shard2_2
[root@mongodb114 ~]# mkdir -p/home/data/shard3_2
[root@mongodb114 ~]# mkdir -p/home/data/config #config server目錄
[root@mongodb114 ~]# mkdir -p/home/config #放配置文件
[root@mongodb114 ~]# mkdir -p/home/data/logs
分片1
[root@mongodb41 config]# catshard1_2.conf
port=11731
dbpath=/home/data/shard1_2
logpath=/home/data/logs/shard1_2.log
directoryperdb=true
logappend=true
replSet=shard1
#bind_ip=172.16.0.41
shardsvr=true
fork=true
pidfilepath=/home/data/shard1_2.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片2
[root@mongodb41 config]# catshard2_2.conf
port=11732
dbpath=/home/data/shard2_2
logpath=/home/data/logs/shard2_2.log
directoryperdb=true
logappend=true
replSet=shard2
#bind_ip=172.16.0.41
shardsvr=true
fork=true
pidfilepath=/home/data/shard2_2.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片3
[root@mongodb41 config]# catshard3_2.conf
port=11733
dbpath=/home/data/shard3_2
logpath=/home/data/logs/shard3_2.log
directoryperdb=true
logappend=true
replSet=shard3
#bind_ip=172.16.0.41
shardsvr=true
fork=true
pidfilepath=/home/data/shard3_2.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
config server 配置文件
[root@mongodb41 config]# catconfig.conf
port=30000
dbpath=/home/data/config
logpath=/home/data/logs/config.log
directoryperdb=true
configsvr=true
logappend=true
#bind_ip=172.16.0.41
fork=true
pidfilepath=/home/data/config.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
RouteProces 配置文件
[root@mongodb41 config]# catmongos.conf
port=60000
logpath=/home/data/logs/mongos.log
logappend=true
configdb=172.16.0.40:30000,172.16.0.41:30000,172.16.0.250:30000
#bind_ip=172.16.0.41
fork=true
pidfilepath=/home/data/mongos.pid
chunkSize=5
maxConns=20000
nohttpinterface=true
主機3(172.16.0.250)配置文件
[root@mongodb103 ~]# mkdir -p/home/data/shard1_3
[root@mongodb103 ~]# mkdir -p/home/data/shard2_3
[root@mongodb103 ~]# mkdir -p/home/data/shard3_3
[root@mongodb103 ~]# mkdir -p/home/data/config #config server目錄
[root@mongodb103 ~]# mkdir -p/home/config #放配置文件
[root@mongodb103 ~]# mkdir -p/home/data/logs
分片1
[root@mongodb250 config]# catshard1_3.conf
port=11731
dbpath=/home/data/shard1_3
logpath=/home/data/logs/shard1_3.log
directoryperdb=true
logappend=true
replSet=shard1
#bind_ip=172.16.0.250
shardsvr=true
fork=true
pidfilepath=/home/data/shard1_3.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片2
[root@mongodb250 config]# catshard2_3.conf
port=11732
dbpath=/home/data/shard2_3
logpath=/home/data/logs/shard2_3.log
directoryperdb=true
logappend=true
replSet=shard2
#bind_ip=172.16.0.250
shardsvr=true
fork=true
pidfilepath=/home/data/shard2_3.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片3
[root@mongodb250 config]# catshard3_3.conf
port=11733
dbpath=/home/data/shard3_3
logpath=/home/data/logs/shard3_3.log
directoryperdb=true
logappend=true
replSet=shard3
#bind_ip=172.16.0.250
shardsvr=true
fork=true
pidfilepath=/home/data/shard3_3.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
config server 配置文件
[root@mongodb250 config]# catconfig.conf
port=30000
dbpath=/home/data/config
logpath=/home/data/logs/config.log
directoryperdb=true
configsvr=true
logappend=true
#bind_ip=172.16.0.250
fork=true
pidfilepath=/home/data/config.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
RouteProces 配置文件
[root@mongodb250 config]# catmongos.conf
port=60000
logpath=/home/data/logs/mongos.log
logappend=true
configdb=172.16.0.40:30000,172.16.0.41:30000,172.16.0.250:30000
#bind_ip=172.16.0.250
fork=true
pidfilepath=/home/data/mongos.pid
chunkSize=5
maxConns=20000
nohttpinterface=true
啟動shard1,shard2,shard3
主機1
[root@mongodb115 ~]#/home/mongodb/bin/mongod -f /home/config/shard1_1.conf
[root@mongodb115 ~]#/home/mongodb/bin/mongod -f /home/config/shard2_1.conf
[root@mongodb115 ~]#/home/mongodb/bin/mongod -f /home/config/shard3_1.conf
主機2
[root@mongodb114 ~]#/home/mongodb/bin/mongod -f /home/config/shard1_2.conf
[root@mongodb114 ~]#/home/mongodb/bin/mongod -f /home/config/shard2_2.conf
[root@mongodb114 ~]#/home/mongodb/bin/mongod -f /home/config/shard3_2.conf
主機3
[root@mongodb103 ~]#/home/mongodb/bin/mongod -f /home/config/shard1_3.conf
[root@mongodb103 ~]#/home/mongodb/bin/mongod -f /home/config/shard2_3.conf
[root@mongodb103 ~]#/home/mongodb/bin/mongod -f /home/config/shard3_3.conf
配置ReplicaSets
配置分片1 在172.16.0.40上配置
[root@mongodb115 ~]# /home/mongodb/bin/mongo--port 11731
MongoDB shell version: 2.0.4
connecting to:127.0.0.1:11731/test
>config={_id:'shard1',members:[{_id:0,host:'172.16.0.40:11731',priority:2},{_id:1,host:'172.16.0.41:11731'},{_id:2,host:'172.16.0.250:11731',arbiterOnly:true}]}
rs.initiate(config)
配置分片2 在172.16.0.41上配置
[root@mongodb114 ~]# /home/mongodb/bin/mongo--port 11732
MongoDB shell version: 2.0.4
connecting to:127.0.0.1:11732/test
>config={_id:'shard2',members:[{_id:0,host:'172.16.0.40:11732',arbiterOnly:true},{_id:1,host:'172.16.0.41:11732',priority:2},{_id:2,host:'172.16.0.250:11732'}]}
rs.initiate(config)
配置分片3 在172.16.0.250上配置members里面的優(yōu)先級priority值高的為主節(jié)點,對于仲裁點一定要加上arbiterOnly:true
[root@mongodb103 ~]# /home/mongodb/bin/mongo--port 11733
MongoDB shell version: 2.0.4
connecting to:127.0.0.1:11733/test
>config={_id:'shard3',members:[{_id:0,host:'172.16.0.40:11733'},{_id:1,host:'172.16.0.41:11733',arbiterOnly:true},{_id:2,host:'172.16.0.25:11733',priority:2}]}
rs.initiate(config)
啟動3臺configserver
主機1
[root@mongodb40 ~]#/home/mongodb/bin/mongod -f/home/config/config.conf
主機2
[root@mongodb41 ~]#/home/mongodb/bin/mongod -f/home/config/config.conf
主機3
[root@mongodb250 ~]#/home/mongodb/bin/mongod -f/home/config/config.conf
啟動3臺Route Process
主機1
[root@mongodb40 ~]# /home/mongodb/bin/mongos-f /home/config/mongos.conf
主機2
[root@mongodb41 ~]# /home/mongodb/bin/mongos-f /home/config/mongos.conf
主機3
[root@mongodb250 ~]#/home/mongodb/bin/mongos -f /home/config/mongos.conf
配置shardCluser
[root@mongodb115 ~]# /home/mongodb/bin/mongo--port 60000
MongoDB shell version: 2.0.4
connecting to:127.0.0.1:60000/test
mongos> use admin
switched to db admin
mongos>db.runCommand({addshard:"shard1/172.16.0.40:11731,172.16.0.41:11731, 172.16.0.250:11731"})
mongos>db.runCommand({addshard:"shard2/172.16.0.40:11732,172.16.0.41:11732, 172.16.0.250:11732"})
mongos> db.runCommand({addshard:"shard3/172.16.0.40:11733,172.16.0.41:11733, 172.16.0.250:11733"})
接下來激活分片,如下面的代碼所示: 采用hash分片
db.runCommand({enablesharding:"test"})
db.runCommand({shardcollection:"test.users",key:{id:"hashed"}})
上面這個會報錯,我這邊版本不支持hashed分片,要把id:1聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com