Mongodb正在运行但仍然无法连接

时间:2017-02-12 15:13:52

标签: javascript node.js mongodb

这是我运行service mongod status时得到的:

    ● mongod.service - LSB: An object/document-oriented database
   Loaded: loaded (/etc/init.d/mongod; bad; vendor preset: enabled)
   Active: active (running) since Sun 2017-02-12 15:05:42 UTC; 4min 59s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 6984 ExecStop=/etc/init.d/mongod stop (code=exited, status=0/SUCCESS)
  Process: 6998 ExecStart=/etc/init.d/mongod start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mongod.service
           └─7009 /usr/bin/mongod --config /etc/mongod.conf

Feb 12 15:05:41 WGJG008 systemd[1]: Stopped LSB: An object/document-oriented database.
Feb 12 15:05:41 WGJG008 systemd[1]: Starting LSB: An object/document-oriented database...
Feb 12 15:05:41 WGJG008 mongod[6998]:  * Starting database mongod
Feb 12 15:05:42 WGJG008 mongod[6998]:    ...done.
Feb 12 15:05:42 WGJG008 systemd[1]: Started LSB: An object/document-oriented database.
Feb 12 15:08:25 WGJG008 systemd[1]: Started LSB: An object/document-oriented database.

然而每当我运行我的服务器时

sudo node server.js

我收到以下错误消息:

MongoError: failed to connect to server [soren_site:27017] on first connect
at null.<anonymous> (/var/www/soren_api/node_modules/mongodb-core/lib/topologies/server.js:325:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/var/www/soren_api/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/var/www/soren_api/node_modules/mongodb-core/lib/connection/connection.js:173:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:998:8)
at nextTickCallbackWith2Args (node.js:441:9)
at process._tickCallback (node.js:355:17)

我通过以下方式连接到我的服务器:

mongoose.connect('mongodb://soren_site');

那么有谁能告诉我我做错了什么?

mongod的输出:

    2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] MongoDB starting : pid=7726 port=27017 dbpath=/data/db 64-bit host=WGJG008
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] db version v3.0.14
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] git version: 08352afcca24bfc145240a0fac9d28b978ab77f3
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] build info: Linux ip-10-30-223-232 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] options: {}
2017-02-12T15:29:21.243+0000 E NETWORK  [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2017-02-12T15:29:21.243+0000 E NETWORK  [initandlisten]   addr already in use
2017-02-12T15:29:21.243+0000 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-02-12T15:29:21.244+0000 I CONTROL  [initandlisten] dbexit:  rc: 100

1 个答案:

答案 0 :(得分:1)

这看起来像主机名解决问题。

确保您在连接字符串中传递的主机名是可解析的。

此外,如果找不到,您可能需要在/ etc / hosts

中添加条目

希望这有帮助