如何从Node.js连接到远程服务器的MongoDB

スクリーンショット 2014-07-23 15.37.55.png
スクリーンショット 2014-07-23 15.38.05.png

远程服务器的环境是樱花VPS+CentOS。

使用node.js连接远程服务器上的mongoDB的方法

驱动程序将使用Node.JS MongoDB驱动器。

安装 MongoDB:`npm install mongodb`

可以通过安装进行。

这是通过Node.js连接MongoDB的代码。

mongo = require("mongodb")
Server = require('mongodb').Server
Db = mongo.Db
BSON = mongo.BSONPure
assert = require('assert')

db = new Db('db1', new Server('xxx.xx.xx.xx', 27017),{safe:false})
db.open (err, db)->
  if err
    log err
  else
    db.collectionNames (err, items)->
      assert items.length > 0

只需要一个选项:当连接到本地数据库时,只需要将127.0.0.1的设置更改为远程服务器的IP地址,就可以成功连接。

那个?
难道?
这个是不是任何人都可以访问的?

当再次进行调查时,发现MongoDB在初始状态下允许外部访问。这是有问题的。

重新审视MongoDB的安全性

在确定是一个篮子后,我们需要想办法解决问题。
在MySQL等数据库中,我们会首先设置具有访问权限的用户,并禁止使用Root账户来提高安全性。
在MongoDB中也可以进行类似的设置。

1. 管理者的设置

打开Mongo shell并输入以下内容

> use admin
switched to db admin
> db.addUser('user1','password')
{
    "user" : "user1",
    "readOnly" : false,
    "pwd" : "e7e8a26d330dbb8bef5b1886ceb5e290",
    "_id" : ObjectId("53cdf6cf3baadf71b7b2a006")
}

您可以使用以下命令在admin集合中添加用户:addUser(‘用户名’,’密码’)。
目前身份验证设置是无效的,但在启用后,只有以管理员身份登录才能添加用户。

2. 在使用的数据库中设置用户。

> use db1
switched to db db1
> db.addUser('user1','password')
{
    "user" : "user1",
    "readOnly" : false,
    "pwd" : "e7e8a26d330dbb8bef5b1886ceb5e290",
    "_id" : ObjectId("53cdf8d0b31d6d88862bae5e")
}

我已经设置了一个名为「db1」的数据库,并且添加了一个名为「user1」的用户,可以访问该数据库。

启用身份验证

我们将在/etc/mongod.conf中找到配置文件并进行修改。

% sudo vim /etc/mongod.conf

大约在第26行附近,因为有 #auth = true,所以请取消此注释以启用 auth。

#auth = true
↓
auth = true

按下esc键,然后键入:wq以保存设置并完成配置。重新启动mongod。

% sudo service mongod restart

4. 使用Mongo shell访问数据库。

打开Mongo Shell,执行”use db1″,然后尝试使用”show collections”命令获取集合的列表。

> show collections
Tue Jul 22 14:53:01.657 error: {
    "$err" : "not authorized for query on db1.system.namespaces",
    "code" : 16550
} at src/mongo/shell/query.js:128

如果认证设置已经启用,应该会失败。
让我们尝试登录。
可以使用db.auth(‘用户名’,’密码’)进行登录。
然后再次连续执行show collections,应该能够获取到。

> db.auth('user1','password')
1
> show collections
system.indexes
system.users
>

或者,您也可以在打开mongo shell时输入用户名和密码。

% mongo 127.0.0.1:27017/db1 -u user1 -p password
MongoDB shell version: 2.4.10
connecting to: 127.0.0.1:27017/db1
> show collections
system.indexes
system.users

5. 通过本地的node.js访问远程服务器的数据库。

迄今为止,我通过SSH登录服务器并进行工作,
下面是直接从本地访问服务器上的MongoDB的方法。
让我们尝试直接使用开始部分的连接到远程服务器的MongoDB的方法(Node.js)。

{[MongoError:对db1.system.namespaces的查询未授权]
[堆栈]:[获取者/设置者],
[参数]:未定义,
[类型]:未定义,
[消息]:’对db1.system.namespaces的查询未授权’,
名称:’MongoError’}

由于未登录,发生了错误。

使用以下方式对数据库进行身份验证:db.authenticate(‘ユーザー名’, ‘パスワード’, 回调函数(err, result))。

可以使用登录。
如果成功,结果会返回1。

db = new Db('db1', new Server('xxx.xxx.xx.xx', 27017),{safe:false})
    db.open (err, db)->
      if err
        log err
      else
        db.authenticate 'user1', 'password', (err, result)->
          if err
            log err
          else
            db.collectionNames (err, items)->
              if err
                log err
              else
                assert items.length > 0
                log items

总结

管理者设定 zhě
> use admin
> db.addUser('user1','password')
每个数据库的连接用户设置
> use db1
> db.addUser('user1','password')
进行/etc/mongod.conf的配置。
#auth = true
↓
auth = true
使用Mongo Shell进行登录。
db.auth('user1','password')
または
mongo 127.0.0.1:27017/db1 -u user1 -p password
使用Node.js进行登录
db.authenticate 'user1', 'password', (err, result)->

以上所述。

广告
将在 10 秒后关闭
bannerAds