服务器选择在 10000 毫秒后超时 - 无法将 Compass 连接到本地主机上的 mongoDB [英] Server selection timed out after 10000 ms - Cannot connect Compass to mongoDB on localhost

查看:47
本文介绍了服务器选择在 10000 毫秒后超时 - 无法将 Compass 连接到本地主机上的 mongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 MongoDB 并尝试将 Compass 连接到我本地电脑上的数据库,但每次都在 Compass 上不断收到错误服务器选择在 10000 毫秒后超时",

这是我终端的登录:

 ~$ mongod2019-09-30T08:51:56.372+0700 I CONTROL [main] 自动禁用 TLS 1.0,强制启用 TLS 1.0 指定 --sslDisabledProtocols 'none'2019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] MongoDB 启动:pid=4661 port=27017 dbpath=/data/db 64-bit host=dell2019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] db version v4.2.02019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] git 版本:a4b751dcf51dd249c5865812b390cfd1c0129c302019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] OpenSSL 版本:OpenSSL 1.1.1 2018 年 9 月 11 日2019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] 分配器:tcmalloc2019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] 模块:无2019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] 构建环境:2019-09-30T08:51:56.377+0700 我控制 [initandlisten] distmod: ubuntu18042019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] distarch: x86_642019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] target_arch: x86_642019-09-30T08:51:56.377+0700 I CONTROL [initandlisten] 选项:{}2019-09-30T08:51:56.377+0700 I STORAGE [initandlisten] 在/data/db 中检测到由wiredTiger"存储引擎创建的数据文件,因此将活动存储引擎设置为wiredTiger".2019-09-30T08:51:56.377+0700 I STORAGE [initandlisten]2019-09-30T08:51:56.377+0700 I STORAGE [initandlisten] ** 警告:强烈建议在 WiredTiger 存储引擎中使用 XFS 文件系统2019-09-30T08:51:56.377+0700 I STORAGE [initandlisten] ** 见 http://dochub.mongodb.org/core/prodnotes-filesystem2019-09-30T08:51:56.377+0700 I STORAGE [initandlisten]wiredtiger_open 配置:创建,cache_size=3427M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_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,checkpoint_progress],2019-09-30T08:51:57.030+0700 I STORAGE [initandlisten] WiredTiger 消息 [1569808317:30575][4661:0x7f5edea92b00],txn-recover:通过恢复日志 42019-09-30T08:51:57.072+0700 I STORAGE [initandlisten] WiredTiger 消息 [1569808317:72698][4661:0x7f5edea92b00],txn-recover:通过恢复日志 52019-09-30T08:51:57.130+0700 I STORAGE [initandlisten] WiredTiger 消息 [1569808317:130714][4661:0x7f5edea92b00], txn-recover: Main4/6/124 起始于 62019-09-30T08:51:57.209+0700 I STORAGE [initandlisten] WiredTiger 消息 [1569808317:209489][4661:0x7f5edea92b00],txn-recover:通过恢复日志 42019-09-30T08:51:57.292+0700 I STORAGE [initandlisten] WiredTiger 消息 [1569808317:291996][4661:0x7f5edea92b00],txn-recover:通过恢复日志 52019-09-30T08:51:57.340+0700 I STORAGE [initandlisten] WiredTiger 消息 [1569808317:340285][4661:0x7f5edea92b00],txn-recover:设置全局恢复时间戳:(02019-09-30T08:51:57.597+0700 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp.Ts:时间戳(0, 0)2019-09-30T08:51:57.601+0700 I STORAGE [initandlisten] 时间戳监视器启动2019-09-30T08:51:57.672+0700 我控制 [initandlisten]2019-09-30T08:51:57.672+0700 I CONTROL [initandlisten] ** 警告:未为数据库启用访问控制.2019-09-30T08:51:57.672+0700 I CONTROL [initandlisten] ** 对数据和配置的读写访问不受限制.2019-09-30T08:51:57.672+0700 我控制 [initandlisten]2019-09-30T08:51:57.672+0700 I CONTROL [initandlisten] ** 警告:此服务器绑定到本地主机.2019-09-30T08:51:57.672+0700 I CONTROL [initandlisten] ** 远程系统将无法连接到此服务器.2019-09-30T08:51:57.672+0700 I CONTROL [initandlisten] ** 使用 --bind_ip 

更新:当我在本地主机上连接到我的数据库时卡在慢速连接后,我终于知道在这种情况下发生了什么,

如果我们让主机名是localhost,连接需要很长时间(超过10s),这就是连接超时的原因

我们必须将本地主机更改为 127.0.0.1 并且它会像风一样快.

如果你让 Node.js 中的连接字符串是 127.0.0.1 而不是 localhost

也会发生同样的事情

之前:

服务器运行在6969端口!数据库连接成功!10046.88992600143 毫秒

之后:

服务器运行在6969端口!数据库连接成功!43.67024099826813 毫秒

您可以在此处获取更多信息:https://github.com/Automattic/mongoose/问题/6211

Mongoose 文档也提到了这一点:

<块引用>

如果在您的机器上连接失败,请尝试使用 127.0.0.1 而不是 localhost.https://mongoosejs.com/docs/connections.html

I'm studying about MongoDB and trying to connect Compass to my db on my local pc but keep getting the error "Server selection timed out after 10000 ms" on Compass everytime,

here is the log in my terminal :

    ~$ mongod
2019-09-30T08:51:56.372+0700 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] MongoDB starting : pid=4661 port=27017 dbpath=/data/db 64-bit host=dell
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] db version v4.2.0
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] allocator: tcmalloc
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] modules: none
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] build environment:
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten]     distmod: ubuntu1804
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten]     distarch: x86_64
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten]     target_arch: x86_64
2019-09-30T08:51:56.377+0700 I  CONTROL  [initandlisten] options: {}
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] 
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3427M,cache_overflow=(file_max=0M),session_max=33000,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,checkpoint_progress],
2019-09-30T08:51:57.030+0700 I  STORAGE  [initandlisten] WiredTiger message [1569808317:30575][4661:0x7f5edea92b00], txn-recover: Recovering log 4 through 5
2019-09-30T08:51:57.072+0700 I  STORAGE  [initandlisten] WiredTiger message [1569808317:72698][4661:0x7f5edea92b00], txn-recover: Recovering log 5 through 5
2019-09-30T08:51:57.130+0700 I  STORAGE  [initandlisten] WiredTiger message [1569808317:130714][4661:0x7f5edea92b00], txn-recover: Main recovery loop: starting at 4/6144 to 5/256
2019-09-30T08:51:57.209+0700 I  STORAGE  [initandlisten] WiredTiger message [1569808317:209489][4661:0x7f5edea92b00], txn-recover: Recovering log 4 through 5
2019-09-30T08:51:57.292+0700 I  STORAGE  [initandlisten] WiredTiger message [1569808317:291996][4661:0x7f5edea92b00], txn-recover: Recovering log 5 through 5
2019-09-30T08:51:57.340+0700 I  STORAGE  [initandlisten] WiredTiger message [1569808317:340285][4661:0x7f5edea92b00], txn-recover: Set global recovery timestamp: (0,0)
2019-09-30T08:51:57.597+0700 I  RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-09-30T08:51:57.601+0700 I  STORAGE  [initandlisten] Timestamp monitor starting
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] 
2019-09-30T08:51:57.681+0700 I  SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
2019-09-30T08:51:57.681+0700 I  STORAGE  [initandlisten] Flow Control is enabled on this deployment.
2019-09-30T08:51:57.681+0700 I  SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
2019-09-30T08:51:57.681+0700 I  SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
2019-09-30T08:51:57.682+0700 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
2019-09-30T08:51:57.682+0700 I  FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-09-30T08:51:57.683+0700 I  SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
2019-09-30T08:51:57.684+0700 I  NETWORK  [initandlisten] Listening on /tmp/mongodb-27017.sock
2019-09-30T08:51:57.684+0700 I  NETWORK  [initandlisten] Listening on 127.0.0.1
2019-09-30T08:51:57.684+0700 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
2019-09-30T08:51:57.684+0700 I  NETWORK  [initandlisten] waiting for connections on port 27017
2019-09-30T08:56:56.140+0700 I  NETWORK  [listener] connection accepted from 127.0.0.1:59152 #1 (1 connection now open)
2019-09-30T08:56:56.142+0700 I  NETWORK  [conn1] received client metadata from 127.0.0.1:59152 conn1: { driver: { name: "nodejs-core", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "5.0.0-29-generic" }, platform: "Node.js v10.2.0, LE", application: { name: "MongoDB Compass" } }
2019-09-30T08:56:56.143+0700 I  NETWORK  [conn1] end connection 127.0.0.1:59152 (0 connections now open)

Actually I cannot see any other error so I dont know how to google.

As you can see, the mongo receive client request from compass but it closed immediately

2019-09-30T08:51:57.684+0700 I  NETWORK  [initandlisten] Listening on /tmp/mongodb-27017.sock
2019-09-30T08:51:57.684+0700 I  NETWORK  [initandlisten] Listening on 127.0.0.1
2019-09-30T08:51:57.684+0700 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
2019-09-30T08:51:57.684+0700 I  NETWORK  [initandlisten] waiting for connections on port 27017
2019-09-30T08:56:56.140+0700 I  NETWORK  [listener] connection accepted from 127.0.0.1:59152 #1 (1 connection now open)
2019-09-30T08:56:56.142+0700 I  NETWORK  [conn1] received client metadata from 127.0.0.1:59152 conn1: { driver: { name: "nodejs-core", version: "3.2.7" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "5.0.0-29-generic" }, platform: "Node.js v10.2.0, LE", application: { name: "MongoDB Compass" } }
2019-09-30T08:56:56.143+0700 I  NETWORK  [conn1] end connection 127.0.0.1:59152 (0 connections now open)
=

Both Compass and MongoDB are running in the same machine, that runs on Ubuntu 18.04

here is another terminal that I run 'mongo' command

~$ mongo
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c60d27ad-75d1-4a8c-83c8-6bb5482fd745") }
MongoDB server version: 4.2.0
Server has startup warnings: 
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] 
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-09-30T08:51:56.377+0700 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-09-30T08:51:57.672+0700 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 

Thanks for reading,

解决方案

look like I have to type 127.0.0.1 instead keep default and everything is working,

Update: After stuck in slow connection when I connect to my db on localhost, I finally find out what happened in this situation,

If we let the hostname is localhost, it takes a long long time to connect ( more than 10s ) that why it makes the connection time out

We have to change the localhost is 127.0.0.1 and it will be as fast as the wind.

The same thing happened if you let the connection string in Node.js is 127.0.0.1 instead of localhost

before:

The server is running in port 6969 !
DB connection succesful! 10046.88992600143 ms

after:

The server is running in port 6969!
DB connection succesful! 43.67024099826813 ms

More info you can get in here: https://github.com/Automattic/mongoose/issues/6211

Mongoose docs also said about that:

If connecting fails on your machine, try using 127.0.0.1 instead of localhost. https://mongoosejs.com/docs/connections.html

这篇关于服务器选择在 10000 毫秒后超时 - 无法将 Compass 连接到本地主机上的 mongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆