远程访问mongodb3.2

不同版本的mongodb可能非常不同。
这里我使用了最新版本,即3.2.10.

允许远程访问的最简单办法是注释掉bindIp: 127.0.0.1
通过mongo example.com:27017/database应该就可以访问了。

这样的安全性肯定比较低。
我们可以开启验证(enable auth)参考链接
我的做法是在/etc/mongod.conf中添加

security:
  authorization: enabled

接下来就要创建并授权用户了。
运行mongo,进入mongo shell
输入use database, database就是你想要授权的数据库。
创建用户的同时,需要给用户一些角色,可以参考这里
示例如下

db.createUser( 
{
   user: "user", 
   pwd: "password",
   roles: [
     { role: "read", db: "reporting" },
     { role: "read", db: "products" },
     { role: "read", db: "sales" },
     { role: "readWrite", db: "accounts" } 
  ]
 })

然后就可以通过mongo example.com:27017/database -u user -p password访问了

如果有程序跟mongodb在同一个服务器,之前可以正常运行的话,开启验证后,也需要配置user和password才可以访问哦。

你可能感兴趣的:(远程访问mongodb3.2)