安裝monodb軟件linux
su – mongodb
tar zxvf mongodb-linux-x86_64-1.6.2.tarmongodb
建立數據目錄
根據本例sharding架構圖所示,在各臺sever上建立shard數據文件目錄
Server1:數據庫
su – monodb
cd /monodb
mkdir -p data/shard11
mkdir -p data/shard21vim
Server2:架構
su – monodb
cd /monodb
mkdir -p data/shard12
mkdir -p data/shard22app
Server3:
su – monodb
cd /monodb
mkdir -p data/shard13
mkdir -p data/shard23spa
配置relica sets(複製集)server
1. 配置shard1所用到的replica sets:進程
Server1:it
cd /mongodb/mongodb-linux-x86_64-1.6.2
mkdir –p conf
cd conf
vim shard11.conf
shardsvr=true
replSet=shard1/172.16.2.14:27018
port=27018
dbpath=/mongoSharding/mongodata/data/shard11
oplogSize=10240
logpath=/mongoSharding/mongodata/logs/shard11.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/shard11.conf
Server2:
cd /mongodb/mongodb-linux-x86_64-1.6.2
mkdir –p conf
cd conf
vim shard21.conf
shardsvr=true
replSet=shard1/172.16.2.13:27018
port=27018
dbpath=/mongoSharding/mongodata/data/shard12
oplogSize=10240
logpath=/mongoSharding/mongodata/logs/shard12.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/shard12.conf
Server3:
cd /mongodb/mongodb-linux-x86_64-1.6.2
mkdir –p conf
cd conf
vim shard13.conf
shardsvr=true
replSet=shard1/172.16.2.15:27018
port=27018
dbpath=/mongoSharding/mongodata/data/shard13
oplogSize=10240
logpath=/mongoSharding/mongodata/logs/shard13.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/shard13.conf
初始化replica set
用mongo鏈接其中一個mongod,執行:
> config = {_id: 'shard1', members: [
{_id: 0, host: '172.16.2.13:27018'},
{_id: 1, host: '172.16.2.14:27018'},
{_id: 2, host: '172.16.2.15:27018'}]
}
> rs.initiate(config);
一樣方法,配置shard2用到的replica sets:
Server1:
cd /mongodb/mongodb-linux-x86_64-1.6.2
cd conf
vim shard21.conf
shardsvr=true
replSet=shard2/172.16.2.14:27019
port=27019
dbpath=/mongoSharding/mongodata/data/shard21
oplogSize=10240
logpath=/mongoSharding/mongodata/logs/shard21.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/shard21.conf
Server2:
cd /mongodb/mongodb-linux-x86_64-1.6.2
cd conf
vim shard22.conf
shardsvr=true
replSet=shard2/172.16.2.13:27019
port=27019
dbpath=/mongoSharding/mongodata/data/shard22
oplogSize=10240
logpath=/mongoSharding/mongodata/logs/shard22.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/shard22.conf
Server3:
cd /mongodb/mongodb-linux-x86_64-1.6.2
cd conf
vim shard23.conf
shardsvr=true
replSet=shard2/172.16.2.15:27019
port=27019
dbpath=/mongoSharding/mongodata/data/shard23
oplogSize=10240
logpath=/mongoSharding/mongodata/logs/shard23.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/shard23.conf
初始化replica set
用mongo鏈接其中一個mongod,執行:
> config = {_id: 'shard2', members: [
{_id: 0, host: '172.16.2.13:27019'},
{_id: 1, host: '172.16.2.14:27019'},
{_id: 2, host: '172.16.2.15:27019'}]
}
> rs.initiate(config);
Server1:
mkdir -p /mongodb/data/config
cd /mongodb/mongodb-linux-x86_64-1.6.2
cd conf
vim config.conf
configsvr=true
dbpath=/mongoSharding/mongodata/data/config
port=30000
logpath=/mongoSharding/mongodata/logs/config.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/config.conf
Server2:
mkdir -p /mongodb/data/config
cd /mongodb/mongodb-linux-x86_64-1.6.2
cd conf
vim config.conf
configsvr=true
dbpath=/mongoSharding/mongodata/data/config
port=30000
logpath=/mongoSharding/mongodata/logs/config.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/config.conf
Server3:
mkdir -p /mongodb/data/config
cd /mongodb/mongodb-linux-x86_64-1.6.2
cd conf
vim config.conf
configsvr=true
dbpath=/mongoSharding/mongodata/data/config
port=30000
logpath=/mongoSharding/mongodata/logs/config.log
logappend=true
fork=true
:wq
cd /mongodb/mongodb-linux-x86_64-1.6.2/bin
./mongod –f ../conf/config.conf
在server1,server2,server3上分別執行:
Cd conf
Vim mongos.conf
configdb=172.16.2.13:30000,172.16.2.14:30000,172.16.2.15:30000
port=40000
chunkSize=5
logpath=/mongoSharding/mongodata/logs/mongos.log
logappend=true
fork=true
:wq
Cd bin
./mongos –f ../conf/mongos.conf
鏈接到其中一個mongos進程,並切換到admin數據庫作如下配置
1. 鏈接到mongs,並切換到admin
./mongo 172.16.2.14:40000/amdin
>use admin;
>db.runCommand( { addshard : "shard1/172.16.2.13:27018,172.16.2.14:27018,172.16.2.15:27018″,name:」s1″,maxsize:20480} );