使用 Mongoose 和 SRV 连接字符串将数据插入 MongoDB Atlas
Inserting data to MongoDB Atlas using Mongoose and SRV connection string
我在 MongoDB Atlas 中创建了一个集群,但似乎无法向其写入数据。
const uri = "mongodb+srv://myUser:myPass@myDB-4myav.mongodb.net/portfolio";
我将此作为我要连接的 uri,每次我尝试写入数据库时,都会收到此错误:
{ MongoError: not authorized on admin to execute command { insert:
"users", documents: [[{name Daniel} {_id
ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
我读到我需要创建一个角色为 "root" 的管理员用户,但是当我使用 mongo shell 连接到我的数据库并尝试创建它时,我得到这个:
Error: couldn't add user: not authorized on admin to execute command
所以基本上我没有可以写入我的数据库的用户。
我还尝试让用户在 MongoDB Atlas 网站(当然是我的集群)上拥有所有可能的角色,然后通过 mongo shell 与它连接, 但也失败了。
总结一下:我在 MongoDB Atlas 上创建了一个新集群。如何向其中写入数据?
在此先致谢,如果我遗漏了一些简单而愚蠢的东西,请随时指出。
{ MongoError: not authorized on admin to execute command { insert: "users", documents: [[{name Daniel} {_id ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
此错误表明您的代码正在尝试将文档插入 admin
数据库,该数据库是为用户和角色信息保留的系统数据库。
在您的连接字符串中,您打算使用 portfolio
数据库。但是,srv
连接字符串格式不支持指定数据库,因此您的选项被忽略,并使用默认数据库 admin
。连接后您需要 select portfolio
数据库。
Mongoose 问题跟踪器 (GitHub issue #6106) 中也报告了这一点,其中一位用户发布了您可以采用的解决方法:
mongoose.connection.on('connected', function() {
// Hack the database back to the right one, because when using mongodb+srv as protocol.
if (mongoose.connection.client.s.url.startsWith('mongodb+srv')) {
mongoose.connection.db = mongoose.connection.client.db('portfolio');
}
console.log('Connection to MongoDB established.')
});
mongoose.connect(...);
I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:
你需要manage users via MongoDB Atlas而不是mongo
shell。您可以创建具有 "Read and write to any database" 角色的用户帐户(或使用高级选项获得更精细的权限)。
您需要从 Mongo Shell,
创建一个具有 root 角色的用户
use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
);
exit;
或者您需要通过图集添加新用户(参考下面的截图)
- 转到 群集
- 单击安全
- 单击添加新用户
- 输入用户名
- 根据 SCRAM-SHA1 方法
创建密码
- 选择角色 Atlas 管理员
- 单击添加用户
创建用户后,转到Clusters-->OverView --> CONNECT
- 验证 IP 白名单
- 选择连接方式:
- 连接MongoShell(我选择)
- 连接您的应用程序
- 连接 MongoDB Compass
$ mongo "mongodb+srv://project-u-s3cgp.azure.mongodb.net/test" --username dbAdmin
MongoDB shell version v3.6.1
Enter password:
connecting to: mongodb+srv://project-u-s3cgp.azure.mongodb.net/test
.........
.........
MongoDB Enterprise project-U-shard-0:PRIMARY>db.user.insert({name:"Daniel"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise O2Ci-U-shard-0:PRIMARY>
我希望,您可以在创建 root 用户后插入记录。
谢谢,
卡尔西克
我在 MongoDB Atlas 中创建了一个集群,但似乎无法向其写入数据。
const uri = "mongodb+srv://myUser:myPass@myDB-4myav.mongodb.net/portfolio";
我将此作为我要连接的 uri,每次我尝试写入数据库时,都会收到此错误:
{ MongoError: not authorized on admin to execute command { insert: "users", documents: [[{name Daniel} {_id ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
我读到我需要创建一个角色为 "root" 的管理员用户,但是当我使用 mongo shell 连接到我的数据库并尝试创建它时,我得到这个:
Error: couldn't add user: not authorized on admin to execute command
所以基本上我没有可以写入我的数据库的用户。
我还尝试让用户在 MongoDB Atlas 网站(当然是我的集群)上拥有所有可能的角色,然后通过 mongo shell 与它连接, 但也失败了。
总结一下:我在 MongoDB Atlas 上创建了一个新集群。如何向其中写入数据?
在此先致谢,如果我遗漏了一些简单而愚蠢的东西,请随时指出。
{ MongoError: not authorized on admin to execute command { insert: "users", documents: [[{name Daniel} {_id ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
此错误表明您的代码正在尝试将文档插入 admin
数据库,该数据库是为用户和角色信息保留的系统数据库。
在您的连接字符串中,您打算使用 portfolio
数据库。但是,srv
连接字符串格式不支持指定数据库,因此您的选项被忽略,并使用默认数据库 admin
。连接后您需要 select portfolio
数据库。
Mongoose 问题跟踪器 (GitHub issue #6106) 中也报告了这一点,其中一位用户发布了您可以采用的解决方法:
mongoose.connection.on('connected', function() {
// Hack the database back to the right one, because when using mongodb+srv as protocol.
if (mongoose.connection.client.s.url.startsWith('mongodb+srv')) {
mongoose.connection.db = mongoose.connection.client.db('portfolio');
}
console.log('Connection to MongoDB established.')
});
mongoose.connect(...);
I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:
你需要manage users via MongoDB Atlas而不是mongo
shell。您可以创建具有 "Read and write to any database" 角色的用户帐户(或使用高级选项获得更精细的权限)。
您需要从 Mongo Shell,
创建一个具有 root 角色的用户use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
);
exit;
或者您需要通过图集添加新用户(参考下面的截图)
- 转到 群集
- 单击安全
- 单击添加新用户
- 输入用户名
- 根据 SCRAM-SHA1 方法 创建密码
- 选择角色 Atlas 管理员
- 单击添加用户
创建用户后,转到Clusters-->OverView --> CONNECT
- 验证 IP 白名单
- 选择连接方式:
- 连接MongoShell(我选择)
- 连接您的应用程序
- 连接 MongoDB Compass
$ mongo "mongodb+srv://project-u-s3cgp.azure.mongodb.net/test" --username dbAdmin
MongoDB shell version v3.6.1
Enter password:
connecting to: mongodb+srv://project-u-s3cgp.azure.mongodb.net/test
.........
.........
MongoDB Enterprise project-U-shard-0:PRIMARY>db.user.insert({name:"Daniel"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise O2Ci-U-shard-0:PRIMARY>
我希望,您可以在创建 root 用户后插入记录。
谢谢, 卡尔西克