如何使用Ubuntu 18.04使mongodb在Azure VM中的特定IP上侦听?

时间:2019-06-10 16:56:23

标签: mongodb azure ubuntu

我使用默认本地主机在具有Ubuntu 18.04的Azure VM上运行了MongoDB(4.0.10)。现在,我想将我的IP地址添加到/etc/mongod.conf中的bindIp,以便可以从我的计算机访问数据库。基于文档(也来自this example),我只需要在逗号后将其附加到bindIp行。

net:
  port: 27017
  bindIp: 127.0.0.1,70.123.39.234
  ipv6: true

(注意,我根据阅读文档添加了ipv6,但似乎没有任何效果)。

但是,它对我不起作用,因为服务无法启动。我得到这样的东西:

$ sudo service mongod restart 
$ sudo service mongod status
    ● mongod.service - MongoDB Database Server
       Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
       Active: failed (Result: exit-code) since Mon 2019-06-10 15:59:35 UTC; 2s ago
         Docs: https://docs.mongodb.org/manual
      Process: 8140 ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf (code=exited, status=48)
     Main PID: 8140 (code=exited, status=48)

    Jun 10 15:59:35 mllinux systemd[1]: Started MongoDB Database Server.
    Jun 10 15:59:35 mllinux systemd[1]: mongod.service: Main process exited, code=exited, status=48/n/a
    Jun 10 15:59:35 mllinux systemd[1]: mongod.service: Failed with result 'exit-code'.

我尝试了很多类似的方式

bindIp: 127.0.0.1, 70.123.39.234
or
bindIp: "127.0.0.1,70.123.39.234"
or
bindIp: "127.0.0.1, 70.123.39.234"

我遇到了同样的错误。

但是,如果我使用空格而不是逗号

bindIp: 127.0.0.1 70.123.39.234
or
bindIp: "127.0.0.1 70.123.39.234"

服务似乎启动正常,因为

$ sudo service mongod restart
$ sudo service mongod status
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-06-10 16:37:39 UTC; 3s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 14629 (mongod)
   CGroup: /system.slice/mongod.service
           └─14629 /usr/bin/mongod --auth --config /etc/mongod.conf

Jun 10 16:37:39 mllinux systemd[1]: Started MongoDB Database Server.

但是,我仍然无法通过笔记本电脑连接到它。

要进行测试,请设置

bindIp: 0.0.0.0
or
bindIpAll: true

然后,我可以从笔记本电脑连接到它。

我做错了什么?

更新:追加数据库日志

2019-06-10T02:52:27.682+0000 I CONTROL  [main] ***** SERVER RESTARTED *****
2019-06-10T02:52:27.684+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] MongoDB starting : pid=3757 port=27017 dbpath=/var/lib/mongodb 64-bit host=mllinux
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] db version v4.0.10
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.0g  2 Nov 2017
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] modules: none
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] build environment:
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten]     distmod: ubuntu1804
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1,70.123.39.234", port: 27017 }, processManagement: { timeZoneInfo: "/usr/share/zoneinfo" }, security: { authorizatio$2019-06-10T02:52:27.697+0000 E STORAGE  [initandlisten] Failed to set up listener: SocketException: Cannot assign requested address
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] now exiting
2019-06-10T02:52:27.697+0000 I CONTROL  [initandlisten] shutting down with code:48


2019-06-10T02:53:53.313+0000 I CONTROL  [main] ***** SERVER RESTARTED *****
2019-06-10T02:53:53.315+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] MongoDB starting : pid=3934 port=27017 dbpath=/var/lib/mongodb 64-bit host=mllinux
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] db version v4.0.10
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.0g  2 Nov 2017
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] modules: none
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] build environment:
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten]     distmod: ubuntu1804
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-06-10T02:53:53.324+0000 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1 70.123.39.234", port: 27017 }, processManagement: { timeZoneInfo: "/usr/share/zoneinfo" }, security: { authorizatio$2019-06-10T02:53:53.325+0000 I STORAGE  [initandlisten] Detected data files in /var/lib/mongodb created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-06-10T02:53:53.325+0000 I STORAGE  [initandlisten]
2019-06-10T02:53:53.325+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-06-10T02:53:53.325+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-06-10T02:53:53.325+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1448M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=jou$2019-06-10T02:53:54.146+0000 I STORAGE  [initandlisten] WiredTiger message [1560135234:146511][3934:0x7fe345319a40], txn-recover: Main recovery loop: starting at 2/6656 to 3/256
2019-06-10T02:53:54.249+0000 I STORAGE  [initandlisten] WiredTiger message [1560135234:249632][3934:0x7fe345319a40], txn-recover: Recovering log 2 through 3
2019-06-10T02:53:54.317+0000 I STORAGE  [initandlisten] WiredTiger message [1560135234:317186][3934:0x7fe345319a40], txn-recover: Recovering log 3 through 3
2019-06-10T02:53:54.366+0000 I STORAGE  [initandlisten] WiredTiger message [1560135234:366900][3934:0x7fe345319a40], txn-recover: Set global recovery timestamp: 0
2019-06-10T02:53:54.485+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-06-10T02:53:54.504+0000 I CONTROL  [initandlisten]
2019-06-10T02:53:54.504+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-06-10T02:53:54.504+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-06-10T02:53:54.504+0000 I CONTROL  [initandlisten]
2019-06-10T02:53:54.515+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongodb/diagnostic.data'
2019-06-10T02:53:54.517+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-06-10T02:56:27.392+0000 I CONTROL  [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2019-06-10T02:56:27.392+0000 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2019-06-10T02:56:27.392+0000 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2019-06-10T02:56:27.395+0000 I CONTROL  [signalProcessingThread] Shutting down free monitoring
2019-06-10T02:56:27.395+0000 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2019-06-10T02:56:27.397+0000 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2019-06-10T02:56:27.397+0000 I STORAGE  [signalProcessingThread] Shutting down session sweeper thread
2019-06-10T02:56:27.397+0000 I STORAGE  [signalProcessingThread] Finished shutting down session sweeper thread
2019-06-10T02:56:27.558+0000 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2019-06-10T02:56:27.558+0000 I CONTROL  [signalProcessingThread] now exiting
2019-06-10T02:56:27.558+0000 I CONTROL  [signalProcessingThread] shutting down with code:0

2 个答案:

答案 0 :(得分:0)

您不能使用bindip来限制从外部访问mongodb。 bindip选择所需的服务器侦听IP,而不是客户端IP。

如果仅希望能够从IP地址连接到服务器,则应使用Azure的Network Security Group功能。这比通过虚拟机上安装的软件进行处理更为安全,因为流量甚至无法到达虚拟机。

答案 1 :(得分:0)

对于您的问题,您应该了解这三个IP的含义,然后您将知道需要执行的操作。

第一个:127.0.0.1

此IP仅用于在机器内部进行测试,无法从外部访问。这只是一个循环IP。

第二个:0.0.0.0

然后设置此IP,然后您可以在外部访问,也可以在内部访问。这意味着该计算机中所有应用程序的本地主机,无论内部还是外部。

第三个:私有IP

这是在您的专用网络中分配的IP,而不是公共IP。例如,它表示您提供的文档中的IP 192.168.1.100。对于Azure VM,这意味着从子网分配的专用IP。但是,仅当您访问此IP时,它才有效。

因此,在您的问题中,是否要同时访问内部和外部。然后,您可以以最简单的方式设置绑定IP:

  1. 0.0.0.0
  2. 私有IP

注意:不要忘记在与您的Azure VM关联的NSG中公开端口。如果您没有特殊要求,建议您绑定IP 0.0.0.0,这是最好的方法。