首页 > 解决方案 > 无法从 EC2 连接本地 mongod 服务器,反之亦然

问题描述

无法从 AWS EC2 实例连接我的本地 MongoDB 服务器,反之亦然。

从 EC2 到本地 Mongod 服务器:

root@ip-172-31-31-13:~# mongo -u krishna -p Kmk123@# 192.168.0.104/admin
MongoDB shell version v4.0.12
connecting to: mongodb://192.168.0.104:27017/admin?gssapiServiceName=mongodb
2019-08-26T16:48:14.271+0000 E QUERY    [js] Error: couldn't connect to server 192.168.0.104:27017, connection attempt failed: SocketException: Error connecting to 192.168.0.104:27017 :: caused by :: Connection timed out :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6
exception: connect failed
root@ip-172-31-31-13:~#

从本地到 AWS EC2:

[root@krishna ~]# mongo -u root -p root123 ec2-18-206-199-178.compute-1.amazonaws.com/admin
MongoDB shell version v4.0.12
connecting to: mongodb://ec2-18-206-199-178.compute-1.amazonaws.com:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a6b35f6a-3e71-4ada-b10a-c0783743837b") }
MongoDB server version: 4.0.12
Server has startup warnings:
2019-08-26T16:36:21.897+0000 I STORAGE  [initandlisten]
2019-08-26T16:36:21.897+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-08-26T16:36:21.897+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-08-26T16:36:22.912+0000 I CONTROL  [initandlisten]
2019-08-26T16:36:22.912+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten]
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten]
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten]
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-26T16:36:22.913+0000 I CONTROL  [initandlisten]

启用 MongoDB 的免费基于云的监控服务,该服务将接收并显示有关您的部署的指标(磁盘利用率、CPU、操作统计信息等)。

监控数据将在 MongoDB 网站上提供,您和任何与您共享 URL 的人都可以访问唯一的 URL。MongoDB 可能会使用这些信息来改进产品并向您推荐 MongoDB 产品和部署选项。

要启用免费监控,请运行以下命令:db.enableFreeMonitoring()

要永久禁用此提醒,请运行以下命令:db.disableFreeMonitoring()

> >

我编辑了 EC2 实例的 SG,如下所示:

SSH TCP 22 0.0.0.0/0 自定义 TCP 规则 TCP 27017 0.0.0.0/0 自定义 TCP 规则 TCP 27017::/0

标签: mongodbamazon-ec2remote-access

解决方案


您需要更新mongod.conf文件以允许从远程访问您的 mongodb。当我做下一页中提到的所有事情时,它对我有用。

参考:https ://ianlondon.github.io/blog/mongodb-auth/


推荐阅读