最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】如何允许外网IP远程访问MongoDB数据库

MongoDB crifan 2202浏览 0评论

折腾:

【已解决】阿里云ECS服务器中已有的MongoDB的用户名密码和端口

期间,在创建新的MongoDB的用户之前,

此处更需要先去解决:

允许外网,即此处本地的Mac,去远程连接(位于阿里云ECS服务器中的)MongoDB

不过弄这一切之前,需要先去:

阿里云ECS中申请外网访问27017端口,即外网 入方向 27017

设置好了允许入方向的访问27017端口后,再去:

想要外网可以访问的话,搜:

mongodb net bindIp 

mongodb 允许远程连接

Configuration File Options — MongoDB Manual 3.6

networking – What does the –bindip configuration option in mongodb does? – Stack Overflow

允许所有IP访问:

方法1:

<code>net.bindIp = 0.0.0.0,:: 
</code>

方法2:

<code>net.bindIpAll = True
</code>

但是在设置外网所有IP都能访问之前,要设置用户名和密码,要保证安全

mongodb 远程访问配置 – 都市烟火 – 博客园

要设置:auth=true

然后重启mongo,然后添加用户和授权用户

ubuntu下部署mongodb以及设置允许远程连接 – HaoPeng_Zhang – 博客园

然后再去修改配置:

把bindIp的:127.0.0.1

改为:0.0.0.0

<code>vi /etc/mongod.conf

     26 # network interfaces
     27 net:
     28   port: 27017
     29   bindIp: 0.0.0.0  # Listen to all interfaces
     30 #  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.
</code>

然后重启,发现restart没用,只能用start:

<code>[root@naturling-general-01 ~]# systemctl restart mongod
^C
[root@naturling-general-01 ~]# systemctl status mongod 
mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
   Active: deactivating (stop) since Mon 2018-04-02 10:08:08 CST; 6s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 7452 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS)
  Control: 7497 (mongod)
   CGroup: /system.slice/mongod.service
           control
             7497 /bin/bash /etc/rc.d/init.d/mongod stop
             7511 sleep 10

Apr 02 10:08:06 naturling-general-01 systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Apr 02 10:08:06 naturling-general-01 runuser[7463]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Apr 02 10:08:06 naturling-general-01 runuser[7463]: pam_unix(runuser:session): session closed for user mongod
Apr 02 10:08:06 naturling-general-01 mongod[7452]: Starting mongod: [  OK  ]
Apr 02 10:08:06 naturling-general-01 systemd[1]: Started SYSV: Mongo is a scalable, document-oriented database..
Apr 02 10:08:08 naturling-general-01 systemd[1]: Stopping SYSV: Mongo is a scalable, document-oriented database....
[root@naturling-general-01 ~]# systemctl start mongod  
[root@naturling-general-01 ~]# systemctl status mongod
[0m mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
   Active: active (running) since Mon 2018-04-02 10:08:19 CST; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 7497 ExecStop=/etc/rc.d/init.d/mongod stop (code=exited, status=0/SUCCESS)
  Process: 7518 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mongod.service
           7533 /usr/bin/mongod -f /etc/mongod.conf

Apr 02 10:08:18 naturling-general-01 systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Apr 02 10:08:18 naturling-general-01 runuser[7529]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Apr 02 10:08:19 naturling-general-01 runuser[7529]: pam_unix(runuser:session): session closed for user mongod
Apr 02 10:08:19 naturling-general-01 mongod[7518]: Starting mongod: [  OK  ]
Apr 02 10:08:19 naturling-general-01 systemd[1]: Started SYSV: Mongo is a scalable, document-oriented database..
</code>

然后:

<code>[root@naturling-general-01 ~]# ps -ef|grep mongo 
mongod    7533     1  0 10:08 ?        00:00:00 /usr/bin/mongod -f /etc/mongod.conf
root      7567  7288  0 10:09 pts/0    00:00:00 grep --color=auto mongo
[root@naturling-general-01 ~]# netstat -lanp | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      7533/mongod         
unix  2      [ ACC ]     STREAM     LISTENING     2184163  7533/mongod          /tmp/mongodb-27017.sock
</code>

然后:

【已解决】连接远程mongoDB失败:Failed to connect to after 5000ms milliseconds giving up

转载请注明:在路上 » 【已解决】如何允许外网IP远程访问MongoDB数据库

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
56 queries in 0.235 seconds, using 18.87MB memory