0

So in node I have:

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/master');
let db = mongoose.connection();
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
  console.log('conn');
});

But nothing ever prints out and if I try and query the db from node it hangs forever without errors. I can open mongo shell fine and create a new collection, insert items, etc.

And this is the log for Mongo:

2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/mongodb-master/ 64-bit host=16b45d9af5c4
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] db version v3.2.11
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] modules: none
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] build environment:
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten]     distmod: ubuntu1404
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten]     distarch: x86_64
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2016-12-02T03:52:14.104+0000 I CONTROL  [initandlisten] options: { net: { port: 27017 }, storage: { dbPath: "/mongodb-master/" } }
2016-12-02T03:52:14.111+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-12-02T03:52:14.683+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-12-02T03:52:14.683+0000 I CONTROL  [initandlisten]
2016-12-02T03:52:14.685+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/mongodb-master/diagnostic.data'
2016-12-02T03:52:14.685+0000 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-12-02T03:52:14.847+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

2016-12-02T03:52:25.898+0000 I NETWORK  [initandlisten] connection accepted from 172.17.0.1:46140 #1 (1 connection now open)
2016-12-02T03:52:25.903+0000 I NETWORK  [initandlisten] connection accepted from 172.17.0.1:46142 #2 (2 connections now open)
2016-12-02T03:52:25.905+0000 I NETWORK  [initandlisten] connection accepted from 172.17.0.1:46144 #3 (3 connections now open)
2016-12-02T03:52:25.907+0000 I NETWORK  [initandlisten] connection accepted from 172.17.0.1:46146 #4 (4 connections now open)
2016-12-02T03:52:25.909+0000 I NETWORK  [initandlisten] connection accepted from 172.17.0.1:46148 #5 (5 connections now open)

2 Answers2

0

Try to do:

  this.connection = mongoose.createConnection('mongodb://localhost:27017', { server: { reconnectTries: Number.MAX_VALUE } });

However, you then have to create models in a different way. Have a look here

Community
  • 1
  • 1
kimy82
  • 4,069
  • 1
  • 22
  • 25
  • Still nothing. I also tried just cloning the repo [here](http://theholmesoffice.com/mongoose-connection-best-practice/) and running it but no luck - same issue. – FergusThePlant Dec 04 '16 at 23:14
0

Fixed this by uninstalling and reinstalling node and mongodb with brew and then rebooting and reinstalling (make sure to delete node_modules in project repository and reinstall)