w_lpz w_lpz -4 years ago 405
Node.js Question

Error when connecting to MongoDB = MongoError: failed to connect to server [localhost:27017] on first connect

Having issues connecting to MongoDB since I am getting the following error when I run

node app.js
:

Express server listening on port 30000

/Users/Wilo/Desktop/myway_stuff/myway_app/MyWay/server/node_modules/mongodb/lib/mongo_client.js:336
throw err
^
MongoError: failed to connect to server [localhost:27017] on first connect
at Pool.<anonymous> (/Users/Wilo/Desktop/myway_stuff/myway_app/MyWay/server/node_modules/mongodb-core/lib/topologies/server.js:326:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (/Users/Wilo/Desktop/myway_stuff/myway_app/MyWay/server/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (/Users/Wilo/Desktop/myway_stuff/myway_app/MyWay/server/node_modules/mongodb-core/lib/connection/connection.js:175:49)
at Socket.g (events.js:291:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1276:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)


This is my
app.js
file:

var express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
mongoose = require('mongoose'),
expressSession = require('express-session'),
MongoStore = require('connect-mongo')(expressSession),
accountRoutes = require('./routes/account'),
// bookingRoutes = require('./routes/bookings'),
app = express(),
port = 30000;

var dbName = 'mywayDB';
var connectionString = 'mongodb://localhost:27017/' + dbName;

mongoose.connect(connectionString);

app.use(expressSession({
secret: '128013A7-5B9F-4CC0-BD9E-4480B2D3EFE9',
resave: true,
saveUninitialized: true,
store: new MongoStore({
url: 'mongodb://localhost/test-app',
ttl: 20 * 24 * 60 * 60 // = 20 days.
})
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/api', [accountRoutes]);

var server = app.listen(port, function () {
console.log('Express server listening on port ' + server.address().port);
});


Now, when I run
mongod
in terminal, this is the following error I get:

2017-03-01T12:10:01.888-0800 I CONTROL [initandlisten] MongoDB starting : pid=4545 port=27017 dbpath=/data/db 64-bit host=WiLos-MacBook-Pro.local
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] db version v3.4.2
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2k 26 Jan 2017
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] allocator: system
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] modules: none
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] build environment:
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] distarch: x86_64
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] target_arch: x86_64
2017-03-01T12:10:01.889-0800 I CONTROL [initandlisten] options: {}
2017-03-01T12:10:01.889-0800 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
2017-03-01T12:10:01.889-0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-03-01T12:10:01.890-0800 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-03-01T12:10:01.890-0800 I CONTROL [initandlisten] now exiting
2017-03-01T12:10:01.890-0800 I CONTROL [initandlisten] shutting down with code:100


I have already created a
/data/db
folder and added the following exports into my
.bash_profile
:

export MONGO_PATH=/usr/local/Cellar/mongodb/3.4.2
export PATH=$PATH:$MONGO_PATH/bin


Another extra info is that I installed MongoDB via Brew.

Would appreciate any help with this. Thanks!

Answer Source
2017-03-01T12:10:01.889-0800 I STORAGE  [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating

You have to create also a .lock file due to the limited permissions of the /data/db folder. You could either create that yourself or set a --dbpath on a folder that the current user has access too.

Here's a similar answer on SO

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download