Advertisement
Guest User

Untitled

a guest
May 30th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1.  
  2.  
  3. # Andrew Erlichson
  4. # MongoDB
  5. # script to start a sharded environment on localhost
  6.  
  7. # clean everything up
  8. echo "killing mongod and mongos"
  9. killall mongod
  10. killall mongos
  11. echo "removing data files"
  12. rm -rf /data/config
  13. rm -rf /data/shard*
  14.  
  15.  
  16. # start a replica set and tell it that it will be shard0
  17. echo "starting servers for shard 0"
  18. mkdir -p /data/shard0/rs0 /data/shard0/rs1 /data/shard0/rs2
  19. mongod --replSet s0 --logpath "s0-r0.log" --dbpath /data/shard0/rs0 --port 37017 --fork --shardsvr --smallfiles
  20. mongod --replSet s0 --logpath "s0-r1.log" --dbpath /data/shard0/rs1 --port 37018 --fork --shardsvr --smallfiles
  21. mongod --replSet s0 --logpath "s0-r2.log" --dbpath /data/shard0/rs2 --port 37019 --fork --shardsvr --smallfiles
  22.  
  23. sleep 5
  24. # connect to one server and initiate the set
  25. echo "Configuring s0 replica set"
  26. mongo --port 37017 << 'EOF'
  27. config = { _id: "s0", members:[
  28. { _id : 0, host : "localhost:37017" },
  29. { _id : 1, host : "localhost:37018" },
  30. { _id : 2, host : "localhost:37019" }]};
  31. rs.initiate(config)
  32. EOF
  33.  
  34. # start a replicate set and tell it that it will be a shard1
  35. echo "starting servers for shard 1"
  36. mkdir -p /data/shard1/rs0 /data/shard1/rs1 /data/shard1/rs2
  37. mongod --replSet s1 --logpath "s1-r0.log" --dbpath /data/shard1/rs0 --port 47017 --fork --shardsvr --smallfiles
  38. mongod --replSet s1 --logpath "s1-r1.log" --dbpath /data/shard1/rs1 --port 47018 --fork --shardsvr --smallfiles
  39. mongod --replSet s1 --logpath "s1-r2.log" --dbpath /data/shard1/rs2 --port 47019 --fork --shardsvr --smallfiles
  40.  
  41. sleep 5
  42.  
  43. echo "Configuring s1 replica set"
  44. mongo --port 47017 << 'EOF'
  45. config = { _id: "s1", members:[
  46. { _id : 0, host : "localhost:47017" },
  47. { _id : 1, host : "localhost:47018" },
  48. { _id : 2, host : "localhost:47019" }]};
  49. rs.initiate(config)
  50. EOF
  51.  
  52. # start a replicate set and tell it that it will be a shard2
  53. echo "starting servers for shard 2"
  54. mkdir -p /data/shard2/rs0 /data/shard2/rs1 /data/shard2/rs2
  55. mongod --replSet s2 --logpath "s2-r0.log" --dbpath /data/shard2/rs0 --port 57017 --fork --shardsvr --smallfiles
  56. mongod --replSet s2 --logpath "s2-r1.log" --dbpath /data/shard2/rs1 --port 57018 --fork --shardsvr --smallfiles
  57. mongod --replSet s2 --logpath "s2-r2.log" --dbpath /data/shard2/rs2 --port 57019 --fork --shardsvr --smallfiles
  58.  
  59. sleep 5
  60.  
  61. echo "Configuring s2 replica set"
  62. mongo --port 57017 << 'EOF'
  63. config = { _id: "s2", members:[
  64. { _id : 0, host : "localhost:57017" },
  65. { _id : 1, host : "localhost:57018" },
  66. { _id : 2, host : "localhost:57019" }]};
  67. rs.initiate(config)
  68. EOF
  69.  
  70.  
  71. # now start 3 config servers
  72. echo "Starting config servers"
  73. mkdir -p /data/config/config-a /data/config/config-b /data/config/config-c
  74. mongod --logpath "cfg-a.log" --dbpath /data/config/config-a --port 57040 --fork --configsvr --smallfiles
  75. mongod --logpath "cfg-b.log" --dbpath /data/config/config-b --port 57041 --fork --configsvr --smallfiles
  76. mongod --logpath "cfg-c.log" --dbpath /data/config/config-c --port 57042 --fork --configsvr --smallfiles
  77.  
  78.  
  79. # now start the mongos on a standard port
  80. mongos --logpath "mongos-1.log" --configdb localhost:57040,localhost:57041,localhost:57042 --fork
  81. echo "Waiting 60 seconds for the replica sets to fully come online"
  82. sleep 60
  83. echo "Connnecting to mongos and enabling sharding"
  84.  
  85. # add shards and enable sharding on the test db
  86. mongo <<'EOF'
  87. db.adminCommand( { addshard : "s0/"+"localhost:37017" } );
  88. db.adminCommand( { addshard : "s1/"+"localhost:47017" } );
  89. db.adminCommand( { addshard : "s2/"+"localhost:57017" } );
  90. db.adminCommand({enableSharding: "school"})
  91. db.adminCommand({shardCollection: "school.students", key: {student_id:1}});
  92. EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement