簡體   English   中英

MongoDB 錯誤:無法連接到服務器 127.0.0.1:27017

[英]MongoDB error: couldn't connect to server 127.0.0.1:27017

在 macOS 上運行“mongo”(甚至作為 sudo)時,我收到以下錯誤:

 ➜ server git:(master) ✗ mongo MongoDB shell version v4.0.4 connecting to: mongodb://127.0.0.1:27017 2018-12-17T17:14:54.659-0800 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 :: Connection refused : connect@src/mongo/shell/mongo.js:257:13 @(connect):1:6 exception: connect failed

但是,當我運行“mongod”時,除了一些警告消息外,一切似乎都在順利運行:

 ➜ server git:(master) ✗ sudo mongod 2018-12-17T17:25:51.718-0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] MongoDB starting : pid=5919 port=27017 dbpath=/data/db 64-bit host=Aarons-MacBook-Air-3.local 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] db version v4.0.4 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] allocator: system 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] modules: none 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] build environment: 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] distarch: x86_64 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] target_arch: x86_64 2018-12-17T17:25:51.732-0800 I CONTROL [initandlisten] options: {} 2018-12-17T17:25:51.733-0800 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. 2018-12-17T17:25:51.733-0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress), 2018-12-17T17:25:52.678-0800 I STORAGE [initandlisten] WiredTiger message [1545096352:678713][5919:0x10bfe35c0], txn-recover: Main recovery loop: starting at 8/6528 to 9/256 2018-12-17T17:25:52.846-0800 I STORAGE [initandlisten] WiredTiger message [1545096352:846951][5919:0x10bfe35c0], txn-recover: Recovering log 8 through 9 2018-12-17T17:25:52.961-0800 I STORAGE [initandlisten] WiredTiger message [1545096352:961090][5919:0x10bfe35c0], txn-recover: Recovering log 9 through 9 2018-12-17T17:25:53.044-0800 I STORAGE [initandlisten] WiredTiger message [1545096353:44078][5919:0x10bfe35c0], txn-recover: Set global recovery timestamp: 0 2018-12-17T17:25:53.174-0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0) 2018-12-17T17:25:53.295-0800 I CONTROL [initandlisten] 2018-12-17T17:25:53.295-0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-12-17T17:25:53.295-0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-12-17T17:25:53.295-0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2018-12-17T17:25:53.295-0800 I CONTROL [initandlisten] 2018-12-17T17:25:53.295-0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2018-12-17T17:25:53.295-0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2018-12-17T17:25:53.296-0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP 2018-12-17T17:25:53.296-0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2018-12-17T17:25:53.296-0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2018-12-17T17:25:53.296-0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2018-12-17T17:25:53.296-0800 I CONTROL [initandlisten] 2018-12-17T17:25:53.296-0800 I CONTROL [initandlisten] 2018-12-17T17:25:53.296-0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 2018-12-17T17:25:53.396-0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data' 2018-12-17T17:25:53.403-0800 I NETWORK [initandlisten] waiting for connections on port 27017

我一開始以為它安裝不正確,但即使在下載 tar 文件后,將 bin 文件目錄添加到我的路徑變量,並創建 mongod 指向的 /data/db 路徑,我仍然看到此錯誤。 好消息是我可以通過 mongo 控制台讀/寫這個數據庫,但我的目標是能夠用 node.js 運行它。 嘗試運行我的節點項目時,我看到了類似的錯誤:

 ➜ server git:(master) ✗ node index.js (node:6064) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] at Pool.<anonymous> (/Users/aaronmednick/code/aaronjospehm/mern_app/server/node_modules/mongodb-core/lib/topologies/server.js:564:11) at Pool.emit (events.js:182:13) at Connection.<anonymous> (/Users/aaronmednick/code/aaronjospehm/mern_app/server/node_modules/mongodb-core/lib/connection/pool.js:317:12) at Object.onceWrapper (events.js:273:13) at Connection.emit (events.js:182:13) at Socket.<anonymous> (/Users/aaronmednick/code/aaronjospehm/mern_app/server/node_modules/mongodb-core/lib/connection/connection.js:246:50) at Object.onceWrapper (events.js:273:13) at Socket.emit (events.js:182:13) at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3) at process.internalTickCallback (internal/process/next_tick.js:72:19) (node:6064) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:6064) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

這是我的代碼供參考:

 const { GraphQLServer } = require('graphql-yoga'); const mongoose = require("mongoose"); mongoose.connect("mongodb://localhost/test5", { useNewUrlParser: true } ); const Todo = mongoose.model('Todo', { text: String, complete: Boolean }); const typeDefs = ` type Query { hello(name: String): String! } type Todo { id: ID! text: String! complete: Boolean! } type Mutation { createTodo(test: String!): Todo } ` const resolvers = { Query: { hello: (_, { name }) => `Hello ${name || 'World'}`, }, Mutation: { createTodo: async (_, { text }) => { const todo = new Todo({ text, complete: false}); await todo.save(); return todo; } } } const server = new GraphQLServer({ typeDefs, resolvers }) mongoose.connection.once('open', function() { server.start(() => console.log('Server is running on localhost:4000')); }); module.exports = mongoose;

有任何想法嗎?

我建議你做的是檢查/var/log/mongodb。 就我而言,我收到了如下消息:

無法取消套接字文件 /tmp/mongodb-27017.sock 操作不允許致命斷言 40486 在 src/mongo/transport/transport_layer_asio.cpp 68

所以我試圖移除襪子並重新啟動未能啟動的 mongod 服務。 我不確定它為什么會發生,但它已經解決了問題

sudo rm -rf /tmp/mongodb-27017.sock
sudo service mongod start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM