1. 程式人生 > >mongodb叢集部署(經過生產測試完整)

mongodb叢集部署(經過生產測試完整)

先去官網下載

0.tar -xvf 解壓檔案

1. mkdir  mongos config  shard1  shard2  shard3


mkdir -p /data/mongdb/mongos/log


mkdir -p /data/mongdb/config/log
mkdir -p /data/mongdb/config/data


mkdir -p /data/mongdb/shard1/log
mkdir -p /data/mongdb/shard1/data


mkdir -p /data/mongdb/shard2/log
mkdir -p /data/mongdb/shard2/data


mkdir -p /data/mongdb/shard3/log
mkdir -p /data/mongdb/shard3/data




2.建立log data檔案
  mongos --log 
  config --data  log 
  shard1 --data  log 
  shard2 --data  log 
  shard3 --data  log 
3.每臺啟動配置伺服器
./mongodb/bin/mongod --configsvr --dbpath /data/mongdb/config/data --port 21000 --logpath /data/mongdb/config/log/config.log --fork


---nohup ./bin/mongod --configsvr --dbpath /data/mongdb/config/data --port 21000 --logpath /data/mongdb/config/log/config.log --fork & tail -f nohup.out  &




4.在每一臺伺服器分別啟動mongos伺服器。
./mongodb/bin/mongos --configdb 192.168.2.1:21000,192.168.2.2:21000,192.168.2.3:21000 --port 20000 --logpath /data/mongdb/mongos/log/mongos.log --fork


5. 
 ./mongodb/bin/mongod --shardsvr --replSet shard1 --port 22001 --dbpath /data/mongdb/shard1/data --logpath /data/mongdb/shard1/log/shard1.log --fork --nojournal --oplogSize 10
 ./mongodb/bin/mongod --shardsvr --replSet shard2 --port 22002 --dbpath /data/mongdb/shard2/data --logpath /data/mongdb/shard2/log/shard2.log --fork --nojournal --oplogSize 10
 ./mongodb/bin/mongod --shardsvr --replSet shard3 --port 22003 --dbpath /data/mongdb/shard3/data --logpath /data/mongdb/shard2/log/shard3.log --fork --nojournal --oplogSize 10
 


./bin/mongo -port 22003




5.分配
./mongodb/bin/mongo -port 22001
 config = {_id: 'shard1', members: [
        {_id: 0, host: '192.168.2.1:22001',priority:10},
        {_id: 1, host: '192.168.2.3:22001',priority:5},
        {_id: 2, host: '192.168.2.2:22001',arbiterOnly: true}
    ]};
    rs.initiate(config);
    rs.status();
   


./mongodb/bin/mongo -port 22002
 config = {_id: 'shard2', members: [
        {_id: 0, host: '192.168.2.2:22002',priority:10},
        {_id: 1, host: '192.168.2.1:22002',priority:5},
        {_id: 2, host: '192.168.2.3:22002',arbiterOnly: true}
    ]};
    rs.initiate(config);
    rs.status();
    
  ./mongodb/bin/mongo -port 22003 
 config = {_id: 'shard3', members: [
    {_id: 0, host: '192.168.2.3:22003',priority:10},
    {_id: 1, host: '192.168.2.2:22003',priority:5},
    {_id: 2, host: '192.168.2.1:22003',arbiterOnly: true}
]};
rs.initiate(config);
rs.status();




./mongodb/bin/mongo 127.0.0.1:20000
use admin
6.任意一臺機器


db.runCommand( { addshard : "shard1/192.168.2.1:22001,192.168.2.2:22001,192.168.2.3:22001"});
db.runCommand( { addshard : "shard2/192.168.2.1:22002,192.168.2.2:22002,192.168.2.3:22002"});
db.runCommand( { addshard : "shard3/192.168.2.1:22003,192.168.2.2:22003,192.168.2.3:22003"});




db.runCommand( { listshards : 1 } );




 for (var i = 1; i <= 100000; i++)db.table1.save({id:i,"test1":"testval1"});


show collections  table1


show collections