connect ECONNREFUSED 127.0.0.1:27017

情景:项目链接数据库,出现超时错误:

name: 'MongoTimeoutError',
  reason: Error: connect ECONNREFUSED 127.0.0.1:27017
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1126:14) {
    name: 'MongoNetworkError',
    errorLabels: [ 'TransientTransactionError' ],
    [Symbol(mongoErrorContextSymbol)]: {}
  },

上网找了下,有人说没有启动mongo:
那我就在命令行启动呗,报错:

E:\MongoDB\Server\4.2>mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2019-11-13T03:34:05.193-0700 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 :: ����Ŀ�����������ܾ����޷� ���ӡ� :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2019-11-13T03:34:05.197-0700 F  -        [main] exception: connect failed
2019-11-13T03:34:05.201-0700 E  -        [main] exiting with code 1

又有人说要先启动mongod,我照做了,又报错:

E:\MongoDB\Server\4.2>mongod
2019-11-13T18:32:02.963+0800 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-11-13T18:32:03.418+0800 I  CONTROL  [initandlisten] MongoDB starting : pid=16184 port=27017 dbpath=E:\data\db\ 64-bit host=DESKTOP-7V4UGDG
2019-11-13T18:32:03.418+0800 I  CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2019-11-13T18:32:03.418+0800 I  CONTROL  [initandlisten] db version v4.2.1
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten] git version: edf6d45851c0b9ee15548f0f847df141764a317e
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten] allocator: tcmalloc
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten] modules: enterprise
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten] build environment:
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten]     distmod: windows-64
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten]     distarch: x86_64
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten]     target_arch: x86_64
2019-11-13T18:32:03.419+0800 I  CONTROL  [initandlisten] options: {}
2019-11-13T18:32:03.420+0800 I  STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory E:\data\db\ not found., terminating
2019-11-13T18:32:03.420+0800 I  NETWORK  [initandlisten] shutdown: going to close listening sockets...
2019-11-13T18:32:03.420+0800 I  -        [initandlisten] Stopping further Flow Control ticket acquisitions.
2019-11-13T18:32:03.420+0800 I  CONTROL  [initandlisten] now exiting
2019-11-13T18:32:03.421+0800 I  CONTROL  [initandlisten] shutting down with code:100

但是,不要怕,报错是为了提醒你错在哪里,仔细看错误提示:

NonExistentPath: Data directory E:\data\db\ not found.

问题就很清楚了,mongodb需要一个数据库存放位置,那我就创建一个:


data\db.png

运行mongod:
出现一堆东西,其中有一些警告不能忽视,你以后可能要用到:

WARNING: Access control is not enabled for the database.
WARNING: This server is bound to localhost.
Remote systems will be unable to connect to this server.
Start the server with --bind_ip 
to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning.

1.没有权限控制。
2.此服务器只绑定了本地服务器(只能本地访问),如果要开启远程访问,请按提示操作。
至此,说明新的数据库已经建立成功,再次进行本地访问,成功!
(注意,运行了mongod的窗口不能关闭,它现在就相当于运行中的数据库服务器。)
快捷验证方式:
打开浏览器,输入:

http://localhost:27017

结果:


image.png

成功!
如果你的错误不同,请务必好好看报错信息!答案很可能已经给出了。


问题2:如果我原本就有一个mongoDB数据库,现在我想改路径怎么办?
方法:配置路径:

mongod --dbpath 你的数据库路径

例子:

mongod --dbpath E:\MongoDB\Server\4.2\data

至此,原本的数据库就启动了!

你可能感兴趣的:(connect ECONNREFUSED 127.0.0.1:27017)