需要環境
configserver = 3套
shard = 3套
mongos =1套
編譯安裝請參考這篇
先建立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})