title: MongoDB error: couldn't connect to server 127.0.0.1:27017
body: <p><strong>When running "mongo" (even as sudo) on macOS I'm getting the following error:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>➜  server git:(master) ✗ mongo
  MongoDB shell version v4.0.4
  connecting to: mongodb://127.0.0.1:27017
  2018-12-17T17:14:54.659-0800 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
  connect@src/mongo/shell/mongo.js:257:13
  @(connect):1:6
  exception: connect failed</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>However when I run "mongod" everything seems to be running smoothely besides a few warning messages:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>➜  server git:(master) ✗ sudo mongod
  2018-12-17T17:25:51.718-0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten] MongoDB starting : pid=5919 port=27017 dbpath=/data/db 64-bit host=Aarons-MacBook-Air-3.local
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten] db version v4.0.4
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten] allocator: system
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten] modules: none
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten] build environment:
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten]     distarch: x86_64
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten]     target_arch: x86_64
  2018-12-17T17:25:51.732-0800 I CONTROL  [initandlisten] options: {}
  2018-12-17T17:25:51.733-0800 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
  2018-12-17T17:25:51.733-0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
  2018-12-17T17:25:52.678-0800 I STORAGE  [initandlisten] WiredTiger message [1545096352:678713][5919:0x10bfe35c0], txn-recover: Main recovery loop: starting at 8/6528 to 9/256
  2018-12-17T17:25:52.846-0800 I STORAGE  [initandlisten] WiredTiger message [1545096352:846951][5919:0x10bfe35c0], txn-recover: Recovering log 8 through 9
  2018-12-17T17:25:52.961-0800 I STORAGE  [initandlisten] WiredTiger message [1545096352:961090][5919:0x10bfe35c0], txn-recover: Recovering log 9 through 9
  2018-12-17T17:25:53.044-0800 I STORAGE  [initandlisten] WiredTiger message [1545096353:44078][5919:0x10bfe35c0], txn-recover: Set global recovery timestamp: 0
  2018-12-17T17:25:53.174-0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
  2018-12-17T17:25:53.295-0800 I CONTROL  [initandlisten] 
  2018-12-17T17:25:53.295-0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
  2018-12-17T17:25:53.295-0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
  2018-12-17T17:25:53.295-0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
  2018-12-17T17:25:53.295-0800 I CONTROL  [initandlisten] 
  2018-12-17T17:25:53.295-0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
  2018-12-17T17:25:53.295-0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
  2018-12-17T17:25:53.296-0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip &lt;address&gt; to specify which IP 
  2018-12-17T17:25:53.296-0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
  2018-12-17T17:25:53.296-0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
  2018-12-17T17:25:53.296-0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
  2018-12-17T17:25:53.296-0800 I CONTROL  [initandlisten] 
  2018-12-17T17:25:53.296-0800 I CONTROL  [initandlisten] 
  2018-12-17T17:25:53.296-0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
  2018-12-17T17:25:53.396-0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
  2018-12-17T17:25:53.403-0800 I NETWORK  [initandlisten] waiting for connections on port 27017</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>I thought it was incorrectly installed at first, but even after downloading the tar file, adding the bin file directory to my path variable, and creating the /data/db path to which mongod points to I am still seeing this error.
  The good news is that I can read/write on this database through the mongo console, but my goal is to be able to run it with node. I see a similar error when attempting to run my node project:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>➜  server git:(master) ✗ node index.js 
  (node:6064) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
      at Pool.&lt;anonymous&gt; (/Users/aaronmednick/code/aaronjospehm/mern_app/server/node_modules/mongodb-core/lib/topologies/server.js:564:11)
      at Pool.emit (events.js:182:13)
      at Connection.&lt;anonymous&gt; (/Users/aaronmednick/code/aaronjospehm/mern_app/server/node_modules/mongodb-core/lib/connection/pool.js:317:12)
      at Object.onceWrapper (events.js:273:13)
      at Connection.emit (events.js:182:13)
      at Socket.&lt;anonymous&gt; (/Users/aaronmednick/code/aaronjospehm/mern_app/server/node_modules/mongodb-core/lib/connection/connection.js:246:50)
      at Object.onceWrapper (events.js:273:13)
      at Socket.emit (events.js:182:13)
      at emitErrorNT (internal/streams/destroy.js:82:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
      at process.internalTickCallback (internal/process/next_tick.js:72:19)
  (node:6064) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:6064) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Here's my code for reference:</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const { GraphQLServer } = require('graphql-yoga');
  
  const mongoose = require("mongoose");
  
  mongoose.connect("mongodb://localhost/test5", { useNewUrlParser: true } );
  
  const Todo = mongoose.model('Todo', {
   text: String,
   complete: Boolean
  });
  
  const typeDefs = `
    type Query {
      hello(name: String): String!
    }
    type Todo {
     id: ID!
     text: String!
     complete: Boolean!
    }
    type Mutation {
     createTodo(test: String!): Todo
    }
  `
  
  const resolvers = {
    Query: {
      hello: (_, { name }) =&gt; `Hello ${name || 'World'}`,
    },
    Mutation: {
     createTodo: async (_, { text }) =&gt; {
      const todo = new Todo({ text, complete: false});
      await todo.save();
      return todo;
     }
    }
  }
  
  const server = new GraphQLServer({ typeDefs, resolvers })
  
  mongoose.connection.once('open', function() {
   server.start(() =&gt; console.log('Server is running on localhost:4000'));
  });
  
  module.exports = mongoose;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Any ideas?</strong></p>