title: MongoTimeoutError: Server selection timed out after 30000 ms while connecting to mongoDB
body: <p>Code works fine when I connect to mongodb://localhost:27017/myapp
  <br></p>
  
  <p>But when I'm trying to connect to the MongoDB MLab DB account, code gives <strong>MongoTimeoutError: Server selection timed out after 30000 ms</strong></p>
  
  <p>more links - <a href="https://github.com/Automattic/mongoose/issues/8485" rel="nofollow noreferrer">https://github.com/Automattic/mongoose/issues/8485</a></p>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>const http = require('http');
  const app = require('./app');
  const port = process.env.PORT || 3000;
  const mongoose = require(`mongoose`);
  const key = require('./config');
  
  const server = http.createServer(app);
  server.listen(port, () =&gt; {
      console.log(`server running on ${port}`);
  });
  mongoose.set('useUnifiedTopology', true);
  mongoose.set('useFindAndModify', false);
  const url = process.env.MONGODB_URI || key.MONGO_URL;
  
  const option = {
      useNewUrlParser: true
  };
  
  mongoose.Promise = global.Promise;
  mongoose.connect(url, option).then(() =&gt; console.log('Database connected'))
      .catch((err) =&gt; console.log(err));
  </code></pre>
  
  <p><strong>ERROR IN CONSOLE</strong></p>
  
  <pre><code>    at new MongooseTimeoutError (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/node_modules/mongoose/lib/error/timeout.js:22:11)
      at NativeConnection.Connection.openUri (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/node_modules/mongoose/lib/connection.js:763:19)
      at Mongoose.connect (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/node_modules/mongoose/lib/index.js:332:15)
      at Object.&lt;anonymous&gt; (/home/sachin/Desktop/Freelancing/Sahi Price/Data Upload/server.js:20:10)
      at Module._compile (internal/modules/cjs/loader.js:778:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
      at Module.load (internal/modules/cjs/loader.js:653:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
      at Function.Module._load (internal/modules/cjs/loader.js:585:3)
      at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
      at startup (internal/bootstrap/node.js:283:19)
      at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
    message: 'Server selection timed out after 30000 ms',
    name: 'MongooseTimeoutError',
    [Symbol(mongoErrorContextSymbol)]: {} }
  </code></pre>