mongodb直连报错:
org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13 (Unauthorized): 'not authorized on zdytPdf to execute command { insert: "pdfOut", ordered: true, documents: [ { _id:
ObjectId('620084cc7f369c0996db96e3'), kprq: "20220207101014", fphm: "10163629", fileName: "05000191001110163629.PDF", pdfFileData: BinData(0, 255044462D312E370A25E2E3CFD30A312030206F626A0A3C3C2F547970652F584F626A6563742F537562747970652F496D6167652F57696474682037352F4865696768742037...), _class: "com.dxhy.tools.filestore.mongo.dto.PdfOut" } ] }' on server 172.16.3.248:27017. The full response is {"ok": 0.0, "errmsg": "not authorized on zdytPdf to execute command { insert: \"pdfOut\", ordered: true, documents: [ { _id: ObjectId('620084cc7f369c0996db96e3'), kprq: \"20220207101014\", fphm: \"10163629\", fileName: \"05000191001110163629.PDF\", pdfFileData: BinData(0, 255044462D312E370A25E2E3CFD30A312030206F626A0A3C3C2F547970652F584F626A6563742F537562747970652F496D6167652F57696474682037352F4865696768742037...), _class: \"com.dxhy.tools.filestore.mongo.dto.PdfOut\" } ] }", "code": 13, "codeName": "Unauthorized"}; nested exception is com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'not authorized on zdytPdf to execute command { insert: "pdfOut", ordered: true, documents: [ { _id: ObjectId('620084cc7f369c0996db96e3'), kprq: "20220207101014", fphm: "10163629", fileName: "05000191001110163629.PDF", pdfFileData: BinData(0, 255044462D312E370A25E2E3CFD30A312030206F626A0A3C3C2F547970652F584F626A6563742F537562747970652F496D6167652F57696474682037352F4865696768742037...), _class: "com.dxhy.tools.filestore.mongo.dto.PdfOut" } ] }' on server 172.16.3.248:27017. The full response is {"ok": 0.0, "errmsg": "not authorized on zdytPdf to execute command { insert: \"pdfOut\", ordered: true, documents: [ { _id: ObjectId('620084cc7f369c0996db96e3'), kprq: \"20220207101014\", fphm: \"10163629\", fileName: \"05000191001110163629.PDF\", pdfFileData: BinData(0, 255044462D312E370A25E2E3CFD30A312030206F626A0A3C3C2F547970652F584F626A6563742F537562747970652F496D6167652F57696474682037352F4865696768742037...), _class: \"com.dxhy.tools.filestore.mongo.dto.PdfOut\" } ] }", "code": 13, "codeName": "Unauthorized"}
Mongodb默认是不需要用户密码就可以连接的,如果使用命令报错"not authorized on admin to execute command ",则表示当前登陆用户不具备相应权限;
d9cea4c622b44053bb7bd919ac65533.png
解决办法:通过创建一个用户,赋予用户root权限
注意:在createUser之前先use admin切换一下。
db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"root",db:"admin"}]
}
);
添加用户权限成功之后,使用root用户登陆,再次使用命令即可成功!!!
1.数据库用户角色:read、readWrite;
2.数据库管理角色:dbAdmin、dbOwner、userAdmin;
3.集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4.备份恢复角色:backup、restore
5.所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6.超级用户角色:root
网友评论