sharded.js 931 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const co = require('co');
  3. co(function*() {
  4. var Sharded = require('mongodb-topology-manager').Sharded;
  5. // Create new instance
  6. var topology = new Sharded({
  7. mongod: 'mongod',
  8. mongos: 'mongos'
  9. });
  10. yield topology.addShard([{
  11. options: {
  12. bind_ip: 'localhost', port: 31000, dbpath: `/data/db/31000`, shardsvr: null
  13. }
  14. }], { replSet: 'rs1' });
  15. yield topology.addConfigurationServers([{
  16. options: {
  17. bind_ip: 'localhost', port: 35000, dbpath: `/data/db/35000`
  18. }
  19. }], { replSet: 'rs0' });
  20. yield topology.addProxies([{
  21. bind_ip: 'localhost', port: 51000, configdb: 'localhost:35000'
  22. }], {
  23. binary: 'mongos'
  24. });
  25. console.log('Start...');
  26. // Start up topology
  27. yield topology.start();
  28. console.log('Started');
  29. // Shard db
  30. yield topology.enableSharding('test');
  31. console.log('done');
  32. }).catch(error => {
  33. console.error(error);
  34. process.exit(-1);
  35. });