如何在Linode VPS上访问MongoDB?
我有一个nodejs应用程序在我的Linode VPS上运行,安装了MongoDB。我无法连接到同一VPS上的数据库,因为它会给我“未经授权”的错误 在VPS上设置数据库和用户 要在我的MongoDB实例上设置数据库,我将SSH连接到我的VPS并输入mongo shell:如何在Linode VPS上访问MongoDB?,mongodb,vps,linode,Mongodb,Vps,Linode,我有一个nodejs应用程序在我的Linode VPS上运行,安装了MongoDB。我无法连接到同一VPS上的数据库,因为它会给我“未经授权”的错误 在VPS上设置数据库和用户 要在我的MongoDB实例上设置数据库,我将SSH连接到我的VPS并输入mongo shell: $ mongo 然后我切换到管理数据库,并试图查看它的用户。第一件事是它不让我这么做: use admin switched to db admin show users 我得到的错误如下: 2020-05-22T12:
$ mongo
然后我切换到管理数据库,并试图查看它的用户。第一件事是它不让我这么做:
use admin
switched to db admin
show users
我得到的错误如下:
2020-05-22T12:13:25.362+0100 E QUERY [js] Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID("bbb18683-839f-4c7d-b453-f774cbc94efd") }, $db: "admin" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1763:1
shellHelper.show@src/mongo/shell/utils.js:859:9
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
use appDB
switched to db appDB
db.createUser({user: "DBuser",pwd: "DBpassword",roles: ["readWrite"]})
Successfully added user: { "user" : "DBuser", "roles" : [ "readWrite" ] }
我继续,在管理数据库中创建一个用户,这样我就可以在我的应用程序数据库中创建一个用户,使我能够连接到应用程序的数据库:
db.createUser({user: "superAdmin",pwd: "admin123",roles: [ { role: "root", db: "admin" } ]})
这是成功的:
Successfully added user: {
"user" : "superAdmin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
因此,我随后重新加载mongod服务,并与我刚才创建的用户一起登录shell:
mongo --port 27017 -u "superAdmin" -p "admin123" --authenticationDatabase "admin"
然后,我切换到我的应用程序的数据库,并让用户如下所示:
2020-05-22T12:13:25.362+0100 E QUERY [js] Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID("bbb18683-839f-4c7d-b453-f774cbc94efd") }, $db: "admin" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1763:1
shellHelper.show@src/mongo/shell/utils.js:859:9
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
use appDB
switched to db appDB
db.createUser({user: "DBuser",pwd: "DBpassword",roles: ["readWrite"]})
Successfully added user: { "user" : "DBuser", "roles" : [ "readWrite" ] }
使用同一LINODE上的应用程序创建的用户访问数据库
在同一linode VPS上的node.js应用程序中,我使用以下连接URI:
'mongodb://DBuser:DBpassword@localhost:27017/appDB'
现在,当我运行seeds文件时,在SSH插入VPS的同时,为了填充VPS上的数据库,我得到了一个未经授权的错误:
errmsg:
'not authorized on appDB to execute command { dropDatabase: 1, lsid: { id: UUID("b2c1ffc7-1912-42e3-8f74-ab3726dff3f2") }, $db: "appDB" }',
code: 13,
codeName: 'Unauthorized',
当我尝试使用我的前端应用程序在数据库中创建新记录时,甚至会发生此错误
我甚至无法通过网络访问VPS上的数据库实例
这是所有工作在本地没有(用户名和密码)在数据库的URL
如何正确设置MongoDB实例以接受连接
任何帮助都将不胜感激
PS-我按照以下说明设置了我的Linode:您给了用户读写角色,但该角色不允许执行命令dropDatabase,只有dbAdmin角色可以这样做。 请参见此处的可用内置角色: 理想情况下,您应该创建一个具有所有必需特权的自定义角色,并将该角色分配给应用程序的用户。 有关用户定义角色的更多信息,请参见此处: