连接到MongoDB时出错= MongoError:在第一次连接时无法连接到服务器[localhost:27017]

时间:2017-03-02 07:28:40

标签: node.js mongodb express mongoose homebrew

连接到MongoDB时遇到问题,因为我在运行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)

这是我的app.js文件:

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);
});

现在,当我在终端中运行mongod时,我得到以下错误:

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

我已经创建了一个/data/db文件夹,并将以下导出添加到我的.bash_profile中:

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

另一个额外信息是我通过Brew安装了MongoDB。

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

.lock

由于/data/db文件夹的权限有限,您还必须创建--dbpath文件。您可以自己创建,也可以在当前用户也可以访问的文件夹上设置function data_to_csv($data, $headers = TRUE, $filename= "") { if ( ! is_array($data)) { show_error('invalid Data provided'); } $array = array(); if ($headers) { $array[] = array_keys($data[0]); } foreach ($data as $row) { $line = array(); foreach ($row as $item) { $line[] = $item; } $array[] = $line; } header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=\"$filename".".csv\""); header("Pragma: no-cache"); header("Expires: 0"); $handle = fopen('php://output', 'w'); foreach ($array as $array) { fputcsv($handle, $array); } fclose($handle); exit; }

这里是similar answer on SO