需要環境

configserver = 3套
shard = 3套
mongos =1套

編譯安裝請參考這篇

Centos6 源碼安裝mongodb-server

先建立configserver

configserver設定檔
vim /usr/local/mongodb/data/configserver.conf

port = 20001
dbpath = /usr/local/mongodb/data/configserver
logpath = /usr/local/mongodb/data/logs/configserver.log
fork = true
configsvr = true
replSet = cs

vim /usr/local/mongodb/data/configserver2.conf

port = 20002
dbpath = /usr/local/mongodb/data/configserver2
logpath = /usr/local/mongodb/data/logs/configserver2.log
fork = true
configsvr = true
replSet = cs

vim /usr/local/mongodb/data/configserver3.conf

port = 20003
dbpath = /usr/local/mongodb/data/configserver3
logpath = /usr/local/mongodb/data/logs/configserver3.log
fork = true
configsvr = true
replSet = cs

啟動configserver

mongod -f configserver.conf
mongod -f configserver2.conf
mongod -f configserver3.conf

進入configserver寫入相關配置:
mongo 192.168.2.171:20001

mongo>use admin
mongo>cfg = {
_id:'cs',
configsvr:true,
members:[
{_id:0,host:'192.168.2.171:20001'}
]
};
mongo>rs.initiate(cfg);

mongo 192.168.2.171:20002

mongo>use admin
mongo>cfg = {
_id:'cs',
configsvr:true,
members:[
{_id:0,host:'192.168.2.171:20002'}
]
};
mongo>rs.initiate(cfg);

mongo 192.168.2.171:20003

mongo>use admin
mongo>cfg = {
_id:'cs',
configsvr:true,
members:[
{_id:0,host:'192.168.2.171:20003'}
]
};
mongo>rs.initiate(cfg);

shard設定檔
vim /usr/local/mongodb/data/mongodb.conf

port = 20001
dbpath = /usr/local/mongodb/data/db
logpath = /usr/local/mongodb/data/logs/mongodb.log
fork = true
logappend = true
replSet = shard1
shardsvr = true

vim /usr/local/mongodb/data/mongodb2.conf

port = 20002
dbpath = /usr/local/mongodb/data/db2
logpath = /usr/local/mongodb/data/logs/mongodb2.log
fork = true
logappend = true
replSet = shard1
shardsvr = true

vim /usr/local/mongodb/data/mongodb3.conf

port = 20003
dbpath = /usr/local/mongodb/data/db3
logpath = /usr/local/mongodb/data/logs/mongodb3.log
fork = true
logappend = true
replSet = shard1
shardsvr = true

啟動shard

mongod -f mongodb.conf
mongod -f mongodb2.conf
mongod -f mongodb3.conf

進入shard寫入分片相關配置(只需要寫入一台即可):

mongo 192.168.2.174:20001

>use admin;
>config = { _id:"shard1", members:[
{_id:0,host:"192.168.2.174:20001"},
{_id:1,host:"192.168.2.174:20002"},
{_id:2,host:"192.168.2.174:20003",arbiterOnly:true}
]
};
>rs.initiate(config);

mongos設定檔

vim /usr/local/mongodb/data/mongos.conf

port = 30000
configdb= cs/192.168.2.171:20000,192.168.2.171:20002,192.168.2.171:20003
fork = true
logpath = /usr/local/mongodb/data/logs/mongos.log

啟動mongos

mongos -f mongos.conf

手動啟動語法

nohup mongos --configdb cs/192.168.2.171:20000,192.168.2.171:20002,192.168.2.171:20003 --port 30000 &

進入mongodb查看狀態指令

db.runCommand({"buildInfo":1})

db.runCommand({listshards:1})

 

 

 

 

By tony

自由軟體愛好者~喜歡不斷的思考各種問題,有新的事物都會想去學習嘗試 做實驗並熱衷研究 沒有所謂頂天的技術 只有謙虛及不斷的學習 精進專業,本站主要以分享系統及網路相關知識、資源而建立。 Github http://stnet253.github.io

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料