sharded.js 921 B

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