英文:
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.
use products //database
db.createUser(
{
user: "accountUser", //username
pwd: "password" //password
roles: [ "readWrite", "dbAdmin" ] //roles
}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论