情景:項(xiàng)目鏈接數(shù)據(jù)庫,出現(xiàn)超時(shí)錯(cuò)誤:
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)]: {}
},
上網(wǎng)找了下罐脊,有人說沒有啟動(dòng)mongo:
那我就在命令行啟動(dòng)唄定嗓,報(bào)錯(cuò):
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
又有人說要先啟動(dòng)mongod,我照做了萍桌,又報(bào)錯(cuò):
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
但是宵溅,不要怕,報(bào)錯(cuò)是為了提醒你錯(cuò)在哪里上炎,仔細(xì)看錯(cuò)誤提示:
NonExistentPath: Data directory E:\data\db\ not found.
問題就很清楚了恃逻,mongodb需要一個(gè)數(shù)據(jù)庫存放位置,那我就創(chuàng)建一個(gè):
運(yùn)行mongod:
出現(xiàn)一堆東西藕施,其中有一些警告不能忽視寇损,你以后可能要用到:
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 <address> 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.沒有權(quán)限控制。
2.此服務(wù)器只綁定了本地服務(wù)器(只能本地訪問)裳食,如果要開啟遠(yuǎn)程訪問矛市,請(qǐng)按提示操作。
至此诲祸,說明新的數(shù)據(jù)庫已經(jīng)建立成功浊吏,再次進(jìn)行本地訪問,成功烦绳!
(注意卿捎,運(yùn)行了mongod的窗口不能關(guān)閉,它現(xiàn)在就相當(dāng)于運(yùn)行中的數(shù)據(jù)庫服務(wù)器径密。)
快捷驗(yàn)證方式:
打開瀏覽器,輸入:
http://localhost:27017
結(jié)果:
成功躺孝!
如果你的錯(cuò)誤不同享扔,請(qǐng)務(wù)必好好看報(bào)錯(cuò)信息!答案很可能已經(jīng)給出了植袍。
問題2:如果我原本就有一個(gè)mongoDB數(shù)據(jù)庫惧眠,現(xiàn)在我想改路徑怎么辦?
方法:配置路徑:
mongod --dbpath 你的數(shù)據(jù)庫路徑
例子:
mongod --dbpath E:\MongoDB\Server\4.2\data
至此于个,原本的數(shù)據(jù)庫就啟動(dòng)了氛魁!