与mongodb远程连接

时间:2016-09-21 05:31:14

标签: mongodb amazon-web-services amazon-ec2

我已经通过官方文档在亚马逊的linux环境中安装mongodb。

安装后我做了

[root@ip-172-31-20-51 etc]# service mongod restart
dirname: missing operand
Try 'dirname --help' for more information.
Stopping mongod:                                           [  OK  ]
Starting mongod:                                           [FAILED]

开始失败但是当我只写mongod时说

[root@ip-172-31-20-51 etc]# mongod
2016-09-21T05:23:57.703+0000 I CONTROL  [initandlisten] MongoDB starting : pid=16633 port=27017 dbpath=/data/db 64-bit host=ip-172-31-20-59
2016-09-21T05:23:57.703+0000 I CONTROL  [initandlisten] db version v3.2.9
2016-09-21T05:23:57.703+0000 I CONTROL  [initandlisten] git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c
2016-09-21T05:23:57.703+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.0-fips 29 Mar 2010
2016-09-21T05:23:57.703+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2016-09-21T05:23:57.703+0000 I CONTROL  [initandlisten] modules: none
2016-09-21T05:23:57.704+0000 I CONTROL  [initandlisten] build environment:
2016-09-21T05:23:57.704+0000 I CONTROL  [initandlisten]     distmod: amazon
2016-09-21T05:23:57.704+0000 I CONTROL  [initandlisten]     distarch: x86_64
2016-09-21T05:23:57.704+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2016-09-21T05:23:57.704+0000 I CONTROL  [initandlisten] options: {}
2016-09-21T05:23:57.726+0000 I -        [initandlisten] Detected data files in /data/db created by the 'mmapv1' storage engine, so setting the active storage engine to 'mmapv1'.
2016-09-21T05:23:57.735+0000 I JOURNAL  [initandlisten] journal dir=/data/db/journal
2016-09-21T05:23:57.735+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2016-09-21T05:23:57.899+0000 I JOURNAL  [durability] Durability thread started
2016-09-21T05:23:57.899+0000 I JOURNAL  [journal writer] Journal writer thread started
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten]
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten]
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten]
2016-09-21T05:23:57.907+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2016-09-21T05:23:57.907+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2016-09-21T05:23:57.907+0000 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker

然后在另一个控制台上我开始mongo

[root@ip-172-31-20-59 etc]# mongo
MongoDB shell version: 3.2.9
connecting to: test
Server has startup warnings:
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten]
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten]
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-09-21T05:23:57.905+0000 I CONTROL  [initandlisten]
>

并且在mongod控制台中它接受了连接

2016-09-21T05:25:09.684+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:49068 #1 (1 connection now open)

现在我有几个问题,

1.我无法理解我的mongo是否正在工作? (因为启动mongod总是失败但是在打字时只有mongod正在运行)

2.i无法通过我的robomongo连接到远程mongo服务器它说"无法连接到mongodb"

我的mongo.config文件 - >

 # mongod.conf

    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/

    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /var/log/mongodb/mongod.log

    # Where and how to store data.
    storage:
      dbPath: /var/lib/mongo
      journal:
        enabled: true
    #  engine:
    #  mmapv1:
    #  wiredTiger:

    # how the process runs
    processManagement:
      fork: true  # fork and run in background
    #pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

    # network interfaces
    net:
      port: 27017
      bindIp: 0.0.0.0,10.0.0.1,127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

2 个答案:

答案 0 :(得分:0)

回答你的问题:

  1. 如果可以使用mongo shell进行连接,则服务器正在运行。否则,您会看到Connection refusedexception: connect failed

  2. 等错误
  3. 默认情况下,AWS防火墙非常严格。请检查是否允许来自任何主机的端口27017的传入连接。会有一个警告说您正在向所有人开放端口,但是必须允许连接到数据库。

答案 1 :(得分:0)

您的bindIp设置有点奇怪。为什么逗号分隔列表?只需使用0.0.0.0

之后,在您的安全组中打开端口27017,您就可以连接。