配置本地mongodb安装的用户名和密码

huangapple go评论81阅读模式
英文:

Configuring username and password for local mongodb installation

问题

我已将mongob作为本地服务安装,并配备了mongo compass。我有一个遗留的Java代码,其中连接代码如下:

this.username = "...";
this.password = "...";
this.database = "testdb";
this.host = "localhost";
this.portNum = 27017;
MongoCredential credential = MongoCredential.createCredential(username, database, password.toCharArray());

mongo = MongoClients.create(MongoClientSettings.builder()
                    .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress(host, portNum))))
                    .credential(credential)
                    .build());

在本地安装过程中,我没有看到任何关于用户名/密码配置的选项,但代码需要它。

如何配置这些详细信息呢?

英文:

I have installed mongob as a service locally, along with mongo compass. I have a legacy java code which has connection code as:

this.username = "....";
this.password = "....";
this.database = "testdb";
this.host = "localhost";
this.portNum = 27017;
MongoCredential credential = MongoCredential.createCredential(username, database, password.toCharArray());

mongo = MongoClients.create(MongoClientSettings.builder()
                    .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress(host, portNum))))
                    .credential(credential)
                    .build());

During local installation, I didn't see any option for username/password configuration, but code needs it.

How to get these details configured?

答案1

得分: 1

你可以使用 db.createUser 创建用户。在那时,您需要提供 username, password,这些可以在代码中使用。

来自文档的片段

use products //数据库 
db.createUser(
   {
     user: "accountUser", //用户名
     pwd: "password", //密码 
     roles: [ "readWrite", "dbAdmin" ] //角色
   }
)
英文:

you can create user using db.createUser. At that time, you need to provide username, password which can be used in the code.

Snippet from doc

use products //database 
db.createUser(
   {
     user: "accountUser", //username
     pwd: "password" //password 
     roles: [ "readWrite", "dbAdmin" ] //roles
   }
)

huangapple
  • 本文由 发表于 2020年8月28日 14:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63628474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定