1、配置集群目录
1 $ mkdir -p /home/scotte.ye/data/10001 2 $ mkdir -p /home/scotte.ye/data/10002 3 $ mkdir -p /home/scotte.ye/data/10003 4 5 $ mkdir -p /home/scotte.ye/data/10011 6 $ mkdir -p /home/scotte.ye/data/10012 7 $ mkdir -p /home/scotte.ye/data/10013 8 9 $ mkdir -p /home/scotte.ye/data/config1 10 $ mkdir -p /home/scotte.ye/data/config2 11 $ mkdir -p /home/scotte.ye/data/config3
日志目录:
1 $ mkdir -p /home/scotte.ye/data/logs
1 //10001 2 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10001/ -port=10001 --nohttpinterface --replSet set1 3 4 //10002 5 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10002/ -port=10002 --nohttpinterface --replSet set1 6 7 //10003 8 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10003/ -port=10003 --nohttpinterface --replSet set1
配置集群
1 $ ./mongo -port 10001 2 MongoDB shell version: 2.0.2 3 connecting to: 127.0.0.1:10001/test 4 >#配置集合1 5 >#构建参数 6 >cfg={_id:'set1', 7 members:[{_id:1,host:'192.168.35.106:10001'}, 8 {_id:1,host:'192.168.35.106:10002'}, 9 {_id:1,host:'192.168.35.106:10003'}] 10 }; 11 { 12 "_id":"set1", 13 "members":[ 14 { 15 "_id":1, 16 "host":"192.168.35.106:10001" 17 }, 18 { 19 "_id":2, 20 "host":"192.168.35.106:10002" 21 }, 22 { 23 "_id":3, 24 "host":"192.168.35.106:10003" 25 } 26 ] 27 } 28 >#让参数生效 29 >rs.initiate(cfg); 30 { 31 "info":"Config now saved locally. Should come online in about aminute.", 32 "OK":1 33 } 34 >#查看运行状态 35 >rs.status(); 36 { 37 "set" : "set1", 38 "date" : ISODate("2012-02-29T12:02:46Z"), 39 "myState" : 2, 40 "syncingTo" : "192.168.35.106:10003", 41 "members" : [ 42 { 43 "_id" : 1, 44 "name" : "192.168.35.106:10001", 45 "health" : 1, 46 "state" : 2, 47 "stateStr" : "PRIMARY", 48 "optime" : { 49 "t" : 1330435339000, 50 "i" : 2413 51 }, 52 "optimeDate" : ISODate("2012-02-28T13:22:19Z"), 53 "self" : true 54 }, 55 { 56 "_id" : 2, 57 "name" : "192.168.35.106:10002", 58 "health" : 1, 59 "state" : 2, 60 "stateStr" : "SECONDARY", 61 "uptime" : 1112736, 62 "optime" : { 63 "t" : 1330435339000, 64 "i" : 2413 65 }, 66 "optimeDate" : ISODate("2012-02-28T13:22:19Z"), 67 "lastHeartbeat" : ISODate("2012-02-29T12:02:45Z"), 68 "pingMs" : 0 69 }, 70 { 71 "_id" : 3, 72 "name" : "192.168.35.106:10003", 73 "health" : 1, 74 "state" : 1, 75 "stateStr" : "SECONDARY", 76 "uptime" : 1112736, 77 "optime" : { 78 "t" : 1330435339000, 79 "i" : 2413 80 }, 81 "optimeDate" : ISODate("2012-02-28T13:22:19Z"), 82 "lastHeartbeat" : ISODate("2012-02-29T12:02:46Z"), 83 "pingMs" : 0 84 } 85 ], 86 "ok" : 1 87 }
配置第二组sets
1 //10011 2 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10011/ -port=10011 --nohttpinterface --replSet set2 3 4 //10012 5 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10012/ -port=10012 --nohttpinterface --replSet set2 6 7 //10013 8 $ ./mongod --shardsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/10013/ -port=10013 --nohttpinterface --replSet set2
配置集群
1 $ ./mongo -port 10011 2 MongoDB shell version: 2.0.2 3 connecting to: 127.0.0.1:10011/test 4 >#配置集合1 5 >#构建参数 6 >cfg={_id:'set2', 7 members:[{_id:1,host:'192.168.35.106:10011'}, 8 {_id:1,host:'192.168.35.106:10012'}, 9 {_id:1,host:'192.168.35.106:10013'}] 10 }; 11 { 12 "_id":"set2", 13 "members":[ 14 { 15 "_id":1, 16 "host":"192.168.35.106:10011" 17 }, 18 { 19 "_id":2, 20 "host":"192.168.35.106:10012" 21 }, 22 { 23 "_id":3, 24 "host":"192.168.35.106:10013" 25 } 26 ] 27 } 28 >#让参数生效 29 >rs.initiate(cfg); 30 { 31 "info":"Config now saved locally. Should come online in about aminute.", 32 "OK":1 33 } 34 >#查看运行状态 35 >rs.status(); 36 { 37 "set" : "set2", 38 "date" : ISODate("2012-02-29T12:12:46Z"), 39 "myState" : 2, 40 "syncingTo" : "192.168.35.106:10011", 41 "members" : [ 42 { 43 "_id" : 1, 44 "name" : "192.168.35.106:10011", 45 "health" : 1, 46 "state" : 2, 47 "stateStr" : "PRIMARY", 48 "optime" : { 49 "t" : 1330435339000, 50 "i" : 2413 51 }, 52 "optimeDate" : ISODate("2012-02-28T13:22:19Z"), 53 "self" : true 54 }, 55 { 56 "_id" : 2, 57 "name" : "192.168.35.106:10012", 58 "health" : 1, 59 "state" : 2, 60 "stateStr" : "SECONDARY", 61 "uptime" : 1112736, 62 "optime" : { 63 "t" : 1330435339000, 64 "i" : 2413 65 }, 66 "optimeDate" : ISODate("2012-02-28T13:22:19Z"), 67 "lastHeartbeat" : ISODate("2012-02-29T12:02:45Z"), 68 "pingMs" : 0 69 }, 70 { 71 "_id" : 3, 72 "name" : "192.168.35.106:10013", 73 "health" : 1, 74 "state" : 1, 75 "stateStr" : "SECONDARY", 76 "uptime" : 1112736, 77 "optime" : { 78 "t" : 1330435339000, 79 "i" : 2413 80 }, 81 "optimeDate" : ISODate("2012-02-28T13:22:19Z"), 82 "lastHeartbeat" : ISODate("2012-02-29T12:02:46Z"), 83 "pingMs" : 0 84 } 85 ], 86 "ok" : 1 87 }
启用config servier
1 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config1/ -port=20000 --nohttpinterface 2 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config2/ -port=20001 --nohttpinterface 3 $ ./mongod --configsvr --fork -logpath=/home/data/logs/null -dbpath=/home/data/config3/ -port=20002 --nohttpinterface
启用Route server
1 $ ./mongos -configdb="192.168.35.106:20000,192.168.35.106:20001,192.168.35.106:20002" --fork -logpath=/home/data/logs/null 2 >#查看是否正常运行 3 >ps aux|grep mongos|grep -v grep 4 root 2726 0.0 0.1 174192 3392 ? Sl Feb16 3:20 ./mongos -configdb=192.168.35.106:20000,192.168.35.106:20001,192.168.35.106:20002 --fork -logpath=/home/data/logs/null
开始配置Sharding
1 $ ./mongo -port 10001 2 MongoDB shell version: 2.0.2 3 connecting to: 127.0.0.1:10001/test 4 >#进入管理数据库 5 >use admin 6 switched to db admin 7 >#添加Sharding 8 > db.runcommand({addshard:'set1/192.168.35.106:10001,192.168.35.106:10002,192.168.35.106:10003'}) 9 {"shardAdded":"set1","ok":1} 10 > db.runCommand({addshard:'set2/192.168.35.106:10011,192.168.35.106:10012,192.168.35.106:10013'}) 11 {"shardAdded":"set2","ok":1} 12 >#让test数据库支持Sharding 13 >db.runCommand({enablesharding:'test'}) 14 {"ok":1} 15 >#让数据库中的一个集合生效,且根据ID来进行分片 16 >db.runCommand({shardcollection:'test.user',key:{_id:1}}) 17 {"collectionshrded":"test.user","ok":1} 18 19 注:另外可以通过下面两个命令查看sharding情况: 20 >db.runCommand({listshards:1} 21 >printShardingStatus();