MongoDB错误:无法连接到服务器127.0.0.1:27017

时间:2018-12-18 01:36:23

标签: javascript node.js mongodb

在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文件目录添加到我的path变量中,并创建了mongod指向的/ data / db路径,我仍然看到这个错误。 好消息是,我可以通过mongo控制台在此数据库上进行读取/写入,但是我的目标是能够在node上运行它。尝试运行节点项目时,我看到类似的错误:

➜  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;

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我建议您做的是检查/ var / log / mongodb。 就我而言,我收到了以下消息:

无法取消套接字文件/tmp/mongodb-27017.sock的链接操作失败 在以下位置允许致命断言40486 src / mongo / transport / transport_layer_asio.cpp 68

因此,我尝试卸下袜子并重新启动了无法启动的mongod服务。我不确定为什么会发生这种情况,但是它已经解决了问题

library(tidyverse)
df_1 <- tibble::tribble(
  ~sub_date, ~value_1, ~value_2,
  "2020-05",       58,      "130",
  "2020-05",       58,       "check",
  "2020-03",       50,      "120",
  "2020-03",       55,       "check",
  "2020-03",       55,       "check"
)

df_ref <- tibble::tribble(
  ~sub_date, ~ref_value,
  "2020-05",        123,
  "2020-03",        234
)

df_1 %>% 
  left_join(df_ref) %>%
  mutate(value_2_true = ifelse(value_2 == "check",ref_value,value_2)) %>%
  mutate(value_2 = value_2_true %>% as.numeric()) %>% 
  select(-value_2_true,-ref_value)